import os
import sys
import json

def loadData(parFile):
    #parameterFile is a json with settings

    with open(parFile,'r') as f:
        pars=json.load(f)

    ds=openData(pars['data']['filename'],pars['data']['format'])
    #var names are here
    #print(ds[0])
    #open data
    transferToDestination(ds,pars)

def openData(qFile,qFormat="DTA"):
    if qFormat=="DTA":
        import pandas as pd
        ds=pd.read_stata(qFile)
        #print(ds.dtypes)
        rows=[]
        n=len(ds.columns)
        rng=range(len(ds.columns))
        for row in ds.itertuples(index=False):
            varRow={ds.columns[i]:row[i] for i in rng if not pd.isna(row[i])}
            rows.append(varRow)
        return rows

def transferToDestination(rows,pars):
    dest=pars['destination']
    if dest['format']=='LabKey':
        return transferToLabKey(rows,pars)

def transferToLabKey(rows,pars):
    src=pars['data']
    dest=pars['destination']
    fsetup=os.path.join(os.path.expanduser('~'),'.labkey','setup.json')
    with open(fsetup,'r') as f:
        setup=json.load(f)

    sys.path.append(setup['paths']['nixWrapper'])
    import nixWrapper
    nixWrapper.loadLibrary('labkeyInterface')
    import labkeyInterface
    import labkeyDatabaseBrowser

    net=labkeyInterface.labkeyInterface()
    net.init(dest['server'])
    db=labkeyDatabaseBrowser.labkeyDB(net)

    for r in rows:
        idField=dest['participantField']
        idValue=r[src['idField']]
        idFilter={'variable':idField,'value':'{}'.format(idValue),'oper':'eq'}

        try: 
            sField=dest['visitField']
        except KeyError:
            sField='SequenceNum'
        try:
            seqNum=src['visitField']
        except KeyError:
            seqNum="0.0"
        
        visitFilter={'variable':sField,'value':seqNum,'oper':'eq'}

        ds=db.selectRows(dest['project'],dest['schema'],dest['query'],
                [idFilter,visitFilter])
        if len(ds['rows'])>0:
            mode='update'
            outRow=ds['rows'][0]
        else:
            mode='insert'
            outRow={idField:idValue,sField:seqNum}
        for var in r.keys():
            outRow[var]=r[var]
        
        resp=db.modifyRows(mode,dest['project'],dest['schema'],dest['query'],
                [outRow])
        print(resp)
    print('Done')

        



if __name__=="__main__":
    loadData(sys.argv[1])
