import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import AntiClipSettings 1.0

Popup {
    id: root
    parent: Overlay.overlay
    anchors.centerIn: Overlay.overlay
    modal: true
    closePolicy: Popup.CloseOnEscap | Popup.NoAutoClose
    property int inputHeight: 56
    background: Rectangle {
        radius: 8
    }
    contentItem: ColumnLayout {
        anchors.centerIn: parent
        Label {
            Layout.alignment: Qt.AlignCenter
            text: "有线网络设置"
        }

        Row {
            spacing: 10

            Label {
                text: "模式"
                width: 100
                anchors.verticalCenter: parent.verticalCenter
            }

            RadioButton {
                id: dhcpMode
                text: "DHCP"
                checked: App.currentNetworkInfomation.dhcp
            }

            RadioButton {
                id: staticMode
                text: "静态IP"
                checked: !App.currentNetworkInfomation.dhcp
            }
        }

        Row {
            spacing: 5
            visible: staticMode.checked
            Row {
                width: 100
                anchors.verticalCenterOffset: -10
                anchors.verticalCenter: parent.verticalCenter
                Label {
                    text: "设备IP"
                }
                Label {
                    color: "red"
                    text: "*"
                }
            }

            IpTextField {
                id: ipInput
                height: inputHeight
                width: 350
                text: App.currentNetworkInfomation.ip
                regularExpression: /^(22[0-3]|2[01][0-9]|1[0-9]{2}|[1-9][0-9]?|1[0-9]{2})\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$/;
            }
        }

        Row {
            spacing: 5
            visible: staticMode.checked
            Row {
                width: 100
                anchors.verticalCenterOffset: -10
                anchors.verticalCenter: parent.verticalCenter
                Label {
                    text: "子网掩码"
                }
                Label {
                    color: "red"
                    text: "*"
                }
            }

            IpTextField {
                id: netmaskInput
                width: 350
                height: inputHeight
                text: App.currentNetworkInfomation.netmask
            }
        }

        Row {
            spacing: 5
            visible: staticMode.checked
            Label {
                anchors.verticalCenter: parent.verticalCenter
                text: "设备网关"
                anchors.verticalCenterOffset: -10
                width: 100
            }

            IpTextField {
                id: gatewayInput
                width: 350
                height: inputHeight
                text: App.currentNetworkInfomation.gateway
                regularExpression: /^(22[0-3]|2[01][0-9]|1[0-9]{2}|[1-9][0-9]?|1[0-9]{2})\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$/;
                canEmpty: true
            }
        }

        Row {
            spacing: 5
            visible: false // staticMode.checked    暂时不用设置
            Label {
                anchors.verticalCenter: parent.verticalCenter
                text: "DNS服务器"
                anchors.verticalCenterOffset: -10
                width: 100
            }

            IpTextField {
                id: dnsInput
                width: 350
                height: inputHeight
                text: App.currentNetworkInfomation.dns
                canEmpty: true
            }
        }

        Row {
            Layout.rightMargin: 20
            Layout.alignment: Qt.AlignRight
            spacing: 20

            Button {
                text: "保存"
                onClicked: {
                    ipInput.validate()
                    netmaskInput.validate()
                    gatewayInput.validate()
                    dnsInput.validate()
                    if (dhcpMode.checked || (staticMode.checked && ipInput.valid
                                             && netmaskInput.valid
                                             && gatewayInput.valid
                                             && dnsInput.valid)) {
                        App.updateNetworkInfomation(dhcpMode.checked,
                                                    ipInput.text,
                                                    netmaskInput.text,
                                                    gatewayInput.text,
                                                    dnsInput.text)
                        networkPopup.close()
                    }
                }
            }

            Button {
                text: "取消"
                onClicked: root.close()
            }
        }
    }
    onVisibleChanged: {
        if (visible) {
            ipInput.reset()
            netmaskInput.reset()
            gatewayInput.reset()
            dnsInput.reset()

            dhcpMode.checked = App.currentNetworkInfomation.dhcp
            staticMode.checked = !App.currentNetworkInfomation.dhcp
            ipInput.text = App.currentNetworkInfomation.ip
            netmaskInput.text = App.currentNetworkInfomation.netmask
            gatewayInput.text = App.currentNetworkInfomation.gateway
            dnsInput.text = App.currentNetworkInfomation.dns
        }
    }
}