import os

from pypif import pif
import pypif.obj as pifobj 

from ... import Operation as opmod 
from ...Operation import Operation
        
class ShipToDataSet(Operation):
    """
    Take a pypif.obj.System object and ship it to a given Citrination data set.    
    """

    def __init__(self):
        input_names = ['pif','client','dsid','json_dirpath','json_filename','keep_json','ship_flag']
        output_names = ['response']
        super(ShipToDataSet,self).__init__(input_names,output_names)
        self.input_doc['pif'] = 'A pypif.obj.System object or an array/list thereof'
        self.input_doc['client'] = 'A running Citrination client' 
        self.input_doc['dsid'] = 'Data set ID where the pif record(s) will be stored on Citrination' 
        self.input_doc['json_dirpath'] = 'Filesystem path where a json file of the pif(s) will be saved' 
        self.input_doc['json_filename'] = 'Name of the .json file where the pif(s) will be saved' 
        self.input_doc['keep_json'] = 'Flag for whether or not to keep the json file' 
        self.input_doc['ship_flag'] = 'Flag for shipping the pif- set to False for a dry run' 
        self.output_doc['response'] = 'The Citrination server response to the shipment'
        self.input_type['pif'] = opmod.workflow_item
        self.input_type['client'] = opmod.plugin_item
        self.inputs['ship_flag'] = False
        self.inputs['keep_json'] = False

    def run(self):
        cl = self.inputs['client'] 
        dsid = self.inputs['dsid'] 
        p = self.inputs['pif']        
        json_dir = self.inputs['json_dirpath']
        json_file = self.inputs['json_filename']
        if cl is None or dsid is None or p is None \
        or json_dir is None or json_file is None:
            return
        if not os.path.splitext(json_file)[1] == 'json':
            json_file = json_file+'.json'
        json_file = os.path.join(json_dir,json_file)

        json_flag = self.inputs['keep_json']
        ship_flag = self.inputs['ship_flag']
        try:
            # make p an array of pifs to get a big json that has all records
            pif.dump(p, open(json_file,'w'))
            if ship_flag:
                r = cl.upload_file(json_file,dataset_id = dsid)
            else:
                r = 'dry run: no shipment occurred. pif object: {}'.format(pif.dumps(p))
            if not json_flag:
                os.remove(json_file) 
        except Exception as ex:
            r = 'An error occurred while shipping. Error message: {}'.format(ex.message)
        self.outputs['response'] = r


