/*!
 *
 * Copyright (c) 2021-2024 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.15

// Project

//  Qml imports
import "qrc:/globals"
import "qrc:/compounds"
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     : 160
    readonly property bool  isConnected : vDevice.ssid !== ""

    confirmVisible  : false

    notificationText: vDevice.status


    contentItem: Item { id: _contentRow
        anchors.fill: parent

        Item { id: _wifiItem
            anchors {
                left            : parent.left
                verticalCenter  : parent.verticalCenter
            }

            width   : parent.width / 2.5
            height  : parent.height

            Text { id : _wifiInfoTitle
                anchors {
                    top : parent.top
                    left: parent.left
                }
                width           : parent.width
                height          : 60
                text            :  qsTr("Current Wi-Fi Information")
                font.pixelSize  : Fonts.fontPixelButton
                font.weight     : Font.Medium
                color           : Colors.offWhite

                Line { id: _divider
                    color   : Colors.panelBorderColor
                    anchors {
                        bottom      : parent.bottom
                        bottomMargin: 10
                        left        : parent.left
                        right       : parent.right
                    }
                }
            }

            TouchGrid { id: _currentWifi
                anchors {
                    top : _wifiInfoTitle.bottom
                    left: _wifiInfoTitle.left
                }
                width               : parent.width
                touchable           : false
                alignCenter         : false
                rowCount            : 5
                colCount            : 1
                itemWidth           : width
                itemsText           : [ qsTr("SSID")        ,
                                        qsTr("IP Address")  ,
                                        qsTr("Gateway")     ,
                                        qsTr("Subnet Mask") ,
                                        qsTr("DNS")         ]
                lineColor           : Colors.panelBorderColor

                itemsValue          : [ vDevice.ssid                                ,
                                        _root.isConnected ? vDevice.ipAddress : ""  ,
                                        vDevice.gateway                             ,
                                        vDevice.subnetMask                          ,
                                        vDevice.dns                                 ]

                delegateColor       :  Colors.transparent
                itemsHasLine        :  Array(itemsText.length).fill(true) // sets all to true
                itemsHasImage       :  Array(itemsText.length).fill(false) // sets all to false
            }
        }

            Column { id: _networkColumn
                anchors {
                    right           : parent.right
                    verticalCenter  : parent.verticalCenter
                }
                width       : parent.width / 2.5
                height      : parent.height

                Rectangle { id: _header
                    width       : parent.width
                    height      : 75
                    color       : Colors.panelBackgroundColor
                    radius      : 9

                    border {
                        width: 1
                        color: Colors.panelBorderColor
                    }

                    Text { id: _title
                        text                : qsTr("Available Networks")
                        color               : Colors.offWhite
                        font.family         : Fonts.fontFamilyFixed
                        font.weight         : Font.DemiBold
                        font.pixelSize      : Fonts.fontPixelTextRectTitle
                        anchors {
                            left            : parent.left
                            leftMargin      : Variables.defaultMargin
                            verticalCenter  : parent.verticalCenter
                        }
                    }

                    IconButton { id: _refreshIcon
                        anchors {
                            right           : parent.right
                            rightMargin     : Variables.defaultMargin
                            verticalCenter  : parent.verticalCenter
                        }
                        iconSize        : Variables.iconsDiameter
                        iconImageSource : enabled ? "qrc:/images/iRefresh" : "qrc:/images/iRefreshDisabled"
                        enabled         : vDevice.wifiListEnabled

                        onPressed       : vDevice.wifiList = ""
                    }
                }

                Item { id: _wifiListItem
                    width               : parent.width
                    height              : _contentRow.height

                    ScrollBar {
                        flickable           : _networkList
                        anchors.fill        : _networkList
                        scrollColor         : Colors.scrollBarColor
                        handleWidth         : 5
                    }

                    ListView    { id        : _networkList
                        model               : vDevice.model
                        clip                : true
                        width               : parent.width
                        height              : parent.height


                        delegate : TouchRect { id : _delegate
                            readonly property color  networkDelegateTextColor   : wifiSupported ? Colors.textMain : Colors.textDisableButton
                            readonly property string postSecurityTypeLabel      : wifiSupported ? "" : " - " + qsTr("Not Supported")
                            readonly property bool   isConnected                : wifiConnected
                            onIsConnectedChanged: {
                                if ( isConnected ) {
                                    vDevice.doInitWifiInfo()
                                }
                            }

                            clip            : true
                            text.text       : wifiSsid
                            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
                            borderColor                     : wifiConnected ? Colors.borderButtonSelected : Colors.borderButton

                            Text { id           : _securityLevel
                                anchors {
                                    left        : parent.left
                                    leftMargin  : 10
                                    bottom      : parent.bottom
                                }
                                font.pixelSize  : Fonts.fontPixelDialogText
                                text            : wifiSecurityTypes + _delegate.postSecurityTypeLabel
                                color           : _delegate.networkDelegateTextColor
                            }

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

                            onClicked           : {
                                if( wifiSupported ) {
                                    _userConfirmation.isPassword    = ! wifiConnected
                                    _userConfirmation.ssid          =   wifiSsid
                                    _userConfirmation.macAddress    =   wifiMacAddress
                                    push( _userConfirmation )
                                    _userConfirmation.setFocus()
                                }
                            }
                        }
                    }
                }
//            }

        }
    }


//    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             : vDevice.wifiListEnabled
//        onClicked           : vDevice.wifiList = ""
//    }




    UserConfirmation { id : _userConfirmation
        property string ssid        : ""
        property string macAddress  : ""
        property string separator   : "\n"
        readonly property string spaceSeparator : " "
        notificationText    : vDevice.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 ) {
                    vDevice.doWifiConnect( true, ssid, passwordCurrent )
                    pop()
                }
            } else {
                vDevice.doWifiConnect( false, ssid, "" )
                pop()
            }
        }
    }
}
