/*!
 *
 * 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"
import "qrc:/dialogs"

/*!
 * \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
                leftMargin      : Variables.defaultMargin * 2
                top             : contentItem.top
                bottom          : contentItem.bottom
            }

            width   : parent.width / 2.5

            ReviewContainer { id: _currentWifiInfo
                anchors {
                    top : parent.top
                    left: parent.left
                }
                title               : qsTr("Current Wi-Fi Information")
                width               : parent.width
                cellHeight          : 100
                valuePixelSize      : Fonts.fontPixelTextRectTitle
                color               : Colors.transparent
                headerColor         : Colors.transparent
                border.width        : 0
                delegateFontWeight  : Font.Medium

                label   : [ qsTr("SSID")        ,
                            qsTr("IP Address")  ,
                            qsTr("Gateway")     ,
                            qsTr("Subnet Mask") ,
                            qsTr("DNS")         ]

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

                actual  : [ "" , "" , ""  , ""  , "" ]
                units   : [ "" , "" , ""  , ""  , "" ]
            }
        }

        Rectangle { id: _networkRect
            color       : Colors.panelBackgroundColor
            anchors {
                right           : parent.right
                rightMargin     : Variables.defaultMargin * 2
                top             : contentItem.top
                bottom          : contentItem.bottom
                bottomMargin    : Variables.defaultMargin
            }
            width       : parent.width / 2.5

            Column { id: _networkColumn
                anchors.fill: parent

                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.isWifiScanning

                        onPressed       : vDevice.wifiList = ""


                        RotationAnimator on rotation {
                            running : vDevice.isWifiScanning
                            from    : 0
                            to      : 360
                            loops   : Animation.Infinite
                            duration: 7200
                        }
                    }
                }

                Item { id: _wifiListItem
                    width               : parent.width
                    height              : parent.height - _header.height

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

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

                            clip                            : true
                            text.text                       : wifiSsid
                            text.elide                      : Text.ElideLeft
                            text.color                      : _delegate.networkDelegateTextColor
                            width                           : parent.width
                            height                          : 75
                            radius                          : 0
                            backgroundColor                 : Colors.waterSampleContainer
                            pixelSize                       : Fonts.fontPixelTextRectTitle
                            text.font.weight                : Font.Medium
                            text.anchors.horizontalCenter   : undefined
                            text.anchors.verticalCenter     : undefined

                            text.horizontalAlignment        : Text.AlignLeft
                            text.leftPadding                : _delegate.margin
                            text.topPadding                 : 10
                            text.verticalAlignment          : Text.AlignTop
                            border.width                    : 0

                            Text { id           : _securityLevel
                                anchors {
                                    left        : parent.left
                                    leftMargin  : _delegate.margin
                                    bottom      : parent.bottom
                                    bottomMargin: 5
                                }
                                font.pixelSize  : Fonts.fontPixelDialogText
                                font.weight     : Font.Light
                                text            : wifiSecurityTypes + _delegate.postSecurityTypeLabel
                                color           : _delegate.networkDelegateTextColor
                            }

                            Text { id           : _isConnected
                                anchors {
                                    verticalCenter  : parent.verticalCenter
                                    right           : parent.right
                                    rightMargin     : _delegate.margin
                                }
                                font.pixelSize  : Fonts.fontPixelTextRectExtra
                                font.weight     : Font.Medium
                                text            : wifiConnected ? qsTr("Connected") : ""
                                color           : Colors.statusTextActive
                            }

                            Line { id: _wifiDivider
                                color   : Colors.panelBorderColor
                                visible : index !== _networkList.count - 1
                                anchors {
                                    bottom      : parent.bottom
                                    left        : parent.left
                                    leftMargin  : _delegate.margin
                                    right       : parent.right
                                    rightMargin : _delegate.margin
                                }
                            }

                            onClicked           : {
                                if( wifiSupported ) {
                                    _wifiConfirmDialog.isPassword   = ! wifiConnected
                                    _wifiConfirmDialog.ssid         =   wifiSsid
                                    _wifiConfirmDialog.open()
                                }
                            }
                        }
                    }

                    ScrollBar {
                        flickable           : _networkList
                        anchors.fill        : _networkList
                        scrollColor         : Colors.scrollBarColor
                        handleWidth         : 5
                        visible             : _networkList.count > 0
                    }
                }
            }
        }
    }

    ConfirmPasswordDialog { id: _wifiConfirmDialog

        onAccepted    : {
            if ( isPassword ) {
                if(  passwordLength > 0 ) {
                    vDevice.doWifiConnect( true, ssid, password )
                }
            } else {
                vDevice.doWifiConnect( false, ssid, "" )
            }
        }
    }

}
