"""
    PrepareDeposit_gui.py
    Copyright (C) 2015 Newcastle University
    Author: Martin Noble
    
    """

from PyQt4 import QtGui,QtCore

from CCP4TaskWidget import CTaskWidget

#-------------------------------------------------------------------
class PrepareDeposit_gui(CTaskWidget):
    #-------------------------------------------------------------------
    TASKMODULE = 'export'       # Where this plugin will appear on the gui
    TASKTITLE = 'Prepare files for deposition'     # A short title for gui menu
    DESCRIPTION = 'Prepare files for deposition'
    TASKVERSION = 0.1
    TASKNAME = 'PrepareDeposit'
    
    def drawContents(self):        
        self.openFolder(folderFunction='inputData')
        
        self.createLine(['label','Refinement in final step used reflection','stretch','widget','USINGIORF'])
        self.connect(self.container.inputData.USINGIORF,QtCore.SIGNAL('dataChanged'),self.affectsReflectionContentFlag)
        
        #self.createLine(['label','Final refinement step exploited anomalous','stretch','widget','USINGANOMALOUS'])
        self.connect(self.container.inputData.USINGANOMALOUS,QtCore.SIGNAL('dataChanged'),self.affectsReflectionContentFlag)
        
        self.createLine(['label','Provide sequences of crystallized species','stretch','widget','PROVIDESEQUENCES'])
        self.connect(self.container.inputData.PROVIDESEQUENCES,QtCore.SIGNAL('dataChanged'),self.handleProvideSequences)
        self.createLine(['widget','-title','Sequences of crystallized species','SEQUENCE_LIST'],toggle=['PROVIDESEQUENCES','open',[True]])
        
        self.createLine(['label','Provide Refined TLS parameters','stretch','widget','PROVIDETLS'])
        self.connect(self.container.inputData.PROVIDETLS,QtCore.SIGNAL('dataChanged'),self.handleTLS)
        self.createLine(['widget','-title','Refined TLS parameter object','TLSIN'],toggle=['PROVIDETLS','open',[True]])
        
        self.createLine(['label','Provide DICT file for ligand in structure','stretch','widget','PROVIDEDICT'])
        self.connect(self.container.inputData.PROVIDEDICT,QtCore.SIGNAL('dataChanged'),self.handleDICT)
        self.openSubFrame(frame=[True], toggle=['PROVIDEDICT','open',[True]])
        self.createLine ( [ 'widget','DICT' ] )
        self.closeSubFrame()
        
        #From here on, GUI is autogenerated from the keywords container
        self.autoGenerate(container=self.container.inputData,selection={'excludeParameters' : ['USINGIORF','USINGANOMALOUS','PROVIDESEQUENCES','SEQUENCE_LIST','PROVIDEDICT','DICT','PROVIDETLS','TLSIN','OUTPUT_DIRECTORY']})
        #Explain and provide UI fo rwhere to put output
        self.createLine(['advice',''])
        self.openSubFrame(frame=[True])
        self.createLine(['advice','The task will generate files "Reflections.cif" and "Coordinates.cif"'])
        self.createLine(['advice','which can be uploaded to the deposition service'])
        self.createLine(['label','Directory in which to place the CIF files','widget','OUTPUT_DIRECTORY'])
        self.closeSubFrame()
    
    def showMolin(self):
        return self.container.inputData.PROVIDEMOL and not self.container.inputData.MOLSMILESORSKETCH != 'SMILES'

    def affectsReflectionContentFlag(self):
        if self.container.inputData.USINGIORF.__str__() == 'I':
            if self.container.inputData.USINGANOMALOUS: requiredContentFlag = 1
            else: requiredContentFlag = 3
        elif self.container.inputData.USINGIORF.__str__() == 'F':
            if self.container.inputData.USINGANOMALOUS: requiredContentFlag = 2
            else: requiredContentFlag = 4
        print self.container.inputData.USINGIORF
        print self.container.inputData.USINGANOMALOUS
        print requiredContentFlag
        self.container.inputData.F_SIGF.setQualifiers({'requiredContentFlag' : [requiredContentFlag]})

    def handleProvideSequences(self):
        if self.container.inputData.PROVIDESEQUENCES:
            self.container.inputData.SEQUENCE_LIST.setQualifiers({'minLength' : [1]})
            self.container.inputData.SEQUENCE_LIST.setQualifiers({'maxLength' : [100]})
        else:
            self.container.inputData.SEQUENCE_LIST.setQualifiers({'minLength' : [0]})
            self.container.inputData.SEQUENCE_LIST.setQualifiers({'maxLength' : [0]})

    def handleTLS(self):
        if not self.container.inputData.PROVIDETLS:
            self.container.inputData.TLSIN.unSet()
            self.container.inputData.TLSIN.setQualifiers({'allowUndefined' : True})
        else:
            self.container.inputData.TLSIN.setQualifiers({'allowUndefined' : False})

    def handleDICT(self):
        if not self.container.inputData.PROVIDEDICT:
            self.container.inputData.DICT.unSet()
            self.container.inputData.DICT.setQualifiers({'allowUndefined' : True})
        else:
            self.container.inputData.DICT.setQualifiers({'allowUndefined' : False})
