import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Material 2.12
import QtQuick.Layouts 1.12


Rectangle {
    id: sideBarView
    visible: true
//    color: "#F5F5F5"
    color: "#FFFFFF"
    Accessible.name: "kylin-aiassistant_SideBar_sideBarView"
    ColumnLayout {
        anchors.fill: parent

        // 新建对话按钮
        Row {
            id: sidebarRow
            width:parent.width
            leftPadding: 10
            rightPadding: 10
            topPadding: 4
            bottomPadding: 0
            Accessible.name: "kylin-aiassistant_SideBar_sidebarRow"
            Rectangle {
                width: sideBarView.width - sidebarRow.leftPadding - sidebarRow.rightPadding
                height: 36
                radius: 4
                color: "#3790FA"
                Accessible.name: "kylin-aiassistant_SideBar_Rectangle_1"

                Row {
                    anchors.centerIn: parent
                    spacing: 8
                    Accessible.name: "kylin-aiassistant_SideBar_Row_1"

                    Image {
                        id: createButtonImage
                        width: 16
                        height: 16
                        Accessible.name: "kylin-aiassistant_SideBar_createButtonImage"
                        source: "qrc:/icon/icon/sidebar/create_dialogue.svg"
                        anchors.verticalCenter: parent.verticalCenter
                    }
                    Text {
                        id: ctreateButtonText
                        text: qsTr("New conversation")
                        Accessible.name: "kylin-aiassistant_SideBar_ctreateButtonText"
                        color: "#FFFFFF"
                        anchors.verticalCenter: parent.verticalCenter
                    }
                }

                MouseArea {
                    anchors.fill: parent
                    hoverEnabled: true
                    Accessible.name: "kylin-aiassistant_SideBar_MouseArea_1"
                    // 按钮三态
                    onEntered: {
                        parent.color = "#3489EE";
                    }
                    onExited: {
                        parent.color = "#3790FA";
                    }
                    onPressed: {
                        parent.color = "#2C73C8";
                    }
                    onReleased: {
                        parent.color = "#3489EE";
                    }
                    onClicked: {
                        if(mychat.isReplying){
                           msgPane.onContinueDialog();
                        }else{
                            //msgPane.onStopSpeak();//停止朗读
                            //页面切换
                            var winMode = msgPane.getCurWinMode()
                            console.log("当前模式:" + winMode)
                            if(winMode === 2){
                                // 侧边栏模式页面切换
                                msgPane.onShowPage(1)
                            }else if(winMode === 3){
                                // 窗口模式页面切换
                                msgPane.onShowPage(4)
                                msgPane.updateDialogueListSessionId()
                            }else{
                                return
                            }
                            // 清空之前对话
                            root.onClearListModel()
                            // 新建对话时，初始化朗读
                            root.onInitReadingIndex()
                            // 清空session
                            msgPane.closeSession()
                        }
                    }
                }
            }
        }

         // 标签页切换栏
        Rectangle {
            id: changeRet
            Layout.fillWidth: true
            Layout.fillHeight: true
            Accessible.name: "kylin-aiassistant_SideBar_changeRet"
            color: "#FFFFFF"
            property int currentIndex: 0
            Connections {
                target: changeRet
                onCurrentIndexChanged: function(){
                    if (changeRet.currentIndex === 0) {
                        if (stackView.currentItem !== historyPageComponent) {
                            stackView.clear()
                            stackView.push(historyPageComponent)
                        }
                    } else {
                        if (stackView.currentItem !== pluginPageComponent) {
                            stackView.clear()
                            stackView.push(pluginPageComponent)
                        }
                    }
                }
            }

            ColumnLayout {
                anchors.fill: parent
                Rectangle {
                    id: tabBarRet
                    width: 200
                    Layout.preferredHeight: 36
                    Layout.alignment: Qt.AlignHCenter | Qt.AlignTop
                    Accessible.name: "kylin-aiassistant_SideBar_tabBarRet"
                    RowLayout {
                        anchors.centerIn: parent
                        id:tabBarRow
                        Accessible.name: "kylin-aiassistant_SideBar_tabBarRow"
                        spacing:0
                        Row{
                            id: historicalDialogueRow
                            Accessible.name: "kylin-aiassistant_SideBar_historicalDialogueRow"
                            Rectangle {
                                color: "#FFFFFF"
                                border.color: "transparent" // 默认边框透明
                                border.width: 0
                                width: (sideBarView.width - sidebarRow.leftPadding - sidebarRow.rightPadding) / 2
                                Accessible.name: "kylin-aiassistant_SideBar_Rectangle_2"
                                height:36
                                Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                                Text {
                                    // text: qsTr("Historical Dialogue")
                                    text: qsTr("Chat history")
                                    color: (changeRet.currentIndex === 0 ? "#3790FA" : "#000000")
                                    font.pixelSize: 14
                                    horizontalAlignment: Text.AlignHCenter
                                    verticalAlignment: Text.AlignVCenter
                                    anchors.centerIn: parent
                                }
                                // 只添加下边框
                                Rectangle {
                                    anchors.bottom: parent.bottom
                                    anchors.left: parent.left
                                    anchors.right: parent.right
                                    height: 2
                                    color: (changeRet.currentIndex === 0 ? "#3790FA" : "#0d262626") // 下边框颜色
                                    Accessible.name: "kylin-aiassistant_SideBar_Rectangle_3"
                                }
                                MouseArea{
                                    anchors.fill: parent
                                    hoverEnabled: true
                                    Accessible.name: "kylin-aiassistant_SideBar_MouseArea_2"
                                    onClicked: {
                                        changeRet.currentIndex = 0
                                        root.onCloseCommand()
                                    }
                                }
                            }
                        }
                    }

//                    Button {
//                        id: historicalDialogueTabButton
//                        width: (sideBarView.width - sidebarRow.leftPadding - sidebarRow.rightPadding) / 2
//                        contentItem: Text {
//                            // text: qsTr("Historical Dialogue")
//                            text: qsTr("历史对话")
//                            color: (historicalDialogueTabButton.checked ? "#3790FA" : "#000000")
//                            horizontalAlignment: Text.AlignHCenter
//                            verticalAlignment: Text.AlignVCenter
//                        }
//                        background: Rectangle {
//                            color: "#FFFFFF"
//                            border.color: "transparent" // 默认边框透明
//                            border.width: 0
//                            // 只添加下边框
//                            Rectangle {
//                                anchors.bottom: parent.bottom
//                                anchors.left: parent.left
//                                anchors.right: parent.right
//                                height: 2
//                                color: (historicalDialogueTabButton.checked ? "#3790FA" : "transparent") // 下边框颜色
//                            }
//                        }
//                    }
//                    Button {
//                        id: aIPluginTabButton
//                        width: (sideBarView.width - sidebarRow.leftPadding - sidebarRow.rightPadding) / 2
//                        contentItem: Text {
//                            // text: qsTr("AI Plugin")
//                            text: qsTr("AI 插件")
//                            color: (aIPluginTabButton.checked ? "#3790FA" : "#000000")
//                            horizontalAlignment: Text.AlignHCenter
//                            verticalAlignment: Text.AlignVCenter
//                        }
//                        background: Rectangle {
//                            color: "#FFFFFF"
//                            border.color: "transparent" // 默认边框透明
//                            border.width: 0
//                            // 只添加下边框
//                            Rectangle {
//                                anchors.bottom: parent.bottom
//                                anchors.left: parent.left
//                                anchors.right: parent.right
//                                height: 2
//                                color: (aIPluginTabButton.checked ? "#3790FA" : "transparent") // 下边框颜色
//                            }
//                        }
//                    }

                    // 初始页面设置
                    Component.onCompleted: {
                        if (changeRet.currentIndex === 0) {
                            stackView.push(historyPageComponent)
                        } else {
                            stackView.push(pluginPageComponent)
                        }
                    }

                    // 页面切换时，重新加载历史记录
                    onVisibleChanged: {
                        if(sideBarView.visible){
                            if (changeRet.currentIndex === 0) {
                                if (stackView.currentItem !== historyPageComponent) {
                                    stackView.clear()
                                    stackView.push(historyPageComponent)
                                }
                            } else {
                                if (stackView.currentItem !== pluginPageComponent) {
                                    stackView.clear()
                                    stackView.push(pluginPageComponent)
                                }
                            }
                        }
                    }
                }


                // 堆叠视图
                StackView {
                    id: stackView
                    Accessible.name: "kylin-aiassistant_SideBar_stackView"
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    Layout.alignment: Qt.AlignTop

                    // 历史对话页面
                    Component {
                        id: historyPageComponent

                        Loader {
                            source: "HistoryPage.qml"
                        }

                    }

                    // 插件页面
                    Component {
                        id: pluginPageComponent

                        Loader {
                            source: "PluginPage.qml"
                        }
                    }
                }
            }
        }
    }

    function onBackBtnClick(){
        var currentWindowMode = msgPane.getCurWinMode()
        //侧边栏模式返回侧边栏模式下的主页
        if(currentWindowMode === 2)
        {
            msgPane.onShowPage(1)
        }
        //窗口模式返回窗口模式下的主页
        else if(currentWindowMode ===3){
            msgPane.onShowPage(4)
        }
    }
}
