import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import RibbonUI
import ProtocolParser

Item {
    id:obj
    property var header_items: ["序号","密钥"]
    property var header_items_width: [100,300]
    property var data_source: []
    implicitHeight: list_layout.height
    implicitWidth: list_layout.width

    onData_sourceChanged: {
        list.data_model.clear()
        list.data_model.append(data_source)
    }

    Component.onCompleted: {
        var data = []
        for (var i = 0; i < ZigBeeParser.pre_hmac_verify_key.length; i++)
        {
            data.push({"序号":i+1,"密钥":key_format(ZigBeeParser.pre_hmac_verify_key[i])})
        }
        data_source = data
    }

    Connections{
        target: ZigBeeParser
        function onPre_hmac_verify_keyChanged(){
            var data = []
            for (var i = 0; i < ZigBeeParser.pre_hmac_verify_key.length; i++)
            {
                data.push({"序号":i+1,"密钥":key_format(ZigBeeParser.pre_hmac_verify_key[i])})
            }
            data_source = data
        }
    }

    RibbonButton{
        anchors{
            top:parent.top
            margins: 4
            right:parent.right
        }
        show_bg: false
        show_hovered_bg: false
        icon_source: RibbonIcons.Dismiss
        onClicked: window_popup.close()
    }

    ColumnLayout{
        id:list_layout
        anchors{
            bottom: parent.bottom
            horizontalCenter: parent.horizontalCenter
        }

        RibbonText{
            id: title_text
            Layout.topMargin: 30
            Layout.alignment: Qt.AlignHCenter
            text: "密钥列表"
            view_only: true
            font.pixelSize: 15
        }

        RowLayout{
            Layout.preferredWidth: list.implicitWidth
            Layout.leftMargin: 30
            Layout.topMargin: 15
            RibbonText{
                id: c_key_text
                text: "当前密钥："
                view_only: true
            }
            RibbonText{
                Layout.preferredWidth: implicitWidth > (parent.width - c_key_text.width) ? parent.width - c_key_text.width : implicitWidth
                text: key_format(ZigBeeParser.hmac_verify_key)
                font.bold: true
                wrapMode: RibbonText.WordWrap
            }
        }

        RibbonText{
            text: `历史密钥(共${ZigBeeParser.pre_hmac_verify_key.length}个)`
            Layout.leftMargin: 30
            view_only: true
        }

        ListTable {
            id: list
            header_items: obj.header_items
            header_items_width: obj.header_items_width
            margins: 0
            Layout.topMargin: 10
            Layout.leftMargin: 30
            Layout.rightMargin: Layout.leftMargin
            Layout.bottomMargin: Layout.leftMargin
            Layout.preferredHeight: implicitHeight > 250 ? 250 : implicitHeight
        }

        RowLayout{
            spacing: 30
            Layout.alignment: Qt.AlignHCenter
            Layout.bottomMargin: 30
            RibbonButton{
                show_tooltip: false
                icon_source: RibbonIcons.Delete
                text: qsTr("删除")
                enabled: list.has_selected
                onClicked: {
                    let data = []
                    for (let i = 0; i < ZigBeeParser.pre_hmac_verify_key.length; i++)
                    {
                        if (i === list.current_index)
                            continue
                        data.push(ZigBeeParser.pre_hmac_verify_key[i])
                    }
                    ZigBeeParser.pre_hmac_verify_key = data
                }
            }
        }
    }
    function key_format(key)
    {
        let str = '0x'
        for (let i = 0; i < key.length; i++)
        {
            str += key[i]
            if ((i + 1) % 2 === 0 && (i + 1) !== key.length)
                str += ' 0x'
        }
        return str
    }
}
