'''
pubs UI classes 
'''

from pubs.ui import *

#pubs modules
from pubs.ui import widgets, models
import pubs.pGraph as pGraph
import pubs.ui.centralWidget as centralWidget
#-------------------------------
#MAIN WINDOW
#-------------------------------
class MainWindow(QtWidgets.QMainWindow):
    __initialized = None
    __instance = None
    def __new__(cls,*args,**kwargs):
        '''
        Creates a new instance of this object.
        '''
        if not MainWindow.__instance:
            MainWindow.__instance = QtWidgets.QMainWindow.__new__(cls)

        return MainWindow.__instance

    def __del__(self):
        '''
        Destructor method. Restting the class attributes and destroying thte window.
        '''
        MainWindow.__initialized = None
        MainWindow.__instance = None

    def __init__(self, graph = pGraph.PGraph('null'), parent = None):
        '''
        This is the main window used for pubs

        :param graph: The graph which the ui will use for the setup view
        :type graph: ml_graph.MlGraph
        
        :param parent: The parent for the ui
        :type parent: QtGui.QWidget
        '''
        
        if not MainWindow.__initialized:
            super(MainWindow, self).__init__(parent)
            MainWindow.__initialized = True
            #load in the style sheet
            
            #set the window title and central widget
            self.setWindowTitle('PUBS UI')
            tabWidget = centralWidget.CentralTabWidget(graph)
            self.setCentralWidget(tabWidget)
            
            #add menu bar to window
            menuBar = self.menuBar()
            fileMenu = menuBar.addMenu('File')
            #newTemplateAction = fileMenu.addAction('New Template')
            #loadTemplateAction = fileMenu.addAction('Load Template')
            #saveTemplateAction = fileMenu.addAction('Save Template')
            #QtCore.QObject.connect(newTemplateAction, QtCore.SIGNAL('triggered()'), self._newTemplate)
            #QtCore.QObject.connect(loadTemplateAction, QtCore.SIGNAL('triggered()'), self._loadTemplate)
            #QtCore.QObject.connect(saveTemplateAction, QtCore.SIGNAL('triggered()'), self._saveTemplate)
            
            self.setMinimumSize(880,550)
            self.resize(1200, 1200)
           
    @staticmethod
    def getInstance():
        '''
        '''
        return MainWindow.__instance

        super(MainWindow, self).__init__(parent)
        #load in the style sheet
        
        #set the window title and central widget
        self.setWindowTitle('PUBS UI')
        tabWidget = centralWidget.CentralTabWidget(graph)
        self.setCentralWidget(tabWidget)
        
        #add menu bar to window
        menuBar = self.menuBar()
        fileMenu = menuBar.addMenu('File')
        newTemplateAction = fileMenu.addAction('New Template')
        loadTemplateAction = fileMenu.addAction('Load Template')
        saveTemplateAction = fileMenu.addAction('Save Template')
        QtCore.QObject.connect(newTemplateAction, QtCore.SIGNAL('triggered()'), self._newTemplate)
        QtCore.QObject.connect(loadTemplateAction, QtCore.SIGNAL('triggered()'), self._loadTemplate)
        #QtCore.QObject.connect(saveTemplateAction, QtCore.SIGNAL('triggered()'), self._saveTemplate)
        
        self.setMinimumSize(880,550)
        self.setMaximumSize(1000, 1200)

    """
    def _loadTemplate(self):
        '''
        Load all the templates into the dialog
        '''
        self.loadTemplateDialog = widgets.LoadTemplateDialog(self)
        self.loadTemplateDialog.show()

        self.loadTemplateDialog.finished.connect(self.setTemplate)
    
    def _newTemplate(self):
        '''
        Load all the templates into the dialog
        '''
        self.inheritTemplateDialog = widgets.InheritTemplateDialog(self)
        self.inheritTemplateDialog.show()

        self.inheritTemplateDialog.finished.connect(self.setNewTemplate)


    def setTemplate(self, *args):
        '''
        sets the template to the template chosen in the templateDialog
        '''
        #get the template chosen
        template = str(self.loadTemplateDialog.template)
        templateFile = self.loadTemplateDialog.templateFile
        #import the file and initialize
        if template != 'None':
            tabWidget = self.centralWidget()
            #get data from the file
            data = dict()
            #run the data we got from file       
            execfile(templateFile, data)
            #newNodeCmd = '%s("%s")' % (template.title(), template) #<-- Construct template
            tabWidget._graph = data[template.title()](template)
            del(data) #<-- delete globals data
            tabWidget._graph.initialize()
            tabWidget._model = models.LayerGraphModel(tabWidget._graph)
            #tabWidget._proxyModel.setSourceModel(tabWidget._model)
            tabWidget._setupTreeView.setModel(tabWidget._model)
            tabWidget._setupTreeView.expandAll()
    """
            
    def closeEvent(self, e):
        # Write window size and position to config file
        #self.settings.setValue("size", self.size())
        #self.settings.setValue("pos", self.pos())
        super(MainWindow,self).closeEvent(e)

def launch(dock = False, graph=pGraph.PGraph('nullGraph')):
    parent=None
    if getMayaWindow:
        parent = getMayaWindow()
    wnd = MainWindow(parent=parent, graph=graph)
    wnd.show()
    wnd.raise_()
    return MainWindow.getInstance()

