import os
import json
import re
import subprocess
import shutil
import sys
import numpy
import pydicom
import copy

shome=os.path.expanduser('~nixUser')
sys.path.insert(1,shome+'/software/src/labkeyInterface')
import labkeyInterface
import labkeyDatabaseBrowser

sys.path.insert(1,shome+'/software/src/orthancInterface')
import orthancInterface
import orthancDatabaseBrowser

sys.path.insert(1,shome+'/software/src/IPNUMM/dicomUtils')
import loadDicom


fhome=os.path.expanduser('~')
fconfig=os.path.join(fhome,'.labkey','network.json')
net=labkeyInterface.labkeyInterface()
net.init(fconfig)
db=labkeyDatabaseBrowser.labkeyDB(net)


#also need merlin credentials

fconfigMerlin=os.path.join(fhome,'.labkey','merlin.json')
netMerlin=labkeyInterface.labkeyInterface()
netMerlin.init(fconfigMerlin)
dbMerlin=labkeyDatabaseBrowser.labkeyDB(netMerlin)

onetMerlin=orthancInterface.orthancInterface()
onetMerlin.init(fconfigMerlin)
odbMerlin=orthancDatabaseBrowser.orthancDB(onetMerlin)


project='iPNUMMretro/Study'
merlinProject=project
#project='Orthanc/Database'

labkeyBase='/data/labkey'
tempBase=os.path.join(fhome,'temp')

#anonymousClinicalDataset='AnonymousClinicalData'
anonymousImagingDataset='AnonymousImaging'
merlinImagingDataset='Imaging'

#getNixID
studyData=db.selectRows(project,'study','Study',[])
nixID=studyData['rows'][0]['nixID']

ds=db.selectRows(project,'study',anonymousImagingDataset,[])

#get UID generator
uid=loadDicom.uuid()



#paths 
projectAnonymousBase=os.path.join(labkeyBase,'files',project,'@files/anonymous')
i=0
status="OK"
for row in ds['rows']:
    outRow=copy.deepcopy(row)
    outRow['PatientId']='{}-{}'.format(nixID,row['PatientId'])

    
    studyUID=uid.generateStudyUUID('volume')
    for iMod in ['CT','PETWB']:
        anonSeriesId=row[iMod+'_UUID']
        dicomZipFile=os.path.join(projectAnonymousBase,anonSeriesId+'.zip')
        tempDir=os.path.join(tempBase,anonSeriesId)
        try:
            os.mkdir(tempDir)
        except FileExistsError:
            shutil.rmtree(tempDir)
            os.mkdir(tempDir)

        subprocess.run(['unzip','-d',tempDir,'-xj',dicomZipFile])
        seriesUID=uid.generateSeriesUUID('volume')
        for f in os.listdir(tempDir):
            dicomFile=os.path.join(tempDir,f)
            #modify patientId field?
            dcm=pydicom.dcmread(dicomFile)
            dcm['PatientID'].value='{}-{}'.format(nixID,row['PatientId'])
            #make sure series and study UID are set:
            try:
                outRow['studyUUID']=dcm['StudyInstanceUID']
            except KeyError:
                dcm.StudyInstanceUID=studyUID
                outRow['studyUUID']=studyUID

            try:
                outRow[iMod+'_UUID']=dcm['SeriesInstanceUID']
            except KeyError:
                dcm.SeriesInstanceUID=seriesUID
                outRow[iMod+'_UUID']=seriesUID
                
            dcm.save_as(dicomFile)
            print('Modified: {}'.format(dicomFile))
            #send instance to orthanc
            resp=odbMerlin.upload(dicomFile)
            try:
                if resp['status']=="FAIL":
                    sys.exit()
            except KeyError:
                pass

            outRow[iMod+'_orthancId']=resp['ParentSeries']
            outRow['orthancId']=resp['ParentStudy']
            outRow['patientOrthancId']=resp['ParentPatient']
            
            #keep track of the ID to change instance
            print(resp)
            if i==-1:
                break
        shutil.rmtree(tempDir)
        if i==-1:
            break
    #check on studyUID for duplicates
    studyFilter={'variable':'studyUUID','value':outRow['studyUUID'],'oper':'eq'}
    dsMerlin=dbMerlin.selectRows(merlinProject,'study',\
            merlinImagingDataset,[studyFilter])

    mode='insert'
    if len(dsMerlin['rows'])>0:
        mode='update'

    dbMerlin.modifyRows(mode,merlinProject,'study',\
            merlinImagingDataset,[outRow])

    if i==-1:
        break
    i=i+1

print('Done')
