import QtQuick
import QtQuick.Window
import QtQuick.Controls 6.2
import QtQuick.Dialogs
import ConfigData
import Uart_Handler
import Ota_handler
import "qrc:/"

Window {
    id:root
    width: 400
    height: 170
    visible: true
    title: qsTr("尾插板测试升级工具有效期至4月20日")
    property var jsonconfig : ""

    function debug_print(data){
        user_uart.debug_print(data)
    }

    ConfigData{
        id:user_config
    }

    Uart_Handler{
        id:user_uart
    }

    Ota_handler{
        id:user_ota
    }
    
    Component.onCompleted:{
        uart_comboBox.model = user_uart.scan_port()
        jsonconfig = JSON.parse(user_config.get_config())
        user_uart.set_baud(jsonconfig["串口波特率"])
        filepath.text = jsonconfig["bin路径"]
        uart_comboBox.currentIndex = jsonconfig["串口默认索引"]
        debug_print("ui加载完成")
    }

    Timer {
        id: uart_rx;
        interval: jsonconfig["串口读取间隔"];
        repeat: true 
        running: false 
        triggeredOnStart: false 
        onTriggered: {
            user_uart.read()
        }
    }

    Connections {
        target: user_uart 
        ignoreUnknownSignals: true 
        function onRxdata_sig(data, len) { 
            // debug_print("收到数据 ")
            user_ota.uart_datapacket_parse(data, len)
        }
        function onPorterrot_sig(){
            user_uart.close()
            uart_open_button.text = "打开串口"
            uart_comboBox.enabled = true
            uart_rx.running = false
        }
    }

    Connections {
        target: user_ota 
        ignoreUnknownSignals: true 
        function onTxdata_sig(data, len) { 
            if(user_uart.get_uartopen_status() == true)
                user_uart.send(data, len)
        }
        function onState_update_sig(data,color) { 
            stateText.text = data
            stateText.color = color
        }
    }

    ComboBox {
        id: uart_comboBox
        x: 15
        y: 10
        width: 160
        height:40
    }

    Timer {
        id: timer_uart_scan;
        interval: 200;
        repeat: true 
        running: true 
        triggeredOnStart: false 
        onTriggered: {
            if(user_uart.get_uartopen_status() == false)
            {
                var name_list = user_uart.scan_port()
                if(name_list.length !== uart_comboBox.model.length)
                {
                    uart_comboBox.model = name_list
                }
            }
        }
    }

    Button{
        id:uart_open_button
        x: 220
        y: 10
        width: 160
        height:40
        text: qsTr("打开串口")
        onClicked: {
            if(user_uart.get_uartopen_status() == false)
            {
                if(user_uart.open(uart_comboBox.currentText) == true)
                {
                    uart_open_button.text = "关闭串口"
                    uart_comboBox.enabled = false
                    uart_rx.running = true
                }
                else
                {
                    stateText.text = "串口打开失败"
                    stateText.color = "red"
                }
            }
            else
            {
                user_uart.close()
                uart_open_button.text = "打开串口"
                uart_comboBox.enabled = true
                uart_rx.running = false
            } 
        }
    }

    TextField{
        id:filepath
        placeholderText:qsTr("文件路径")
        font.family: "Microsoft YaHei"
        font.pixelSize: 12
        x: 30
        y: 55
        width:(root.width-20)/3*2
        height:30
        background: Rectangle {
            border.color: "#26000000"
        }
    }

    FileDialog {
        id: fileDialog
        selectedNameFilter.index: 1
        nameFilters: ["bin files (*.bin)"]
        onAccepted:{
            filepath.text = fileDialog.currentFile.toString().substring(8,fileDialog.currentFile.toString().length)
        }
    }

    SimpleIconButton{
        id:filebutton
        height:30
        width:30
        anchors.left: filepath.right 
        anchors.leftMargin: 30
        anchors.top: filepath.top

        img_src_on:'qrc:/text_on.png'
        img_src_off:'qrc:/text_off.png'

        onClickedLeft:{
            fileDialog.open()
        }

        onClickedRight:{
            fileDialog.open()
        }
    }

    Button{
        id:ota_button
        x: 220
        y: 90
        width: 160
        height:40
        text: qsTr("开始升级")
        onClicked: {
            user_ota.ota_start(filepath.text)
        }
    }

    Button{
        id:test_button
        x: 10
        y: 90
        width: 160
        height:40
        text: qsTr("通讯测试")
        onClicked: {
            if(user_uart.get_uartopen_status() == true)
            {
                user_ota.send_conn_test()
            }
        }
    }
    
    Text{
        id:stateText
        font.family: "Microsoft YaHei"
        font.pixelSize: 16
        color: "green"
        x: 20
        y: 135
        width:root.width - 40
        height:30
        text: ""
        // background: Rectangle {
        //     border.color: "#26000000"
        // }
    }
}

