/*!
 *
 * Copyright (c) 2021-2022 Diality Inc. - All Rights Reserved.
 * \copyright
 * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN
 * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER.
 *
 * \file    SettingsWiFi.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      21-Jun-2021
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  06-May-2021
 *
 */

// Qt
import QtQuick 2.12

// Project
import Gui.Actions 0.1

//  Qml imports
import "qrc:/globals"
import "qrc:/components"
import "qrc:/pages"

/*!
 * \brief   SettingsWiFi
 *          The settings screen to setup the WiFi connection
 */
SettingsBase { id: _root

    property var ipValidator: RegExpValidator {
        regExp:/^(([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))\.){3}([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))$/
    }

    labelWidth    : 200
    entryWidth    : 175

    readonly property int spacing       : 20
    readonly property int leftMargin    : 50
    readonly property int rightMargin   : 50
    readonly property int topMargin     : topMarginContent - 40 // moved up to top of the keyboard

    readonly property bool  isValid     :
           _ipAddress  .textInput.acceptableInput
        && _gateway    .textInput.acceptableInput
        && _subnetmask .textInput.acceptableInput
        && _dns        .textInput.acceptableInput

    confirmEnabled  : isValid
    confirmVisible  : false // since the static setting is done one by one seems confirm is not needed for now.
    firstFocusInput : _ipAddress
    notificationText: vNetwork.status
    onConfirmClicked:  {
        vDateTime.doConfirm(
            _ipAddress  .textInput.text ,
            _gateway    .textInput.text ,
            _subnetmask .textInput.text ,
            _dns        .textInput.text )
    }

    Column { id             : _ipColumn
        spacing             : _root.spacing
        y                   : _root.topMargin
        anchors.left        : parent.left
        anchors.leftMargin  : _root.leftMargin

        TextEntry { id      : _ipAddress
            // nextInput       : _gateway // it's not needed to be entered in order all the time and also the keyboard needs to be hidden for the set result to be shown.
            textInput.text  : vNetwork.ipAddress
            label.text      : qsTr("IP Address")
            textInput.width : entryWidth
            label.width     : labelWidth
            hasCarret       : true
            validator       : ipValidator
            onEnterPressed  : {
                _keyboard.setVisible(false)
                vNetwork.doSetIPAddress(textInput.text)
            }
        }

        TextEntry { id      : _gateway
            // nextInput       : _subnetmask // it's not needed to be entered in order all the time and also the keyboard needs to be hidden for the set result to be shown.
            textInput.text  : vNetwork.gateway
            textInput.width : entryWidth
            label.width     : labelWidth
            label.text      : qsTr("Gateway")
            hasCarret       : true
            validator       : ipValidator
            onEnterPressed  : {
                _keyboard.setVisible(false)
                vNetwork.doSetGateway(textInput.text)
            }
        }

        TextEntry { id      : _subnetmask
            // nextInput       : _dns // it's not needed to be entered in order all the time and also the keyboard needs to be hidden for the set result to be shown.
            textInput.text  : vNetwork.subnetMask
            textInput.width : entryWidth
            label.width     : labelWidth
            label.text      : qsTr("Subnet Mask")
            hasCarret       : true
            validator       : ipValidator
            onEnterPressed  : {
                _keyboard.setVisible(false)
                vNetwork.doSetSubnetMask(textInput.text)
            }
        }

        TextEntry { id      : _dns
            textInput.text  : vNetwork.dns
            textInput.width : entryWidth
            label.width     : labelWidth
            label.text      : qsTr("DNS")
            hasCarret       : true
            validator       : ipValidator
            onEnterPressed  : {
                _keyboard.setVisible(false)
                vNetwork.doSetDNS(textInput.text)
            }
        }
    }

    Row { id : _ssidRow
        anchors.top         : _ipColumn.bottom
        anchors.topMargin   : 50
        anchors.left        : parent.left
        anchors.leftMargin  : _root.leftMargin
        Text { id           : _ssidLabel
            font.pixelSize  : Fonts.fontPixelTextRectExtra
            color           : Colors.textMain
            horizontalAlignment : TextInput.Alignleft
            width           : labelWidth
            text            : qsTr("SSID")
        }

        Text { id           : _ssidText
            font.pixelSize  : Fonts.fontPixelTextRectExtra
            color           : Colors.textMain
            horizontalAlignment : TextInput.Alignleft
            text            : vNetwork.ssid
            width           : entryWidth
        }
    }

    TouchRect { id          : _scanButton
        anchors.bottom      : parent.bottom
        anchors.bottomMargin: Variables.mainMenuHeight * 2 + Variables.minVGap
        anchors.left        : parent.left
        anchors.leftMargin  : _root.leftMargin
        text.text           : qsTr("SCAN")
        width               : 350
        isDefault           : true
        enabled             :!vNetwork.scanInProgress
        onClicked           : vNetwork.doScan()
    }

    ScrollBar { flickable   : _networkList
                anchors.fill: _networkList
    }
    ListView    { id        : _networkList
        model               : vNetwork
        clip                : true
        spacing             : 7
        y                   : _root.topMargin
        width               : 450
        anchors.top         : _ipColumn.top
        anchors.bottom      : _scanButton.bottom
        anchors.right       : _root.right
        anchors.rightMargin : _root.rightMargin
        delegate            : TouchRect { id : _delegate

            property bool isCurrrentNetwork: vNetwork.macAddress === macAddress

            clip            : true
            text.text       : ssid
            text.elide      : Text.ElideLeft
            width           : _networkList.width - Variables.minVGap
            height          : 75
            radius          : Variables.dialogRadius

            text.anchors.horizontalCenter   : undefined
            text.horizontalAlignment        : Text.AlignLeft
            text.leftPadding                : 5
            border.width        : 1

            Text { id           : _securityLevel
                anchors {
                    left        : parent.left
                    leftMargin  : 10
                    bottom      : parent.bottom
                }
                font.pixelSize  : Fonts.fontPixelDialogText
                text            : securityTypes
                color           : Colors.textMain
            }

            Text { id           : _isConnected
                anchors {
                    right       : parent.right
                    rightMargin : 10
                    bottom      : parent.bottom
                }
                font.pixelSize  : Fonts.fontPixelDialogText
                text            : _delegate.isCurrrentNetwork ? qsTr("Connected") : ""
                color           : Colors.textMain
            }

            onClicked           : {
                _userConfirmation.isPassword    = ! _delegate.isCurrrentNetwork
                _userConfirmation.ssid          = ssid
                _userConfirmation.macAddress    = macAddress
                push( _userConfirmation )
                _userConfirmation.setFocus()
            }
        }
    }

    UserConfirmation { id : _userConfirmation
        property string ssid        : ""
        property string macAddress  : ""
        property string separator   : "\n\n"
        // notificationText    : vNetwork.status
        message             : qsTr("Do you want to disconnect from `%1`?").arg(ssid)
        title               : isPassword ? qsTr("Join") + separator + ssid + qsTr("Password") : qsTr("Disconnect") + separator + ssid
        onConfirmClicked    : {
            if ( isPassword ) {
                vNetwork.doJoinNetwork      ( macAddress, password )
            } else {
                vNetwork.doDisconnectNetwork( macAddress )
            }
            pop()
        }
    }
}
