import os
import json
import re

def cmdMatlab():
   pwd=os.path.dirname(os.path.abspath(__file__))
   pwdUp=os.path.dirname(pwd)
   mDir=os.path.join(pwdUp,'matlab')
   with open(os.path.join(os.path.expanduser('~'),'.labkey','setup.json'),'r') as f:
      siteSetup=json.load(f)
   mExec=siteSetup['paths']['matlab']

   cmds=[mExec]
   cmds.append('-sd')
   cmds.append(mDir)
   #cmds.append('-batch')
   return cmds
 
def getPatientId(row,xconfig):
   return row[getPatientField(xconfig)]

def getPatientField(xconfig):
   return xconfig['ParticipantField']

def getVisitField(xconfig):
   return 'visitName'

def getVisitId(row,xconfig):
   return row[getVisitField(xconfig)]

def getIdFilter(row,xconfig):
   return {'variable':getPatientField(xconfig),\
      'value':getPatientId(row,xconfig),
      'oper':'eq'}

def getVisitFilter(row,xconfig):
   return {'variable':getVisitField(xconfig),\
      'value':getVisitId(row,xconfig),
      'oper':'eq'}

def getCode(row,xconfig):
   return '{}_{}'.format(getPatientId(row,xconfig),getVisitId(row,xconfig))

def getTargetSeqNum(row,xconfig):
   if getVisitId(row,xconfig)=='OBR':
      return 2
   return 1

def getPathList(row,xconfig):
   return [xconfig['baseDir'],getPatientId(row,xconfig),getVisitId(row,xconfig)]

def getOutputDir(row,xconfig):
   return '/'.join(getPathList(row,xconfig))


def getTempDir(xconfig):
   tempDir=xconfig['tempDir']
   pathList=tempDir.split('/')
   pathList.insert(0,os.path.expanduser('~'))
   return os.path.join(*pathList)
 

def getLocalDir(row,xconfig):
   return os.path.join(getTempDir(xconfig),getCode(row,xconfig))

def getNodeName(row,xconfig,mode,i=0):
   if mode=='CT':
      return getCode(row,xconfig)+'_CT'
   if mode=='NM':
      return '{}_Volume{}'.format(getCode(row,xconfig),i)
   return getCode(row,xconfig)+'_Dummy'

def decode(code,xconfig):
   #invert code and return object equivalent to row with relevant
   #fields set that can be used in 
   #getPatientId, getVisitId,getIdFilter,getVisitFilter 
   #as an equivalent replacement for r
   values=code.split('_')
   fid=values[0]
   vid=values[1]
   return {getPatientField(xconfig):fid,getVisitField(xconfig):vid}

def printRowCenterNames(r,setup):
   names=[]
   nr=setup['nr']
   nclass=setup['nclass']
   code=getCode(r,setup)
   #tempDir=config.getTempDir(setup)
   for nc in nclass:
      for j in range(nr):
         for i in range(nc): 
            fCode='{}_{}_{}_center{}'.format(code,nc,j+1,i+1)
            names.append('{}_center.txt'.format(fCode))
            names.append('{}_centerWeight.nrrd'.format(fCode))
   return names
 
def getFitParFinalName(code,nc,j,aType):
   fCode='{}_{}_{}'.format(code,nc,j+1)
   if aType!='global':
      fCode='{}_{}'.format(fCode,aType)
   
   return '{}_fitParFinal.txt'.format(fCode)

def printFitParFinalRowNames(r,setup,aType):
   names=[]
   nr=setup['nr']
   nclass=setup['nclass']
   code=getCode(r,setup)
   #tempDir=config.getTempDir(setup)
   for nc in nclass:
      for j in range(nr):
         names.append(getFitParFinalName(code,nc,j,aType))
         #names.append('{}_centerWeight.nrrd'.format(fCode))
   return names
 
def getPixelFitParFinalName(code,nc,s2,mode):
   sigmaCode='{:.2f}'.format(s2)
   sigmaCode=re.sub('\.','p',sigmaCode)
   fName='{}_{}_{}_Pixel'.format(code,nc,sigmaCode)
   if mode!='general':
      fName='{}{}'.format(fName,mode)
   fName='{}_fitParFinal.txt'.format(fName)
   return fName

def getPixelFitParFinalPngName(code,nc,i,s2,mode):
   sigmaCode='{:.2f}'.format(s2)
   sigmaCode=re.sub('\.','p',sigmaCode)
   fName='{}_{}_{}_Pixel'.format(code,nc,sigmaCode)
   if mode!='general':
      fName='{}{}'.format(fName,mode)
   fName='{}_centers{}.png'.format(fName,i)
   return fName


def printPixelFitParFinalRowNames(r,setup,nc,s2,mode):
   names=[]
   code=getCode(r,setup)
   names.append(getPixelFitParFinalName(code,nc,s2,mode))
   for i in range(nc):
      names.append(getPixelFitParFinalPngName(code,nc,i+1,s2,mode))
   return names