#
#  Copyright (C) 2016 STFC Rutherford Appleton Laboratory, UK.
#
#  Author: David Waterman
#  Acknowledgements: based on code by Graeme Winter and Martin Noble.
#

from PyQt4 import QtGui,QtCore

from CCP4TaskWidget import CTaskWidget
import CCP4Container

class xia2_dials_gui(CTaskWidget):

    # Subclass CTaskWidget to give specific task window
    TASKTITLE='Automated integration of images with DIALS using xia2'
    DESCRIPTION='Select a directory containing images and integrate them'
    TASKNAME = 'xia2_dials'
    TASKMODULE = 'data_processing'
    TASKVERSION = 0.0
    RANK=1
    SHORTTASKTITLE = 'xia2/dials'
    TASKVERSION = 0.1

    def drawContents(self):

        # Input data
        self.openFolder(folderFunction='inputData')
        self.createLine(['tip','xia2 will attempt to work out how to process '
            'all of the images in the specified directory','subtitle',
            'Location of directory containing images'])
        self.openSubFrame(frame=True)
        self.createLine(['widget','IMAGE_DIRECTORY'])
        self.closeSubFrame()

        # Basic parameters
        self.createLine(['tip','Subset of xia2 control parameters','subtitle',
            'Basic parameters'])
        self.nestedAutoGenerate(container=self.container.controlParameters,
            expertLevel=['0'])

        # Advanced parameters in a separate folder
        folder = self.openFolder(folderFunction='controlParameters',
            title='Advanced parameters',drawFolder=self.drawAdvanced)

        # testing standard autogenerate
        #folder = self.openFolder(folderFunction='controlParameters', title='TEST')
        #self.autoGenerate(container=self.container.controlParameters,selection={'excludeParameters' : []})
        #self.closeFolder()

    def drawAdvanced(self):
      self.nestedAutoGenerate(container=self.container.controlParameters,
            expertLevel=['0', '1'])

    def nestedAutoGenerate(self, container, expertLevel=['0']):
        """Autogenerate a GUI from nested containers using only elements at
        the specified expertLevels"""

        dataOrder = container.dataOrder()
        contents = [getattr(container,name) for name in dataOrder]

        # Filter by expertLevel
        expert = [c.qualifiers('guiDefinition').get('expertLevel', '0') \
                  for c in contents]
        dataOrder = [d for d, e in zip(dataOrder, expert) if e in expertLevel]
        contents  = [c for c, e in zip(contents, expert) if e in expertLevel]

        # If this container has any content other than nested containers,
        # autogenerate that content
        data = [name for name, model in zip(dataOrder, contents) \
                if not isinstance(model, CCP4Container.CContainer)]
        subcontainers = [model for model in contents \
                         if isinstance(model, CCP4Container.CContainer)]
        if data:
            # only open a sub-frame if there is a label available
            make_subframe = container.qualifiers('guiLabel') is not NotImplemented
            if make_subframe: self.openSubFrame(frame=True)
            self.autoGenerate(container, selection={'includeParameters':data})
            for c in subcontainers:
                self.nestedAutoGenerate(c, expertLevel=expertLevel)
            if make_subframe: self.closeSubFrame()
        else:
            # just drill down without opening a sub frame
            for c in subcontainers: self.nestedAutoGenerate(c,
                expertLevel=expertLevel)
        return
