import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import "."

Rectangle {
    id: root
    Accessible.name: "kylin-aiassistant_main_root"
    visible: true
    color: "#F5F5F5"
    property string tabViewString: "Plugin"
    property bool isSupportDocumentQA: msgPane.isSupportDocumentQA();
    property bool isSupportImageProcessing: msgPane.isSupportImageProcessing();
    property bool isSupportHtml2Docx: msgPane.isSupportHtml2Docx();
    property int currentNum: 0
    property int fileCounts: 0

    // 清空之前对话函数
    function onClearListModel(){
        mychat.handleClearListModel()
    }

    // 清空之前对话函数
    function onClearMessageField(){
        mychat.handleClearMessageField()
    }

    //  初始化朗读
    function onInitReadingIndex(){
        mychat.handleInitReadingIndex()
    }

    function onCloseCommand(){
        mychat.closeCommand()
    }


    DropArea{
        anchors.fill: parent
        property var fileFormats: ["txt","pdf","docx","pptx"]
        property var imageFormats : [ "bmp", "jpe", "jpeg", "jpg", "pbm", "pgm", "png",  "ppm", "tiff", "webp" ]
        onEntered: {
            if(mychat.isReplying){
                drag.accepted = false;
                console.log("DropArea 1")
//                return false;
            }
            if(!(root.currentNum === 1 || root.currentNum === 4 || root.currentNum === 0)){
                drag.accepted = false;
                console.log("DropArea 2")
                return false;
            }

//            if(drag.urls.length !== 1){
//                drag.accepted = false;
//                return false;
//            }
            root.fileCounts = drag.urls.length
            var urls = drag.urls;
            var filePath = urls[0];
            var fileExtension = filePath.split('.').pop().toLowerCase();

//            if(!isSupportDocumentQA || fileFormats.indexOf(fileExtension) === -1){
//                drag.accepted = false;
//                return false;
//            }
            //1203注释，隐藏图片拖拽
            if((fileFormats.indexOf(fileExtension) === -1 && imageFormats.indexOf(fileExtension) === -1) || (!isSupportDocumentQA && fileFormats.indexOf(fileExtension) !== -1) || (!isSupportImageProcessing && imageFormats.indexOf(fileExtension) !== -1)){
                drag.accepted = false;
                console.log("DropArea 2")
                return false;
            }
            drag.accepted = true;
            dragShadow.visible = true;

            return true;
        }

        onExited: {
            dragShadow.visible = false;
        }

        onDropped: {
            mychat.backtoHome();
            var urls = drop.urls;
            var filePath = urls[0];
            filePath = filePath.replace("file://", "");
            var fileExtension = filePath.split('.').pop().toLowerCase();
            if(fileFormats.indexOf(fileExtension) !== -1){
                msgPane.onShowDocument(filePath);
            }
            else if(imageFormats.indexOf(fileExtension) !== -1){
                if(mychat.isReplying){
                    msgPane.onContinueDialog();
                } else {
                    msgPane.onShowImage(filePath,root.fileCounts);
                }
            }
            dragShadow.visible = false;
        }
    }

    Rectangle {
        id: dragShadow
        Accessible.name: "kylin-aiassistant_main_dragShadow"
        anchors.fill: parent
        color: "#bf262626"
        visible: false // 初始时遮罩层不可见
        z: 1 // 确保遮罩层在其他组件之上


        Text {
            id: alertText
            Accessible.name: "kylin-aiassistant_main_alertText"
            anchors.centerIn: parent
            text: qsTr("Release and send to AI assistant.")
            color: "white"
            font.pixelSize: 14
        }
        Image{
            anchors.bottom: alertText.top
            anchors.bottomMargin: 10
            anchors.horizontalCenter:  alertText.horizontalCenter
            source: "qrc:/icon/icon/send-white.svg"
        }

    }
    RowLayout{
        anchors.fill: parent

        SideBar{
            id: sideBarView
            Layout.fillWidth: true
            Layout.fillHeight: true
            visible: false
        }

        Chat{
            id:mychat
            Accessible.name: "kylin-aiassistant_main_mychat"
        }

//        MeetingAssistant{
//            id: meetingAssistantView
//            Accessible.name: "kylin-aiassistant_main_meetingAssistantView"
//            Layout.fillWidth: true
//            Layout.fillHeight: true
//            visible: false
//        }

        StackView{
            id: stackView
            Accessible.name: "kylin-aiassistant_main_stackView"
            Layout.fillWidth: true
            Layout.fillHeight: true
            initialItem: mychat // 设置初始页面
            visible: true
        }

        Connections{
            target: msgPane
            // 界面切换显示
            onShowPage:function (num){
                //console.log("**************** showPage");
                stackView.clear();
                root.currentNum = num;
                switch(num){
                //0显示全部
                case 0:
                    stackView.visible = true;
                    sideBarView.visible = false;
                    stackView.push(mychat);
                    break;
                //1显示主页面
                case 1:
                    ////在切换到主界面前根据当前是否在运行会议助手进行会议助手后台进度条显示
                    //msgPane.sigSetMeetingBackBar(msgPane.isMeetingRunning)
                    stackView.visible = true;
                    sideBarView.visible = false;
                    stackView.push(mychat);
                    mychat.handleIniInputMessagefild()
                    break;
                //2显示侧边栏
                case 2:
                    stackView.visible = false;
                    sideBarView.visible = true;
                    break;
                //3显示会议助手界面
                case 3:
                    stackView.visible = true;
                    sideBarView.visible = false;
                    //stackView.push(meetingAssistantView);
                    break;
                //4窗口模式下显示主界面
                case 4:
                    ////在切换到主界面前根据当前是否在运行会议助手进行会议助手后台进度条显示
                    //msgPane.sigSetMeetingBackBar(msgPane.isMeetingRunning)
                    stackView.visible =true;
                    sideBarView.visible = true;
                    stackView.push(mychat);
                    break;
                //5窗口模式下显示会议界面
//                case 5:
//                    stackView.visible = true;
//                    sideBarView.visible = true;
//                    stackView.push(meetingAssistantView);
//                    break;
                //6侧边栏模式下显示会议助手详情页
//                case 6:
//                    stackView.visible = true;
//                    sideBarView.visible = false;
//                    stackView.push(meetingAssistantView);
//                    meetingAssistantView.showMeetingDetails();
//                    break;
                //7窗口模式下显示会议助手详情页
//                case 7:
//                    stackView.visible = true;
//                    sideBarView.visible = true;
//                    stackView.push(meetingAssistantView);
//                    meetingAssistantView.showMeetingDetails();
//                    break;
                default:
                    break;
                }

            }
            // 侧边栏模式切换插件页面
            onShowPluginPage:function (num, pluginName){

            }

            // 窗口模式切换页面
            onShowWindowPage:function (num){

            }

            // 窗口模式切换插件页面
            onShowWindowPluginPage:function (num, pluginName){
            }

            onChangeToSideMode:function (width, height){
                stackView.Layout.fillWidth = true;
                stackView.Layout.fillHeight = true;
                stackView.Layout.minimumWidth = width;
                stackView.Layout.maximumWidth = width;
                stackView.Layout.minimumHeight = height;
                stackView.Layout.maximumHeight = height;

                sideBarView.Layout.fillWidth = true;
                sideBarView.Layout.fillHeight = true;
                sideBarView.Layout.minimumWidth = width;
                sideBarView.Layout.maximumWidth = width;
                sideBarView.Layout.minimumHeight = height;
                sideBarView.Layout.maximumHeight = height;
            }
            onChangeToWinMode:function (height){
//                stackView.width = root.width - 200;
//                stackView.height = root.height;
                stackView.Layout.minimumWidth = 567;
                stackView.Layout.maximumWidth = 16777215;
                stackView.Layout.minimumHeight = height;
                stackView.Layout.maximumHeight = 16777215;


                sideBarView.width = 200;
                sideBarView.Layout.minimumWidth = 200;
                sideBarView.Layout.maximumWidth = 200;
                sideBarView.Layout.minimumHeight = height;
                sideBarView.Layout.maximumHeight = 16777215;
            }

            onSigBtnClicked:function (activeModule)
            {
                //分转给会议助手进行处理
                if(activeModule ===0){
                    sideBarView.onBackBtnClick();
                }

            }
        }

    }

}
