from PyQt6 import QtCore, QtWidgets, QtGui

class Properties(QtWidgets.QWidget):
    CHART_FONTS_CHANGED = QtCore.pyqtSignal(str,int)
    CANVAS_FONTS_CHANGED = QtCore.pyqtSignal(str,int)

    def __init__(self,parent,config):
        super(Properties,self).__init__(parent)

        #Defaults
        propertiesDefault = dict(config['propertiesDefault'].items())
        self.sensitivity = float(propertiesDefault['sensitivity'])
        self.electronEnergy = int(propertiesDefault['electronenergy'])
        self.azimuth = int(propertiesDefault['azimuth'])
        self.scaleBarLength = int(propertiesDefault['scalebarlength'])
        self.brightness = int(propertiesDefault['brightness'])
        self.brightnessMinimum = int(propertiesDefault['brightnessminimum'])
        self.brightnessMaximum = int(propertiesDefault['brightnessmaximum'])
        self.blackLevel = int(propertiesDefault['blacklevel'])
        self.blackLevelMinimum = int(propertiesDefault['blacklevelminimum'])
        self.blackLevelMaximum = int(propertiesDefault['blacklevelmaximum'])
        self.integralHalfWidth = float(propertiesDefault['integralhalfwidth'])
        self.widthMinimum = int(propertiesDefault['widthminimum'])
        self.widthMaximum = int(propertiesDefault['widthmaximum'])
        self.widthSliderScale = int(propertiesDefault['widthsliderscale'])
        self.chiRange = int(propertiesDefault['chirange'])
        self.chiRangeMinimum = int(propertiesDefault['chirangeminimum'])
        self.chiRangeMaximum = int(propertiesDefault['chirangemaximum'])
        self.radius = int(propertiesDefault['radius'])
        self.radiusMinimum = int(propertiesDefault['radiusminimum'])
        self.radiusMaximum = int(propertiesDefault['radiusmaximum'])
        self.radiusSliderScale = int(propertiesDefault['radiussliderscale'])
        self.tiltAngle = int(propertiesDefault['tiltangle'])
        self.tiltAngleMinimum = int(propertiesDefault['tiltangleminimum'])
        self.tiltAngleMaximum = int(propertiesDefault['tiltanglemaximum'])
        self.tiltAngleSliderScale = int(propertiesDefault['tiltanglesliderscale'])

        self.init_UI()

    def refresh(self,config):
        propertiesDefault = dict(config['propertiesDefault'].items())
        self.sensitivity = float(propertiesDefault['sensitivity'])
        self.electronEnergy = int(propertiesDefault['electronenergy'])
        self.azimuth = int(propertiesDefault['azimuth'])
        self.scaleBarLength = int(propertiesDefault['scalebarlength'])
        self.brightness = int(propertiesDefault['brightness'])
        self.brightnessMinimum = int(propertiesDefault['brightnessminimum'])
        self.brightnessMaximum = int(propertiesDefault['brightnessmaximum'])
        self.blackLevel = int(propertiesDefault['blacklevel'])
        self.blackLevelMinimum = int(propertiesDefault['blacklevelminimum'])
        self.blackLevelMaximum = int(propertiesDefault['blacklevelmaximum'])
        self.integralHalfWidth = float(propertiesDefault['integralhalfwidth'])
        self.widthMinimum = int(propertiesDefault['widthminimum'])
        self.widthMaximum = int(propertiesDefault['widthmaximum'])
        self.widthSliderScale = int(propertiesDefault['widthsliderscale'])
        self.chiRange = int(propertiesDefault['chirange'])
        self.chiRangeMinimum = int(propertiesDefault['chirangeminimum'])
        self.chiRangeMaximum = int(propertiesDefault['chirangemaximum'])
        self.radius = int(propertiesDefault['radius'])
        self.radiusMinimum = int(propertiesDefault['radiusminimum'])
        self.radiusMaximum = int(propertiesDefault['radiusmaximum'])
        self.radiusSliderScale = int(propertiesDefault['radiussliderscale'])
        self.tiltAngle = int(propertiesDefault['tiltangle'])
        self.tiltAngleMinimum = int(propertiesDefault['tiltangleminimum'])
        self.tiltAngleMaximum = int(propertiesDefault['tiltanglemaximum'])
        self.tiltAngleSliderScale = int(propertiesDefault['tiltanglesliderscale'])

        #Parameters page
        self.sensitivityEdit.setText(str(self.sensitivity))
        self.energyEdit.setText(str(self.electronEnergy))
        self.azimuthEdit.setText(str(self.azimuth))
        self.scaleBarEdit.setText(str(self.scaleBarLength))

        #image adjust page
        self.brightnessSlider.setMinimum(self.brightnessMinimum)
        self.brightnessSlider.setMaximum(self.brightnessMaximum)
        self.brightnessSlider.setValue(self.brightness)
        self.blackLevelSlider.setMinimum(self.blackLevelMinimum)
        self.blackLevelSlider.setMaximum(self.blackLevelMaximum)
        self.blackLevelSlider.setValue(self.blackLevel)

        #profile options page
        self.integralHalfWidthSlider.setMinimum(int(self.widthMinimum*self.widthSliderScale))
        self.integralHalfWidthSlider.setMaximum(int(self.widthMaximum*self.widthSliderScale))
        self.integralHalfWidthSlider.setValue(int(self.integralHalfWidth*self.widthSliderScale))
        self.chiRangeSlider.setMinimum(self.chiRangeMinimum)
        self.chiRangeSlider.setMaximum(self.chiRangeMaximum)
        self.chiRangeSlider.setValue(self.chiRange)
        self.radiusSlider.setMinimum(int(self.radiusMinimum*self.radiusSliderScale))
        self.radiusSlider.setMaximum(int(self.radiusMaximum*self.radiusSliderScale))
        self.radiusSlider.setValue(int(self.radius*self.radiusSliderScale))
        self.tiltAngleSlider.setMinimum(int(self.tiltAngleMinimum*self.tiltAngleSliderScale))
        self.tiltAngleSlider.setMaximum(int(self.tiltAngleMaximum*self.tiltAngleSliderScale))
        self.tiltAngleSlider.setValue(int(self.tiltAngle*self.tiltAngleSliderScale))

    def init_UI(self):
        self.tab = QtWidgets.QTabWidget()
        #Parameters page
        self.parametersPage = QtWidgets.QWidget()
        self.parametersGrid = QtWidgets.QGridLayout(self.parametersPage)
        self.sensitivityLabel = QtWidgets.QLabel('Sensitivity (pixel/sqrt[keV])')
        self.sensitivityEdit = QtWidgets.QLineEdit(str(self.sensitivity))
        self.energyLabel = QtWidgets.QLabel('Electron Energy (keV)')
        self.energyEdit = QtWidgets.QLineEdit(str(self.electronEnergy))
        self.azimuthLabel = QtWidgets.QLabel('Azimuth(\u00B0)')
        self.azimuthEdit = QtWidgets.QLineEdit(str(self.azimuth))
        self.scaleBarLabel = QtWidgets.QLabel('Scale Bar Length (\u212B\u207B\u00B9)')
        self.scaleBarEdit = QtWidgets.QLineEdit(str(self.scaleBarLength))
        self.labelButton = QtWidgets.QPushButton('Label')
        self.calibrateButton = QtWidgets.QPushButton('Calibrate')
        self.clearButton = QtWidgets.QPushButton('Clear')
        self.labelButton.setSizePolicy(QtWidgets.QSizePolicy.Policy.Fixed,QtWidgets.QSizePolicy.Policy.Fixed)
        self.calibrateButton.setSizePolicy(QtWidgets.QSizePolicy.Policy.Fixed,QtWidgets.QSizePolicy.Policy.Fixed)
        self.clearButton.setSizePolicy(QtWidgets.QSizePolicy.Policy.Fixed,QtWidgets.QSizePolicy.Policy.Fixed)
        self.buttonGrid1 = QtWidgets.QGridLayout()
        self.buttonGrid1.addWidget(self.labelButton,0,0)
        self.buttonGrid1.addWidget(self.calibrateButton,0,1)
        self.buttonGrid1.addWidget(self.clearButton,0,2)

        self.parametersGrid.addWidget(self.sensitivityLabel,0,0)
        self.parametersGrid.addWidget(self.sensitivityEdit,0,1)
        self.parametersGrid.addWidget(self.energyLabel,1,0)
        self.parametersGrid.addWidget(self.energyEdit,1,1)
        self.parametersGrid.addWidget(self.azimuthLabel,2,0)
        self.parametersGrid.addWidget(self.azimuthEdit,2,1)
        self.parametersGrid.addWidget(self.scaleBarLabel,3,0)
        self.parametersGrid.addWidget(self.scaleBarEdit,3,1)
        self.parametersGrid.addLayout(self.buttonGrid1,4,0,1,2)
        self.tab.addTab(self.parametersPage,"Parameters")

        #image adjust page
        self.imageAdjustPage = QtWidgets.QWidget()
        self.imageAdjustGrid = QtWidgets.QGridLayout(self.imageAdjustPage)
        self.brightnessLabel = QtWidgets.QLabel('Brightness ({})'.format(self.brightness))
        self.brightnessSlider= QtWidgets.QSlider(QtCore.Qt.Orientation.Horizontal)
        self.brightnessSlider.setMinimum(self.brightnessMinimum)
        self.brightnessSlider.setMaximum(self.brightnessMaximum)
        self.brightnessSlider.setValue(self.brightness)
        self.blackLevelLabel = QtWidgets.QLabel('Black Level ({})'.format(self.blackLevel))
        self.blackLevelSlider= QtWidgets.QSlider(QtCore.Qt.Orientation.Horizontal)
        self.blackLevelSlider.setMinimum(self.blackLevelMinimum)
        self.blackLevelSlider.setMaximum(self.blackLevelMaximum)
        self.blackLevelSlider.setValue(self.blackLevel)
        self.autoWBLabel = QtWidgets.QLabel('Auto white balance')
        self.autoWBCheckBox = QtWidgets.QCheckBox()

        self.applyButton2 = QtWidgets.QPushButton('Apply')
        self.resetButton2 = QtWidgets.QPushButton('Reset')
        self.applyButton2.setSizePolicy(QtWidgets.QSizePolicy.Policy.Fixed,QtWidgets.QSizePolicy.Policy.Fixed)
        self.resetButton2.setSizePolicy(QtWidgets.QSizePolicy.Policy.Fixed,QtWidgets.QSizePolicy.Policy.Fixed)
        self.buttonGrid2 = QtWidgets.QGridLayout()
        self.buttonGrid2.addWidget(self.applyButton2,0,0)
        self.buttonGrid2.addWidget(self.resetButton2,0,1)

        self.imageAdjustGrid.addWidget(self.brightnessLabel,0,0)
        self.imageAdjustGrid.addWidget(self.brightnessSlider,0,1)
        self.imageAdjustGrid.addWidget(self.blackLevelLabel,1,0)
        self.imageAdjustGrid.addWidget(self.blackLevelSlider,1,1)
        self.imageAdjustGrid.addWidget(self.autoWBLabel,2,0)
        self.imageAdjustGrid.addWidget(self.autoWBCheckBox,2,1)
        self.imageAdjustGrid.addLayout(self.buttonGrid2,3,0,1,2)
        self.tab.addTab(self.imageAdjustPage,"Adjust Image")

        #profile options page
        self.profileOptionsPage = QtWidgets.QWidget()
        self.profileOptionsGrid = QtWidgets.QGridLayout(self.profileOptionsPage)
        self.integralHalfWidthLabel = QtWidgets.QLabel('Integral Half Width ({:3.2f} \u212B\u207B\u00B9)'.format(self.integralHalfWidth))
        self.integralHalfWidthSlider = QtWidgets.QSlider(QtCore.Qt.Orientation.Horizontal)
        self.integralHalfWidthSlider.setMinimum(int(self.widthMinimum*self.widthSliderScale))
        self.integralHalfWidthSlider.setMaximum(int(self.widthMaximum*self.widthSliderScale))
        self.integralHalfWidthSlider.setValue(int(self.integralHalfWidth*self.widthSliderScale))
        self.chiRangeLabel = QtWidgets.QLabel('Chi Range ({}\u00B0)'.format(self.chiRange))
        self.chiRangeSlider = QtWidgets.QSlider(QtCore.Qt.Orientation.Horizontal)
        self.chiRangeSlider.setMinimum(self.chiRangeMinimum)
        self.chiRangeSlider.setMaximum(self.chiRangeMaximum)
        self.chiRangeSlider.setValue(self.chiRange)
        self.radiusLabel = QtWidgets.QLabel('Radius ({:3.2f} \u212B\u207B\u00B9)'.format(self.radius))
        self.radiusSlider = QtWidgets.QSlider(QtCore.Qt.Orientation.Horizontal)
        self.radiusSlider.setMinimum(int(self.radiusMinimum*self.radiusSliderScale))
        self.radiusSlider.setMaximum(int(self.radiusMaximum*self.radiusSliderScale))
        self.radiusSlider.setValue(int(self.radius*self.radiusSliderScale))
        self.tiltAngleLabel = QtWidgets.QLabel('Tilt Angle ({:2.1f}\u00B0)'.format(self.tiltAngle))
        self.tiltAngleSlider = QtWidgets.QSlider(QtCore.Qt.Orientation.Horizontal)
        self.tiltAngleSlider.setMinimum(int(self.tiltAngleMinimum*self.tiltAngleSliderScale))
        self.tiltAngleSlider.setMaximum(int(self.tiltAngleMaximum*self.tiltAngleSliderScale))
        self.tiltAngleSlider.setValue(int(self.tiltAngle*self.tiltAngleSliderScale))

        self.applyButton3 = QtWidgets.QPushButton('Apply')
        self.resetButton3= QtWidgets.QPushButton('Reset')
        self.applyButton3.setSizePolicy(QtWidgets.QSizePolicy.Policy.Fixed,QtWidgets.QSizePolicy.Policy.Fixed)
        self.resetButton3.setSizePolicy(QtWidgets.QSizePolicy.Policy.Fixed,QtWidgets.QSizePolicy.Policy.Fixed)
        self.buttonGrid3 = QtWidgets.QGridLayout()
        self.buttonGrid3.addWidget(self.applyButton3,0,0)
        self.buttonGrid3.addWidget(self.resetButton3,0,1)

        self.profileOptionsGrid.addWidget(self.integralHalfWidthLabel,0,0)
        self.profileOptionsGrid.addWidget(self.integralHalfWidthSlider,0,1)
        self.profileOptionsGrid.addWidget(self.chiRangeLabel,1,0)
        self.profileOptionsGrid.addWidget(self.chiRangeSlider,1,1)
        self.profileOptionsGrid.addWidget(self.radiusLabel,2,0)
        self.profileOptionsGrid.addWidget(self.radiusSlider,2,1)
        self.profileOptionsGrid.addWidget(self.tiltAngleLabel,3,0)
        self.profileOptionsGrid.addWidget(self.tiltAngleSlider,3,1)
        self.profileOptionsGrid.addLayout(self.buttonGrid3,4,0,1,2)
        self.tab.addTab(self.profileOptionsPage,"Profile Options")

        #Appearance Page
        self.appearance = QtWidgets.QWidget()
        #self.appearance.setMaximumHeight(100)
        self.appearanceGrid = QtWidgets.QGridLayout(self.appearance)
        self.fontListLabel = QtWidgets.QLabel("Choose Font Family")
        self.fontList = QtWidgets.QFontComboBox()
        self.fontList.setCurrentFont(QtGui.QFont("Arial"))
        self.fontList.currentFontChanged.connect(self.refresh_font_name)
        self.chartFontSizeLabel = QtWidgets.QLabel("Adjust Chart Font Size ({})".format(12))
        #self.chartFontSizeLabel.setFixedWidth(160)
        self.chartFontSizeSlider = QtWidgets.QSlider(QtCore.Qt.Orientation.Horizontal)
        self.chartFontSizeSlider.setMinimum(1)
        self.chartFontSizeSlider.setMaximum(100)
        self.chartFontSizeSlider.setValue(12)
        self.chartFontSizeSlider.valueChanged.connect(self.refresh_chart_font_size)
        self.canvasFontSizeLabel = QtWidgets.QLabel("Adjust Canvas Font Size ({})".format(50))
        #self.canvasFontSizeLabel.setFixedWidth(160)
        self.canvasFontSizeSlider = QtWidgets.QSlider(QtCore.Qt.Orientation.Horizontal)
        self.canvasFontSizeSlider.setMinimum(1)
        self.canvasFontSizeSlider.setMaximum(100)
        self.canvasFontSizeSlider.setValue(50)
        self.canvasFontSizeSlider.valueChanged.connect(self.refresh_canvas_font_size)
        self.appearanceGrid.addWidget(self.fontListLabel,0,0)
        self.appearanceGrid.addWidget(self.fontList,0,1)
        self.appearanceGrid.addWidget(self.chartFontSizeLabel,1,0)
        self.appearanceGrid.addWidget(self.chartFontSizeSlider,1,1)
        self.appearanceGrid.addWidget(self.canvasFontSizeLabel,2,0)
        self.appearanceGrid.addWidget(self.canvasFontSizeSlider,2,1)
        self.tab.addTab(self.appearance,"Adjust Font")
        self.tab.setSizePolicy(QtWidgets.QSizePolicy.Policy.Expanding,QtWidgets.QSizePolicy.Policy.Fixed)

        self.UIgrid = QtWidgets.QGridLayout()
        self.UIgrid.addWidget(self.tab,0,0)
        self.UIgrid.setContentsMargins(0,0,0,0)
        self.setLayout(self.UIgrid)
        self.show()

    def refresh_chart_font_size(self):
        self.chartFontSizeLabel.setText("Adjust Chart Font Size ({})".format(self.chartFontSizeSlider.value()))
        self.CHART_FONTS_CHANGED.emit(self.fontList.currentFont().family(),self.chartFontSizeSlider.value())

    def refresh_canvas_font_size(self):
        self.canvasFontSizeLabel.setText("Adjust Canvas Font Size ({})".format(self.canvasFontSizeSlider.value()))
        self.CANVAS_FONTS_CHANGED.emit(self.fontList.currentFont().family(),self.canvasFontSizeSlider.value())

    def refresh_font_name(self):
        self.CHART_FONTS_CHANGED.emit(self.fontList.currentFont().family(),self.chartFontSizeSlider.value())
        self.CANVAS_FONTS_CHANGED.emit(self.fontList.currentFont().family(),self.canvasFontSizeSlider.value())
