import os
import sys
import config
import analysis
import json

def connectDB(server):
   sPath=os.path.join(os.path.expanduser('~'),'.labkey','setup.json')
   with open(sPath,'r') as f:
      setup=json.load(f)

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


   net=labkeyInterface.labkeyInterface()
   fconfig=os.path.join(os.path.expanduser('~'),'.labkey',server)
   net.init(fconfig)
   net.getCSRF()
   import labkeyDatabaseBrowser
   import labkeyFileBrowser
   return labkeyDatabaseBrowser.labkeyDB(net),labkeyFileBrowser.labkeyFileBrowser(net)

def getDataset(db,setup,datasetName,qfilter=[]):
    ds=db.selectRows(setup['project'],setup['schemaName'],\
      setup[datasetName],qfilter)
    try:
        rows=ds['rows']
    except KeyError:
        rows=[]
    return rows

def updateDataset(db,setup,datasetName,mode,rows):
   db.modifyRows(mode,setup['project'],setup['schemaName'],\
      setup[datasetName],rows)
   
def getPatients(db,setup,qfilter=[]):
   return getDataset(db,setup,'queryName',qfilter)

def getSegmentation(db,setup,qfilter=[]):
   return getDataset(db,setup,'segmentationQueryName',qfilter)

def getSummary(db,setup,qfilter=[]):
   return getDataset(db,setup,'summaryQueryName',qfilter)

def updatePatients(db,setup,mode,rows):
   updateDataset(db,setup,'queryName',mode,rows)

def updateSegmentation(db,setup,mode,rows):
   updateDataset(db,setup,'segmentationQueryName',mode,rows)

def updateSummary(db,setup,mode,rows):
   updateDataset(db,setup,'summaryQueryName',mode,rows)

def downloadNode(fb,fileName,rPath,lPath):
    rPath1=rPath+'/'+fileName
    available=fb.entryExists(rPath1)
    if not available:
        print('Missing {}'.format(fileName))
        return
    lPath1=os.path.join(lPath,fileName)
    if os.path.isfile(lPath1):
        return
    print('Loading {}'.format(fileName))
    fb.readFileToFile(rPath1,lPath1)
    
def downloadFiles(fb,r,setup):
    rPath=fb.formatPathURL(setup['project'],config.getOutputDir(r,setup))
    lPath=config.getLocalDir(r,setup)
    if not os.path.isdir(lPath):
        os.makedirs(lPath)
    
    #CT
    fileName=config.getNodeName(r,setup,'CT')+'.nrrd'
    downloadNode(fb,fileName,rPath,lPath)
    
    for i in range(20):
        fileName=config.getNodeName(r,setup,'NM',i)+'.nrrd'
        downloadNode(fb,fileName,rPath,lPath)
    
    fileName=config.getNodeName(r,setup,'Dummy')+'.mcsv'
    downloadNode(fb,fileName,rPath,lPath)
    
def downloadPatientFiles(db,fb,setup,qfilter=[]):
    rows=getPatients(db,setup,qfilter)
    for r in rows:
        #download
        downloadFiles(fb,r,setup)

def uploadCenters(fb,r,setup):
   names=config.printRowCenterNames(r,setup)
   copyToServer(fb,r,setup,names)

def downloadCenters(fb,r,setup):
   names=config.printRowCenterNames(r,setup)
   copyFromServer(fb,r,setup,names)

def uploadFitParFinal(fb,r,setup,mode):
   names=config.printFitParFinalRowNames(r,setup,mode)
   #copy files to server
   copyToServer(fb,r,setup,names)

def copyToServer(fb,r,setup,names):
   tempDir=config.getTempDir(setup)
   code=config.getCode(r,setup)
   project=setup['project']
   remoteDir=fb.buildPathURL(project,config.getPathList(r,setup))
   for n in names:
      localPath=os.path.join(tempDir,code,n)
      remotePath='{}/{}'.format(remoteDir,n)
      fb.writeFileToFile(localPath,remotePath)

def copyFromServer(fb,r,setup,names):
   try:
      forceReload=setup['forceReload']
   except KeyError:
      forceReload=False

   tempDir=config.getTempDir(setup)
   code=config.getCode(r,setup)
   project=setup['project']
   remoteDir=fb.buildPathURL(project,config.getPathList(r,setup))
   for n in names:
      localPath=os.path.join(tempDir,code,n)
      if os.path.isfile(localPath) and not forceReload:
         continue
      remotePath='{}/{}'.format(remoteDir,n)
      fb.readFileToFile(localPath,remotePath)


    
