
from CCP4ReportParser import *
import sys
from lxml import etree
import math

class SubstituteLigand_report(Report):
    # Specify which gui task and/or pluginscript this applies to
    TASKNAME = 'SubstituteLigand'
    RUNNING = True

    def __init__(self, *args, **kws):
        Report.__init__(self, *args, **kws)
        if self.jobStatus is None or self.jobStatus.lower() is 'nooutput': return
        self.defaultReport()

    def defaultReport(self, parent=None):
        if parent is None: parent = self
        self.addDiv(style='clear:both;')
        
        #If there is POINTLESS tags in the XML, then the reflections have been through either aimless_pipe or
        #pointless_reindexToMatch
        reflectionNodes = self.xmlnode.xpath('//POINTLESS')
        if len(reflectionNodes) > 0:
            summaryFold = parent.addFold(label='Key reflection summary', brief='Reflections', initiallyOpen=True)
            pointlessNodes = self.xmlnode.xpath('//POINTLESS')
            if len(pointlessNodes) > 0:
                from pointless_report import pointless_report
                pointlessreport = pointless_report(pointlessNodes[-1])
                pointlessreport.keyText(summaryFold)
            aimlessNodes = self.xmlnode.xpath('//AIMLESS')
            if len(aimlessNodes) > 0:
                from aimless_report import aimless_report
                aimlessreport = aimless_report(aimlessNodes[-1])
                aimlessreport.keyText(summaryFold)
    
        pmaNodes = self.xmlnode.xpath('//PhaserMrResults')
        if len(pmaNodes) > 0:
            from phaser_MR_AUTO_report import phaser_MR_AUTO_report
            pmaNode = pmaNodes[0]
            phaser_MRAReport = phaser_MR_AUTO_report(xmlnode=pmaNode, jobStatus='nooutput')
            if len(self.xmlnode.xpath('//PhaserMrSolutions/Solutions')) > 0:
                compareSolutionsFold = parent.addFold(label='Phaser results',initiallyOpen=True)
                phaser_MRAReport.addResults(parent=compareSolutionsFold)

            #phaser_MRAReport.drawContent(jobStatus=self.jobStatus, parent=self)
    
        refmacNodes = self.xmlnode.xpath('//REFMAC')
        if len(refmacNodes) > 0:
            from refmac_report import refmac_report
            refmac_report = refmac_report(xmlnode=refmacNodes[0], jobStatus='nooutput', jobInfo=self.jobInfo)
            refmac_report.addSummary(parent=self, withTables=False)
            objectMap = {}
            #Use DICTOUT or DICTIN if they have been harvested to define monomer geometry in pictures
            if self.jobInfo['filenames'].get('DICTOUT', None) is not None:
                objectMap['DICT'] = 'DICTOUT'
            elif self.jobInfo['filenames'].get('DICTIN', None) is not None:
                objectMap['DICT'] = 'DICTIN'
            if not self.jobStatus.lower().count('running'):
                refmac_report.addRefinementPictures(parent=self, objectNameMap=objectMap, initiallyOpen=True)


