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

from PyQt4 import QtGui,QtCore

import ShelxCE_gui

def whatNext(jobId=None,childTaskName=None,childJobNumber=None,projectName=None):
    import os
    from lxml import etree
    import CCP4Modules, CCP4Utils, CCP4File, CCP4Container, CCP4Data, CCP4ModelData
    returnList = ['coot_rebuild','parrot',['buccaneer_build_refine_mr','$CCP4I2/pipelines/bucref_mr/script/bucref_after_experimental.params.xml']]
    try:
        jobDirectory = CCP4Modules.PROJECTSMANAGER().db().jobDirectory(jobId=jobId)
        
        cont = CCP4Modules.PROJECTSMANAGER().getJobParams(jobId)
        taskName='phaser_EP_AUTO'
        fileRoot = 'Subsequent'+taskName+'.params.xml'
        paramsPath = os.path.normpath(os.path.join(jobDirectory,fileRoot))
        if True or not os.path.isfile(paramsPath):
            pdbDataFile = CCP4ModelData.CPdbDataFile(cont.inputData.HAIN.__str__())
            cont_new = CCP4Container.CContainer(name=taskName)
            cont_new.__dict__['header']=CCP4File.CI2XmlHeader()
            cont_new.__dict__['header'].pluginName = taskName
            cont_new.addParamsSubContainers()
            cont_new.inputData.addObject( cont.outputData.XYZOUT, reparent=True )
            cont_new.inputData.renameObject('XYZOUT','XYZIN_HA')
            cont_new.inputData.addObject( cont.inputData.SAD, reparent=True )
            cont_new.inputData.renameObject('SAD','F_SIGF')
            cont_new.inputData.addContent( name='LLGC_CYCLES', cls=CCP4Data.CInt, value=10 )
            cont_new.inputData.addContent( name='ELEMENTS', cls=CCP4Data.CList, subItem={'class':CCP4Data.CString} )
            for element in pdbDataFile.fileContent.composition.elements:
                upperCase = element.__str__().upper()
                strippedElement = ''
                i=0
                while i<2 and upperCase[i] in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
                    strippedElement += upperCase[i]
                    i+= 1
                cont_new.inputData.ELEMENTS.addItem(value=strippedElement)
            cont_new.addObject(CCP4Container.CContainer(parent=cont_new,name='keywords'),name='keywords')
            cont_new.keywords.addContent( name='HAND', cls=CCP4Data.CString, value='off' )
            cont_new.saveDataToXml(fileName=paramsPath)
        returnList.append([taskName, paramsPath])
    except:
        print 'Exception in ShelxCECompareHands_gui.whatNext'

    return returnList


#-------------------------------------------------------------------
class ShelxCECompareHands_gui(ShelxCE_gui.ShelxCE_gui):
    #-------------------------------------------------------------------
    
    # Subclass CTaskWidget to give specific task window
    TASKMODULE = 'test'                               # Where this plugin will appear on the gui
    TASKTITLE = 'ShelxE Compare hands pipeline'     # A short title for gui menu
    SHORTTASKTITLE = 'Compare HA hands - ShelxE'     # A short title for gui menu
    DESCRIPTION = 'Phasing, density modification and autobuilding - both hands of the HA structure'
    TASKVERSION = 0.1
    TASKNAME = 'ShelxCECompareHands'                                  # Task name - should be same as class name
    MGDISPLAYFILES = ['XYZOUT']
    
    def __init__(self,*args,**kw):
        super(ShelxCECompareHands_gui, self).__init__(*args, **kw)
    
    def ShelxCEKeywords(self):
        #From here on, GUI is autogenerated from the keywords container
        self.openSubFrame( frame=[True] )
        self.autoGenerate(container=self.container.keywords,selection={'includeParameters' : ['h','z','sX','aN','n','q','mN']})
        self.closeSubFrame()

