import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import "./qml"

ApplicationWindow {
    id: appWindow
    visible: true
    width: 1920
    height: 1080

    //visibility: Window.FullScreen       // 设置窗口为全屏
    flags: Qt.FramelessWindowHint       // 设置窗口无边框

    property bool showFaceLoginInput: false
    property bool showCardLoginInput: false
    property bool showPasswordLoginInput: false
    property bool showFingerLoginInput: false

    property var loginInputSize: []

    Image {
        id: __backgroundImage
        x: 0
        y: 0
        width: parent.width
        height: parent.height
        source: "qrc:/resources/png/backspace.png"
        fillMode: Image.PreserveAspectFit
    }

    Component.onCompleted: {

    }

    Item {
        id: __contentItem
        anchors.fill: parent

        property bool canMouseOnScreen: false       // 点击屏幕是否可以取消登录界面

        Image {
            id: __logoImage
            x: 73
            y: 61
            width: 200
            height: 50
            source: "qrc:/resources/png/bozz.png"
            fillMode: Image.PreserveAspectFit
            MouseArea {
                anchors.fill: parent  // 让MouseArea覆盖整个Image区域
                onClicked: {

                }
            }
        }

        Loader {
            id: __login
            anchors.top: __logoImage.bottom
            anchors.centerIn: parent

            sourceComponent: ColumnLayout {
                id: __loginContent

                Label {
                    id: __titleLabel
                    color: "#ddffffff"
                    text: qsTr("物资管理系统")
                    font.pixelSize: 80
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    font.bold: true

                    Layout.alignment: Qt.AlignCenter
                }

                Row {
                    id: __loginRow
                    spacing: 10
                    Layout.alignment: Qt.AlignCenter

                    Loader {
                        id: __loginFace
                        x: 103
                        y: 56

                        sourceComponent: Login {
                            icon.source: "qrc:/resources/png/face.png"
                            text: qsTr("人脸登录")

                            loginType: Login.Face
                            isShowInput: showFaceLoginInput

                            onClicked: {
                                showFaceLoginInput = true;
                                if (isShowInput !== showFaceLoginInput) {
                                    isShowInput = true;
                                }
                                showCardLoginInput = false;
                                showPasswordLoginInput = false;
                                showFingerLoginInput = false;

                                __contentItem.canMouseOnScreen = true;
                                appWindow.loginInputSize = [width, height];
                            }
                        }
                    }

                    Loader {
                        id: __loginCard
                        x: 743
                        y: 56

                        sourceComponent: Login {
                            icon.source: "qrc:/resources/png/card.png"
                            text: qsTr("工卡登录")

                            loginType: Login.Card
                            isShowInput: showCardLoginInput

                            onClicked: {
                                showFaceLoginInput = false;
                                showCardLoginInput = true;
                                if (isShowInput !== showCardLoginInput) {
                                    isShowInput = true;
                                }
                                showPasswordLoginInput = false;
                                showFingerLoginInput = false;

                                __contentItem.canMouseOnScreen = true;
                                appWindow.loginInputSize = [width, height];
                            }
                        }
                    }

                    Loader {
                        id: __loginPassword
                        x: 423
                        y: 56

                        sourceComponent: Login {
                            id: __loginInputPassword
                            icon.source: "qrc:/resources/png/user.png"
                            text: qsTr("用户名登录")

                            loginType: Login.Password
                            isShowInput: showPasswordLoginInput

                            onClicked: {
                                showFaceLoginInput = false;
                                showCardLoginInput = false;
                                showPasswordLoginInput = true;
                                if (isShowInput !== showPasswordLoginInput) {
                                    isShowInput = true;
                                }
                                showFingerLoginInput = false;

                                __contentItem.canMouseOnScreen = true;
                                appWindow.loginInputSize = [width, height];
                            }

                            Connections {
                                target: __loginInputPassword
                                function onIsShowInputChanged() {
                                    if (!__loginInputPassword.isShowInput && showPasswordLoginInput) {
                                        console.log(__loginInputPassword.isShowInput, showPasswordLoginInput)
                                        showPasswordLoginInput = false;
                                    }
                                }
                            }
                        }
                    }

                    Loader {
                        id: __loginFinger
                        x: 1063
                        y: 56

                        sourceComponent: Login {
                            icon.source: "qrc:/resources/png/finger.png"
                            text: qsTr("指纹登录")

                            loginType: Login.Finger
                            isShowInput: showFingerLoginInput

                            onClicked: {
                                showFaceLoginInput = false;
                                showCardLoginInput = false;
                                showPasswordLoginInput = false;
                                showFingerLoginInput = true;
                                if (isShowInput !== showFingerLoginInput) {
                                    isShowInput = true;
                                }

                                __contentItem.canMouseOnScreen = true;
                                appWindow.loginInputSize = [width, height];
                            }
                        }
                    }
                }

                Rectangle {
                    opacity: 0
                    height: 200
                }

                Label {
                    id: __loginNotice
                    Layout.alignment: Qt.AlignCenter
                    width: 1920
                    color: "#ffffff"
                    text: qsTr("您可以通过刷卡或者指纹直接登录系统")
                    font.pixelSize: 42
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    font.bold: true
                }
            }
        }

        // 以下四个MouseArea是为了点击屏幕可以取消登录输入界面而存在
        MouseArea {
            id: __contentItemMouseArea1
            width: appWindow.loginInputSize[0]
            height: parent.height
            anchors.left: parent.left
            visible: __contentItem.canMouseOnScreen

            onClicked: {
                clickSreenToCancelInput();
            }
        }

        MouseArea {
            id: __contentItemMouseArea2
            width: parent.height
            height: (parent.height - appWindow.loginInputSize[1]) / 2
            anchors.top: parent.top
            visible: __contentItem.canMouseOnScreen

            onClicked: {
                clickSreenToCancelInput();
            }
        }

        MouseArea {
            id: __contentItemMouseArea3
            width: appWindow.loginInputSize[0]
            height: parent.height
            anchors.right: parent.right
            visible: __contentItem.canMouseOnScreen

            onClicked: {
                clickSreenToCancelInput();
            }
        }

        MouseArea {
            id: __contentItemMouseArea4
            width: parent.width
            height: (parent.height - appWindow.loginInputSize[1]) / 2
            anchors.bottom: parent.bottom
            visible: __contentItem.canMouseOnScreen

            onClicked: {
                clickSreenToCancelInput();
            }
        }
    }

    onShowFaceLoginInputChanged: {
        if (!showFaceLoginInput && __contentItem.canMouseOnScreen) {
            __contentItem.canMouseOnScreen = false;
        }
    }

    onShowCardLoginInputChanged: {
        if (!showCardLoginInput && __contentItem.canMouseOnScreen) {
            __contentItem.canMouseOnScreen = false;
        }
    }

    onShowPasswordLoginInputChanged: {
        if (!showPasswordLoginInput && __contentItem.canMouseOnScreen) {
            __contentItem.canMouseOnScreen = false;
        }
    }

    onShowFingerLoginInputChanged: {
        if (!showFingerLoginInput && __contentItem.canMouseOnScreen) {
            __contentItem.canMouseOnScreen = false;
        }
    }

    function clickSreenToCancelInput() {
        showFaceLoginInput = false;
        showCardLoginInput = false;
        showPasswordLoginInput = false;
        showFingerLoginInput = false;

        appWindow.loginInputSize = [__contentItem.width, __contentItem.height];
    }
}
