import imp
from operator import imod
import typing
import re
# from matplotlib.pyplot import box
import qtawesome
from PyQt5 import QtGui,QtCore,QtWidgets
from PyQt5.QtCore import QAbstractTableModel, QModelIndex, Qt, QVariant, QSize
from PyQt5.QtWidgets import QItemDelegate, QHBoxLayout, QWidget, QPushButton, QMessageBox, QLineEdit,  QComboBox, QStyledItemDelegate, QVBoxLayout
from UI.LoadingDataWidget import LoadingDataWidget
from UI.ObjectTypeEditlayout import ObjectTypeEditlayout

from utils import Globals

class VarTableModel(QAbstractTableModel):
    ''' 变量表模型类'''
    def __init__(self, header, data: list, table = None):
        '''
        header : 表头变量
        data : 表格内容
        table : 缺省参数
        '''
        QAbstractTableModel.__init__(self, parent=None)
        self.header = header
        self.datas = data
        self.checkList = ['Unchecked'] * len(self.datas)
        self.supportedDragActions()
        self.table = table
        self.editableList = [] # 表格中可编辑项


   
    def append_data(self, x):
        self.datas.append(x)
        self.checkList = ['Unchecked'] * len(self.datas)
        self.table.proxy.invalidate()
        # self.layoutChanged.emit()

    def insert_data(self, x, index):
        self.datas.insert(index, x)
        self.checkList = ['Unchecked'] * len(self.datas)
        self.table.proxy.invalidate()

    def updateColumn(self, row, value):
        if row < 0 or row >= self.rowCount():
            print("列索引超出范围")
            return
        # for row in range(self.rowCount()):
        self.datas[row][5] = value
        self.table.proxy.invalidate()
        # self.layoutChanged.emit()  # 通知视图数据已更改
        # self.dataChanged.emit()

    def remove_row(self, row):
        self.datas.pop(row)
        self.checkList = ['UnChecked'] * len(self.datas)
        self.table.proxy.invalidate()

    def rowCount(self, parent: QModelIndex = ...) -> int:
        if len(self.datas) > 0:
            return len(self.datas)
        return 0

    def columnCount(self, parent: QModelIndex = ...) -> int:
        return len(self.header)

    def data(self, QModelIndex, role=None):
        # print(Qt.__dict__.items())
        if role == Qt.TextAlignmentRole:
            return Qt.AlignCenter
        if not QModelIndex.isValid():
            print("行或者列有问题")
            return QVariant()

        if role == Qt.TextColorRole:
            return QtGui.QColor('#1A1A1A')

        if role == Qt.DisplayRole or role == Qt.EditRole:
            if QModelIndex.row() in self.editableList or 'w' in self.datas[QModelIndex.row()][4]:
                return self.datas[QModelIndex.row()][QModelIndex.column()]
        if role != Qt.DisplayRole:
            return QVariant()


        return self.datas[QModelIndex.row()][QModelIndex.column()]

    def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...) -> typing.Any: # 表头
        if role != Qt.DisplayRole:
            return None

        if role == Qt.SizeHintRole:
            # print(self.datas[QModelIndex.row()][QModelIndex.column()].find('\r\n'))
            return QSize(500, 500)

        if orientation == Qt.Horizontal:
            return self.header[section]

        # if orientation == Qt.Vertical:
        #     return str(section + 1)

    def setData(self, index, value, role):
        row = index.row()
        col = index.column()
        if role == Qt.EditRole:
            self.datas[row][col] = value
            return True
        return True

    def flags(self, index):
        # if index.column() == 0:
        #     return Qt.ItemIsEnabled | Qt.ItemIsUserCheckable
        if index.row() in self.editableList and index.column() or index.column() == 6:
            return Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsEditable
      

        if 'w' in self.datas[index.row()][4] and index.column() == 5:
            return Qt.ItemIsEnabled | Qt.ItemIsEditable
     
        return Qt.ItemIsEnabled

    def dragMoveEvent(self, event):
        event.setDropAction(QtCore.Qt.MoveAction)
        event.accept()

    def moveRow(self, sourceParent: QModelIndex, sourceRow: int, destinationParent: QModelIndex,
                destinationChild: int) -> bool:
        if self.datas[destinationChild] == self.datas[sourceRow]:
            return
        self.datas[sourceRow], self.datas[destinationChild] = self.datas[destinationChild], self.datas[sourceRow]
        self.table.proxy.invalidate()

    def updateValue(self, valueList):
        print(len(valueList), len(self.datas))
        for index, value in enumerate(valueList):
            self.datas[index][5] = value
        self.table.proxy.invalidate()



class VarButtonDelegate(QItemDelegate):
    """该类用于向单元格中添加按钮  任务表格"""
    def __init__(self, parent=None):
        super(VarButtonDelegate, self).__init__(parent)

    def paint(self, painter, option, index):
        if not self.parent().indexWidget(index):
            startActionBtn = QPushButton(
                qtawesome.icon('fa.play', color='#1fbb6f'),
                "",
                self.parent()
            )
            
            boxLayout = QHBoxLayout()
            
            refreshButton = QPushButton(
                qtawesome.icon('fa.refresh', color='#1fbb6f'),
                "",
                self.parent()
            )

            refreshButton.setToolTip('刷新')
            refreshButton.setObjectName("refreshButton")
            refreshButton.clicked.connect(self.refreshData)
            refreshButton.index = [index.row(), index.column()]

            readORwirte = self.parent().model.datas[index.row()][4]
            if 'w' not in readORwirte:
                boxLayout.addWidget(QSplitter())
                boxLayout.addWidget(refreshButton)
                boxLayout.addWidget(QSplitter())
            else:
                dataType = self.parent().model.datas[index.row()][3]
                objectType =  self.parent().model.datas[index.row()][1]

                editlineLayout = ObjectTypeEditlayout(objectType, dataType)

                startActionBtn.index = [index.row(), index.column()]
                startActionBtn.setToolTip('强制')

                

                
                if objectType == 'TARGET_MODE':
                    combox = QComboBox()
                    combox.setObjectName('modeCombox')
                    combox.addItems(["非服务", "手动初始化", "本地超驰", '手动', '自动', '级联', '远程级联', '远程输出'])
                    combox.setCurrentIndex(-1)
                    combox.currentIndexChanged.connect(lambda index : self.startAction(modelIndex = index))
                    boxLayout.addWidget(combox, 10)
                    boxLayout.addWidget(refreshButton,1)

                else:
                    boxLayout.addLayout(editlineLayout)
                    startActionBtn.clicked.connect(lambda: self.startAction(objectTypeEditlayout = editlineLayout))
                    boxLayout.addWidget(startActionBtn)
                    boxLayout.addWidget(refreshButton)

                
                startActionBtn.setObjectName('startActionBtn')

            boxLayout.setContentsMargins(0, 0, 0, 0)
            widget = QWidget()
            # widget.setMinimumHeight(200)
            widget.setLayout(boxLayout)
            self.parent().setIndexWidget(
                index,
                widget
            )
             
        
    def startAction(self, objectTypeEditlayout = None, modelIndex = None):
        sender = self.sender()
        blockView = self.parent()
        model = blockView.model
        blockType = blockView.blockType
        blockIndex = blockView.blcokIndex

        blockManage = Globals.getValue('blockManage')

        if modelIndex or str(modelIndex) == '0' :
            values = modelIndex
            self.refreshData(modelIndex)

        else:
            parmIndex = model.datas[sender.index[0]][0]
            values = objectTypeEditlayout.getEditlineValue()
            if not values:
                reply = QMessageBox.question(self.parent(),
                                                '警告',
                                                "请输入强制值或数字",
                                                QMessageBox.Yes)
                return
            
            for value in values:
                # print(value)
                pattern = re.compile(r'[^0-9\.-]+')
                if not value or re.findall(pattern, str(value)):
                    reply = QMessageBox.question(self.parent(),
                                                '警告',
                                                "请输入强制值或数字",
                                                QMessageBox.Yes)
                    return
            res = blockManage.writeParmValue(blockType, blockIndex, parmIndex, values)
            self.refreshData()
    
    def refreshData(self, modelIndex = None):
        sender = self.sender()
        blockView = self.parent()
        model = blockView.model
        parmIndex = model.datas[sender.index[0]][0]
        blockType = blockView.blockType
        blockIndex = blockView.blcokIndex
        
        # print(blockName, blcokIndex, index)
        blockManage = Globals.getValue('blockManage')
        value = blockManage.getBlockParmValue(blockType, blockIndex, parmIndex)
        # self.loadingDataWidget = LoadingDataWidget()
        # self.loadingDataWidget.show()
        # self.loadingDataWidget.loadData(1, 1)
        self.loadingDataWidget = LoadingDataWidget(refreshType = True)
        self.loadingDataWidget.exec_()
        model.updateColumn(sender.index[0], value)
        # self.parent().resizeHeader()
        blockView.proxy.invalidate()

    


