import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.14
import QtQuick.Dialogs 1.3
import QtQuick.Controls.Styles 1.4
import QtQuick.VirtualKeyboard 2.15

import TService 1.0

Rectangle{
    id:root
    //anchors.fill:parent

    Item{
        id:rootitem
        anchors.fill:parent
        property int max_compartment_num:4

        enum Page_Type {
            Page_Type_Settings,
            Page_Type_Testing,
            Page_Type_ValveTest,
            Page_Type_Calibration
        }

        SwipeView {
            id: swipeView
            //anchors.fill: parent
            width: main_window.width
            height: main_window.height

            currentIndex: 0
            y: !inputPanel.active ? 0 : Math.min(0, main_window.height - inputPanel.height - activeFocusItemBottom)
            Behavior on y {
                NumberAnimation {
                    duration: 250
                    easing.type: Easing.InOutQuad
                }
            }

            PageSetting {
                id:page_set
            }

            PageTest {
                id:pagetest
                //visible: false;
            }

            PageCalibration {
                id:pagecalibration
            }


            function changePages(compartment_num){
                console.log("changePages num : "+compartment_num)
                removePage(pagetest)
                removePage(pagecalibration)
/*
                if(compartment_num>7){
                    addPage(page_recoveryvalve1)
                    addPage(page_recoveryvalve2)
                    addPage(page_recoveryvalve3)
                    addPage(page_recoveryvalve4)
                    addPage(page_recoveryvalve5)
                    addPage(page_recoveryvalve6)
                    addPage(page_recoveryvalve7)
                    addPage(page_recoveryvalve8)
                }else if(compartment_num>6){
                    removePage(page_recoveryvalve8)
                    addPage(page_recoveryvalve1)
                    addPage(page_recoveryvalve2)
                    addPage(page_recoveryvalve3)
                    addPage(page_recoveryvalve4)
                    addPage(page_recoveryvalve5)
                    addPage(page_recoveryvalve6)
                    addPage(page_recoveryvalve7)
                }else if(compartment_num>5){
                    removePage(page_recoveryvalve8)
                    removePage(page_recoveryvalve7)
                    addPage(page_recoveryvalve1)
                    addPage(page_recoveryvalve2)
                    addPage(page_recoveryvalve3)
                    addPage(page_recoveryvalve4)
                    addPage(page_recoveryvalve5)
                    addPage(page_recoveryvalve6)
                }else if(compartment_num>4){
                    removePage(page_recoveryvalve8)
                    removePage(page_recoveryvalve7)
                    removePage(page_recoveryvalve6)
                    addPage(page_recoveryvalve1)
                    addPage(page_recoveryvalve2)
                    addPage(page_recoveryvalve3)
                    addPage(page_recoveryvalve4)
                    addPage(page_recoveryvalve5)
                }else
                    */
                if(compartment_num>3){
                    //removePage(page_recoveryvalve8)
                    //removePage(page_recoveryvalve7)
                    //removePage(page_recoveryvalve6)
                    //removePage(page_recoveryvalve5)
                    addPage(page_recoveryvalve1)
                    addPage(page_recoveryvalve2)
                    addPage(page_recoveryvalve3)
                    addPage(page_recoveryvalve4)
                }else if(compartment_num>2){
                    //removePage(page_recoveryvalve8)
                    //removePage(page_recoveryvalve7)
                    //removePage(page_recoveryvalve6)
                    //removePage(page_recoveryvalve5)
                    removePage(page_recoveryvalve4)
                    addPage(page_recoveryvalve1)
                    addPage(page_recoveryvalve2)
                    addPage(page_recoveryvalve3)
                }else if(compartment_num>1){
                    //removePage(page_recoveryvalve8)
                    //removePage(page_recoveryvalve7)
                    //removePage(page_recoveryvalve6)
                    //removePage(page_recoveryvalve5)
                    removePage(page_recoveryvalve4)
                    removePage(page_recoveryvalve3)
                    addPage(page_recoveryvalve1)
                    addPage(page_recoveryvalve2)
                }else if(compartment_num>0){
                    //removePage(page_recoveryvalve8)
                    //removePage(page_recoveryvalve7)
                    //removePage(page_recoveryvalve6)
                    //removePage(page_recoveryvalve5)
                    removePage(page_recoveryvalve4)
                    removePage(page_recoveryvalve3)
                    removePage(page_recoveryvalve2)
                    addPage(page_recoveryvalve1)
                }

                addPage(pagetest)
                addPage(pagecalibration)
            }

            function addPage(page){

                for(var n=0; n<count; n++){
                    if(page === itemAt(n))
                        return
                }

                addItem(page)
                page.visible = true
            }

            function removePage(page){
                for(var n=0; n<count; n++){
                    if(page === itemAt(n))
                        takeItem(n)
                }

                page.visible = false
            }

            Component.onCompleted: {
                //page_set.show()
                //currentIndex=0
            }

        }

        PageIndicator {
            id: indicator
            count: swipeView.count

            currentIndex: swipeView.currentIndex
            anchors.bottom: swipeView.bottom
            anchors.horizontalCenter: parent.horizontalCenter

            //interactive: true
        }

        Component.onCompleted: {
            StandardManager.init()
            CalibrationPara.init()

            testService.init()
            //StandardManager.init()
            //CalibrationPara.init()

        }


    }


    RecoveryValve {
        id:page_recoveryvalve1
        compartment_id: 1
        volume: 0
        visible: false

    }


    RecoveryValve {
        id:page_recoveryvalve2
         compartment_id: 2
         volume: 0
         visible: false

    }

    RecoveryValve {
        id:page_recoveryvalve3
         compartment_id: 3
         volume: 0
         visible: false

    }
    RecoveryValve {
        id:page_recoveryvalve4
         compartment_id: 4
         volume: 0
         visible: false

    }
    /*
    RecoveryValve {
        id:page_recoveryvalve5
         compartment_id: 5
         volume: 0
         visible: false

    }
    RecoveryValve {
        id:page_recoveryvalve6
         compartment_id: 6
         volume: 0
         visible: false

    }
    RecoveryValve {
        id:page_recoveryvalve7
         compartment_id: 7
         volume: 0
         visible: false

    }
    RecoveryValve {
        id:page_recoveryvalve8
         compartment_id: 8
         volume: 0
         visible: false

    }
*/
    //PageStandard {
    //    id:pagestandard
    //    visible: false

    //}


    Log{
        id:log
        z:10
        width: 300
        height: 50
        //anchors.centerIn: parent
        anchors.top: parent.top
        anchors.topMargin: 100
        anchors.left: parent.left
        anchors.leftMargin: 480
    }

    Log{
        id:genreportprogress
        z:20
        width: 300
        height: 50
        //anchors.centerIn: parent
        anchors.top: parent.top
        anchors.topMargin: 100
        anchors.left: parent.left
        anchors.leftMargin: 480
    }

    TestService{
        id:testService
    }

    Popup{
       id:             popupCenter
       modal:          true            //模态， 为 true后弹出窗口会叠加一个独特的背景调光效果
       focus:          true            //焦点,  当弹出窗口实际接收到焦点时，activeFocus将为真
       padding:        0
       closePolicy:    Popup.CloseOnEscape  //| Popup.CloseOnPressOutside
       property var    raiseItem:          null
       property bool   item_opened:  false
       background: Rectangle {
            color:      Qt.rgba(0,0,0,0)   //背景为无色
       }
       Loader {
           id:loaderCenter
           onLoaded: {
               popupCenter.x = (main_window.width - loaderCenter.width) * 0.5
               popupCenter.y = (main_window.height - loaderCenter.height) * 0.5
               popupCenter.z = 12
           }
       }
       onOpened: {
           loaderCenter.sourceComponent = popupCenter.raiseItem
           popupCenter.item_opened = true
       }
       onClosed: {
           loaderCenter.sourceComponent = null
           popupCenter.raiseItem = null
           popupCenter.item_opened = false
       }

    }

    Component{
        id: com_leakage
        PageLeakageInput{
            id:leakage_input
            carplate: page_set.licenseplate
        }

    }

    Component.onCompleted: {
        //page_set.show()

        testService.onNotice.connect(qmlProcessNotice);
        testService.onWarning.connect(qmlProcessWarning);
        testService.sigPressure.connect(qmlProcessPressure);

        testService.sigState.connect(qmlProcessState);
        testService.sigSystemResult.connect(qmlProcessSystemResult);
        testService.sigValveResult.connect(qmlProcessValveResult);
        testService.sigSelfTestResult.connect(qmlProcessSelfResult);
        testService.onGenreportProgress.connect(qmlProcessReportProgress);

        testService.sigStop.connect(qmlProcessStop);

        console.log("main.qml tservice init: 222" )
    }

    function delay(duration) { // In milliseconds
        var timeStart = new Date().getTime();

        while (new Date().getTime() - timeStart < duration) {
            // Do nothing
        }

        // Duration has passed
    }

    function qmlProcessReportProgress(str){

        console.log("main.qml tservice qmlProcessReportProgress:", str )

        if(str === "close"){
            genreportprogress.exit()
            if(popupCenter.item_opened == true){
                popupCenter.close()
            }
        }else{
            genreportprogress.show(str)
        }

    }

    function qmlProcessNotice(str){
        console.log("main.qml tservice qmlProcessNotice: enter [%s]", str )
        log.show(str)

    }

    function qmlProcessWarning(str){
        console.log("main.qml tservice init: 222 [%s]", str )
        //log.show2(str)
        //id_mesDialog.text = str+", 是否继续?";
        //id_mesDialog.open()
    }

    function getTestPage(id){
        var page_var
        switch(id){
        case 1:page_var = page_recoveryvalve1
            break
        case 2:page_var = page_recoveryvalve2
            break
        case 3:page_var = page_recoveryvalve3
            break
        case 4:page_var = page_recoveryvalve4
            break
            /*
        case 5:page_var = page_recoveryvalve5
            break
        case 6:page_var = page_recoveryvalve6
            break
        case 7:page_var = page_recoveryvalve7
            break
        case 8:page_var = page_recoveryvalve8
            break
            */
        default:
            page_var = page_recoveryvalve1
            break;
        }

        return page_var
    }

    function qmlProcessPressure(id, map){
        //console.log("main.qml qmlProcessPressure ",id )
        //log.show2(str)
        //var textstring = ''
        //var varmap = map
        //textstring += map.pressure+":"
        //textstring += map.direction.toString()+":"
        //textstring += map.stage.toString()+":"
        //textstring += map.step.toString()+":"

        //console.log("main.qml qmlProcessPressure :",textstring.toString() )

        if((id >= 1) &&(id<=rootitem.max_compartment_num)){
            var page_var = getTestPage(id)
            page_var.update_pressure(map);
        }else if(id === 0){
            //pagetest.show_pressure(map.pressure)
        }


        //console.log("show compartment ",id, str )

    }

    function qmlProcessState(id, str1, str2){
        if((id >= 1) &&(id<=rootitem.max_compartment_num)){
            var page_var = getTestPage(id)
            page_var.update_runstate(str1, str2)
        }else if(id === 0){
            //pagetest.show_selftestState(str1)
            console.log("qmlProcessState pagetest ",id, str1 )
        }

    }

    function qmlProcessSystemResult(id, str, bPassed){
        if((id >= 1) &&(id<=rootitem.max_compartment_num)){
            var page_var = getTestPage(id)
            page_var.show_systemresult(str, bPassed)
        }

    }

    function qmlProcessValveResult(id, str, bPassed){

        if((id >= 1) &&(id<=rootitem.max_compartment_num)){
            var page_var = getTestPage(id)
            page_var.show_valveresult(str, bPassed)
        }

    }

    function qmlProcessSelfResult(id, str, bPassed){
        if(id === 0){
            if(bPassed){
                pagetest.show_selftestResult("通过")
                //pagetest.show_selftestResult(str)
            }else{
                pagetest.show_selftestResult("未通过")
                //pagetest.show_selftestResult(str)
            }

            //pagetest.show_selftestDetail(str)
        }

    }



    function qmlProcessStop(id){
        if((id >= 1) &&(id<=rootitem.max_compartment_num)){
            var page_var = getTestPage(id)
            page_var.test_stoped()

            if(testService.check_alltest_ok(0))
            {
                qmlshowPopupCenter(com_leakage)
            }

        }else if(id === 0){
            //pagetest.selftestStop()
            console.log("qmlProcessStop pagetest ",id )
        }
    }

    function qmlshowPopupCenter(raiseItem){
        popupCenter.raiseItem = raiseItem
        popupCenter.open()
    }

}


