from PyQt5 import QtGui, QtCore,QtWidgets,uic

#from pySAXS.guisaxs.qt import dlgConcatenateui
from pySAXS.tools import isNumeric
from pySAXS.guisaxs import dataset
from pySAXS.guisaxs.qt import preferences
import numpy
import pySAXS


class dlgNewMerge(QtWidgets.QDialog):#,dlgConcatenateui.Ui_concatenateDialog):
    def __init__(self,parentwindow,newdatasetname=''):
        QtWidgets.QDialog.__init__(self)
        self.ui = uic.loadUi(pySAXS.UI_PATH+"dlgNewMerge.ui", self)#
        self.parentwindow=parentwindow
        self.data_dict=parentwindow.data_dict
        self.newdatasetname=newdatasetname
        self.newdataset=None
        self.cut1 = 0.1
        self.cut2 = 0.2
        # -- get preferences
        #self.pref = preferences.prefs()
        self.pref=self.parentwindow.pref
        if self.pref.fileExist():
            self.pref.read()
            print( "ref file exist")
            c1=self.pref.get('cut1', section="merging")
            if c1 is not None:
                self.cut1 =c1
            c2 = self.pref.get('cut2', section="merging")
            if c2 is not None:
                self.cut2 =c2
        else:
            self.cut1=0.1
            self.cut2=0.2
        # Set up the user interface from Designer.
        #self.setupUi(self)
        #construct UI
        self.ConstructUI()
        #QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("clicked(QAbstractButton*)"), self.click)#connect buttons signal
        self.ui.buttonBox.clicked.connect(self.click)
        #QtCore.QObject.connect(self.tableWidget, QtCore.SIGNAL("itemActivated(QTableWidgetItem*)"), self.activated)#connect buttons signal
        
    def ConstructUI(self):
        self.ui.lineEditNewName.setText(self.newdatasetname)
        curves=dataset.getListOfChecked(self.data_dict)
        print(curves)
        if len(curves)>=2:
            self.ui.lineEditCurve1.setText(str(curves[0]))
            self.ui.lineEditCurve2.setText(str(curves[1]))
        self.ui.lineEditCut1.setText(str(self.cut1))
        self.ui.lineEditCut2.setText(str(self.cut2))

    def savePref(self):
        self.cut1=self.ui.lineEditCut1.text()
        self.cut2 = self.ui.lineEditCut2.text()
        self.pref.set('cut1', self.cut1, section="merging")
        self.pref.set('cut2', self.cut2, section="merging")
        self.pref.save()


    def click(self,obj=None):
        '''
        user clicked on the button box
        '''
        name=obj.text()
        #print name
        self.savePref()
        if name=="Close":
            self.close()
        elif name=="Apply":
            #compute
            self.newdataset=self.mergeDatas()
            #get new name
            self.newdatasetname=str(self.lineEditNewName.text())
            self.newdataset.name=self.newdatasetname
            #apply
            self.parentwindow.data_dict[self.newdatasetname]=self.newdataset
            self.parentwindow.redrawTheList()
            self.parentwindow.Replot()
            
        else:
            #Close
            self.close()
    

    def mergeDatas(self):
        '''
        create the new datas
        '''

        name0=self.ui.lineEditCurve1.text()
        name1 = self.ui.lineEditCurve2.text()
        dataset0 = self.data_dict[name0]
        dataset1 = self.data_dict[name1]

        self.cut1 = float(self.ui.lineEditCut1.text())
        self.cut2 = float(self.ui.lineEditCut2.text())

        d=dataset.dataset_merging(dataset0,dataset1,self.cut1,self.cut2)
        return d
    
