/*!
 *
 * Copyright (c) 2021-2023 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)      18-Jul-2023
 * \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
    itemIndex       : SettingsStack.WiFi

    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:  {
        vNetwork.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 // helps user to tap on enter to go to the next entry, keyboard visibility is handled by TextEntry.
            textInput.text  : vNetwork.ipAddress
            label.text      : qsTr("IP Address")
            textInput.width : entryWidth
            label.width     : labelWidth
            hasCursor       : false // set false for now to disable the static IP entry for phase 1
            validator       : ipValidator
            onEnterPressed  : {
                vNetwork.doSetIPAddress(textInput.text)
            }
        }

        TextEntry { id      : _gateway
            nextInput       : _subnetmask // helps user to tap on enter to go to the next entry, keyboard visibility is handled by TextEntry.
            textInput.text  : vNetwork.gateway
            textInput.width : entryWidth
            label.width     : labelWidth
            label.text      : qsTr("Gateway")
            hasCursor       : false // set false for now to disable the static IP entry for phase 1
            validator       : ipValidator
            onEnterPressed  : {
                vNetwork.doSetGateway(textInput.text)
            }
        }

        TextEntry { id      : _subnetmask
            nextInput       : _dns // helps user to tap on enter to go to the next entry, keyboard visibility is handled by TextEntry.
            textInput.text  : vNetwork.subnetMask
            textInput.width : entryWidth
            label.width     : labelWidth
            label.text      : qsTr("Subnet Mask")
            hasCursor       : false // set false for now to disable the static IP entry for phase 1
            validator       : ipValidator
            onEnterPressed  : {
                vNetwork.doSetSubnetMask(textInput.text)
            }
        }

        TextEntry { id      : _dns
            textInput.text  : vNetwork.dns
            textInput.width : entryWidth
            label.width     : labelWidth
            label.text      : qsTr("DNS")
            hasCursor       : false // set false for now to disable the static IP entry for phase 1
            validator       : ipValidator
            onEnterPressed  : {
                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

            readonly property color networkDelegateTextColor : isNetworkSupported ? Colors.textMain : Colors.textDisableButton
            readonly property string postSecurityTypeLabel   : isNetworkSupported ? "" : " - " + qsTr("Not Supported")
            property bool isCurrrentNetwork                  : vNetwork.macAddress === macAddress


            clip            : true
            text.text       : ssid
            text.elide      : Text.ElideLeft
            text.color      : _delegate.networkDelegateTextColor
            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 + _delegate.postSecurityTypeLabel
                color           : _delegate.networkDelegateTextColor
            }

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

            onClicked           : {
                if( isNetworkSupported ) {
                    _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"
        readonly property string spaceSeparator : " "
        // notificationText    : vNetwork.status
        message             : qsTr("Do you want to disconnect from `%1`?").arg(ssid)
        title               : isPassword ? qsTr("Join") + separator + ssid + spaceSeparator + qsTr("Password") : qsTr("Disconnect") + separator + ssid
        onConfirmClicked    : {
            if ( isPassword ) {
                if( passwordCurrent.length > 0 ) {
                    vNetwork.doJoinNetwork  ( macAddress, passwordCurrent )
                    pop()
                }
            } else {
                vNetwork.doDisconnectNetwork( macAddress )
                pop()
            }
        }
    }
}
