import os
import json
import re
import subprocess
import nibabel
import shutil
import sys

if len(sys.argv)<2:
    print("Usage {} sourceDir version(v1 or similar)".format(sys.argv[0]))
    sys.exit(0)

sourceDir=sys.argv[1]
ver=sys.argv[2]

shome=os.path.expanduser('~nixUser')
fhome=os.path.expanduser('~')
with open(os.path.join(fhome,".labkey","setup.json")) as f:
    setup=json.load(f)

sys.path.insert(0,setup["paths"]["labkeyInterface"])
import labkeyInterface
import labkeyDatabaseBrowser
import labkeyFileBrowser

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

fconfig=os.path.join(fhome,'.labkey','network.json')

net=labkeyInterface.labkeyInterface()
net.init(fconfig)
db=labkeyDatabaseBrowser.labkeyDB(net)
fb=labkeyFileBrowser.labkeyFileBrowser(net)

project='iPNUMMretro/Study'
dataset='Imaging1'
tempBase=os.path.join(fhome,'temp')

#all images from database
ds=db.selectRows(project,'study',dataset,[])
#imageSelector={"CT":"CT","PET":"PETWB"};
imageResampledField={"Segm":"Segmentation"}

participantField='PatientId'
#for prosepective
#participantField='ParticipantId'

#projectNIfTIBase=os.path.join(labkeyBase,'files',project,'@files/nifti')
#use webdav to transfer file (even though it is localhost)


def getPatientLabel(row,participantField='PatientId'):
    return row[participantField].replace('/','_') 

def getVisitLabel(row):
    return 'VISIT_'+str(int(row['SequenceNum']))

def getStudyLabel(row,participantField='PatientId'):
    return getPatientLabel(row,participantField)+'-'+getVisitLabel(row)

def updateRow(project,dataset,row,imageResampledField,gzFileNames):
    for im in imageResampledField:
        row[imageResampledField[im]]=gzFileNames[im]
    db.modifyRows('update',project,'study',dataset,[row])
 
i=0
for row in ds["rows"]:

    #interesting files are processedDir/studyName_CT_notCropped_2mmVoxel.nii
    #asn processedDir/studyName_PET_notCropped_2mmVoxel.nii

    #standard names provided by Zan and Daniel
    baseFileNames={im:\
            getStudyLabel(row,participantField)+'_'+im \
                for im in imageResampledField}
    
    #append suffix to base name for source files
    gzSrcFileNames={im:baseFileNames[im]+'.nii.gz'\
            for im in baseFileNames}

    #add version to out files
    gzOutFileNames={im:baseFileNames[im]+'_'+ver+'.nii.gz'\
            for im in baseFileNames}

    #build/check remote directory structure
    remoteDir=fb.buildPathURL(project,\
            ['preprocessedImages',getPatientLabel(row,participantField),\
            getVisitLabel(row)])

    #target files
    gzRemoteFiles={im:remoteDir+'/'+f\
            for (im,f) in gzOutFileNames.items()}

    remoteFilePresent=[fb.entryExists(f)\
            for f in gzRemoteFiles.values()]

    for f in gzRemoteFiles.values():
        print("[{}]: [{}]".format(f,fb.entryExists(f)))


    if all(remoteFilePresent):
        print("Entry for row done.")
        updateRow(project,dataset,row,imageResampledField,\
                gzOutFileNames)
        continue

    inputDir=fb.buildPathURL(project,[sourceDir]) 
    inputFiles={im:inputDir+'/'+f for (im,f) in gzSrcFileNames.items()}
    
    for im in inputFiles:
        f=inputFiles[im]
        if not fb.entryExists(f):
            print("Input file {} not found".format(f))
            continue
        print("Found {}".format(f))
        localFile=os.path.join(tempBase,gzSrcFileNames[im])
        print("Local {}".format(localFile))
        fb.readFileToFile(f,localFile)
        fb.writeFileToFile(localFile,gzRemoteFiles[im])
        print("Remote {}".format(gzRemoteFiles[im]))
        os.remove(localFile)

    #update row and let it know where the processed files are
    updateRow(project,dataset,row,imageResampledField,gzOutFileNames)
   

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


print("Done")
