import re

from PyQt5.QtWidgets import  QWidget, QVBoxLayout, QLabel, QPushButton, \
    QHBoxLayout, QMessageBox, QSplitter, QRadioButton

from UI.SoftKeyBoardEdit import SoftKeyBoardEdit

class ForceButton(QPushButton):
    def __init__(self, number = None, valueLabel = None, valueEdit = None):
        super().__init__()
        self.valueEdit = valueEdit
        self.valueLabel = valueLabel
        self.number = number
        self.setObjectName('forceBtn')
        self.setText('强制')

class ForceRadioButton(QRadioButton):
    def __init__(self, number = None):
        super().__init__()
        self.number = number

class RightAreaWidgets(QWidget):
    def __init__(self, areaWidget, order, byteLineEdit, dataType, deviceName):
        super().__init__()
        self.areaLineEditValue = []
        self.areaLabel = {}
        self.areaWidget = areaWidget
        self.devicesManange = self.areaWidget.devicesManange
        self.order = order
        self.byteLineEdit = byteLineEdit
        self.dataType = dataType
        self.deviceName = deviceName
        if '主站' in self.deviceName and self.dataType == 'AI' or ('从站' in self.deviceName and self.dataType == 'AO'):
            self.force = False
        elif '主站' in self.deviceName and self.dataType == 'DI' or ('从站' in self.deviceName and self.dataType == 'DO'):
            self.force = False
        else:
            self.force = True
        self.addLayout()

        # self.setStyleSheet("background-color: black")
     
     
    
    def addLayout(self):
        self.setLayout(self.areaLayout())
   

    def areaLayout(self):
        areaLayout = QVBoxLayout()
        areaLayout.setContentsMargins(0, 0, 0, 0)
        if self.dataType in ['AI','AO']:
            hLayout = QHBoxLayout()
            hLayout.addWidget(QSplitter())
            hLayout.addLayout(self.dateLayout(0))
            hLayout.addWidget(QSplitter())
            areaLayout.addWidget(QSplitter())
            areaLayout.addLayout(hLayout)
            areaLayout.addWidget(QSplitter())
        else:
            byteLineEdit = int(self.byteLineEdit) * 4
            areaLayout.addWidget(QSplitter())
            for i in range(int(byteLineEdit)):
                hLayout = QHBoxLayout()
                hLayout.addWidget(QSplitter(), 1)
                hLayout.addLayout(self.dateLayout(number = (i), isAnalog = False), 3) 
                hLayout.addWidget(QSplitter(), 1)
                hLayout.addLayout(self.dateLayout(number = (i) + 8, isAnalog = False), 3)
                hLayout.addWidget(QSplitter(), 1)
                areaLayout.addLayout(hLayout)
            areaLayout.addWidget(QSplitter())
     
        return areaLayout
    
    def dateLayout(self, number, isAnalog = True):
        if not isAnalog:
            forceLayout = QHBoxLayout()
            valueRadio = ForceRadioButton(number = number)
            valueRadio.setObjectName('valueRadio')
            valueRadio.setAutoExclusive(False)
            valueRadio.setText('OFF')
            valueRadio.toggled.connect(self.DIDOForceValues)
            massesageLabel = QLabel('通道 {} '.format(str(number + 1)))
            massesageLabel.setObjectName('massesageLabel')
            forceLayout.addWidget(massesageLabel)
            forceLayout.addWidget(valueRadio)
            forceLayout.setContentsMargins(0, 0, 0, 0)
            forceLayout.setSpacing(0)
            self.areaLabel[number] = valueRadio
            if not self.force:
                valueRadio.setEnabled(False)
        else:
            forceLayout = QHBoxLayout()
            forceLayout.setContentsMargins(0, 0, 0, 0)
            areaMessLabel = QLabel('当前值:' )
            areaMessLabel.setObjectName('areaMessLabel')
            areaValueLabel = QLabel('0')
            areaValueLabel.setObjectName('areaValueLabel')
            forceLayout.addWidget(areaMessLabel)
            forceLayout.addWidget(areaValueLabel)
       
            if self.force:
                areaLineEdit = SoftKeyBoardEdit('0')
                areaLineEdit.setObjectName('areaLineEdit')
                areaLineEdit.setFixedSize(80, 33)
                forceBtn = ForceButton(number = number, valueLabel=areaValueLabel, valueEdit=areaLineEdit)
                forceBtn.clicked.connect(self.AIAOForceValues)
                # forceBtn.setFixedSize(50, 27)
                forceLayout.addWidget(areaLineEdit)
                forceLayout.addWidget(forceBtn)
            else:
                self.areaLabel[number] = areaValueLabel

        self.areaLineEditValue.append(0)
        
            #     if self.deviceName[-2:] + self.dataType  in ['从站DI','主站DO']:
            #         areaMessLabel.setObjectName('wirteDIDOareaMessLabel')
            #         areaValueLabel.setObjectName('wirteDIDOareaValueLabel')
            #         areaLineEdit.setObjectName('wirteDIDOareaLineEdit')
            #         areaLineEdit.setFixedSize(30, 27)
            #         areaLineEdit.setAlignment(Qt.AlignHCenter)
            #         forceBtn.setObjectName('wirteDIDOforceBtn')
            
            # if self.deviceName[-2:] + self.dataType  in ['主站DI','从站DO']:
            #     areaMessLabel.setObjectName('readDIDOareaMessLabel')
            #     areaValueLabel.setObjectName('readDIDOareaValueLabel')
            

        


        # forceLayout.setSpacing(10)
        return forceLayout

    def wirteAreaLineEditValue(self,dataTypeAndModel, number, value, valueLabel = None):
        if self.areaWidget.okBtnValue:
            QMessageBox.warning(self, '提示', '请先保存通道信息')
            return
 
        if dataTypeAndModel in ['主站AO','从站AI']:
            pattern = re.compile(r'-?\d*\.?\d+')
            match = pattern.match(value)
            if  not match:
                QMessageBox.warning(self, '提示', '请输入数字。')
                return
            else:
                valueLabel.setText(str(float(value)))
                self.areaLineEditValue[number] = float(value)
     
        #判断输入值是否为0和1
        elif dataTypeAndModel in ['主站DO','从站DI']:
            
            # pattern = re.compile(r'^[01]$')
            # match = pattern.match(value)
            # if  not match:
            #     QMessageBox.warning(self, '提示', '请输入0或1。')
            #     return
            # else:
            #     valueLabel.setText(value)
            self.areaLineEditValue[number] = value
                
        return self.areaLineEditValue

    
    def readValues(self, curIndex):
        # print(curIndex)
        if not self.force:
            device = self.devicesManange.getDevice(self.deviceName)
            values = device.getAreaValues(curIndex)
            # print(self.deviceName, curIndex, values)
            if len(values) > len(self.areaLabel):
                return
            for index, value in enumerate(values):
                # print(self.dataType, self.force)
                if self.dataType in ['DI', 'DO'] and not self.force:
                    if value == 1:
                        value = 'ON'
                        self.areaLabel[index].setChecked(True)
                    else:
                        value = 'OFF'
                        self.areaLabel[index].setChecked(False)
                    self.areaLabel[index].setText(value)
                    continue
                self.areaLabel[index].setText(str(value))
                # print(self.areaLabel[index],values)
                    
      

    def AIAOForceValues(self):
        sender = self.sender()
        number = sender.number
        valueLabel = sender.valueLabel
        value = sender.valueEdit.text()
        dataTypeAndModel = self.deviceName[-2:] + self.dataType
        curIndex = self.areaWidget.areaTabWidget.currentIndex()
        valueList = self.wirteAreaLineEditValue(dataTypeAndModel=dataTypeAndModel, valueLabel=valueLabel,number=number, value=value)
        # print(valueList, curIndex)
        if valueList is None:
            return
        else:
            self.devicesManange.writeAreas(deviceName = self.deviceName, areaIndex = curIndex, values = valueList)

    
    def DIDOForceValues(self):
        sender = self.sender()
        number = sender.number
        value = 1 if sender.isChecked() else 0
        dataTypeAndModel = self.deviceName[-2:] + self.dataType
        curIndex = self.areaWidget.areaTabWidget.currentIndex()
        valueList = self.wirteAreaLineEditValue(dataTypeAndModel=dataTypeAndModel, number=number, value=value)
        if valueList is None:
            return
        else:
            self.devicesManange.writeAreas(deviceName = self.deviceName, areaIndex = curIndex, values = valueList)
        if sender.isChecked():
            sender.setText('ON')
        else:
            sender.setText('OFF')