/*!
 *
 * Copyright (c) 2023-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    SettingsDeviceRegistration.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      31-Aug-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  05-Apr-2023
 *
 */

// Qt
import QtQuick 2.12

// Project
import Gui.Actions 0.1

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

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

    confirmVisible  : false
    labelWidth      : 150
    entryWidth      : 350

    readonly property int       spacing         : 20
    readonly property int       leftMargin      : 100
    readonly property int       rightMargin     : 150
    readonly property int       topMargin       : topMarginContent
    readonly property int       separatorGap    : 40
    readonly property string    separatorText   : ":"

    property alias              isRegistered    : _checkListView.completeVisible
    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]))$/
    }

    onVisibleChanged: {
        if ( visible ) vAdjustmentVersions     .doAdjustment()
    }

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

        TextEntry { id      : _version_UI
            textInput.clip  : true
            hasCursor       : false
            textInput.text  : Qt                    .application.version
            textInput.horizontalAlignment: Text.AlignLeft
            label.text      : qsTr("UI Version")
            separator.text  : _root.separatorText
            separator.width : _root.separatorGap
            textInput.width : _root.entryWidth
            label.width     : _root.labelWidth
        }

        TextEntry { id      : _serial_HD
            textInput.clip  : true
            hasCursor       : false
            textInput.text  : vAdjustmentVersions     .hdSerial
            textInput.horizontalAlignment: Text.AlignLeft
            label.text      : qsTr("HD Serial")
            separator.text  : _root.separatorText
            separator.width : _root.separatorGap
            textInput.width : _root.entryWidth
            label.width     : _root.labelWidth
        }

        TextEntry { id      : _serial_DG
            textInput.clip  : true
            hasCursor       : false
            textInput.text  : vAdjustmentVersions     .dgSerial
            textInput.horizontalAlignment: Text.AlignLeft
            label.text      : qsTr("DG Serial")
            separator.text  : _root.separatorText
            separator.width : _root.separatorGap
            textInput.width : _root.entryWidth
            label.width     : _root.labelWidth
        }

        TextEntry { id      : _ipAddress_wlan0
            textInput.clip  : true
            hasCursor       : false
            textInput.text  : vNetwork.wirelessIP
            textInput.horizontalAlignment: Text.AlignLeft
            label.text      : qsTr("Wireless")
            separator.text  : _root.separatorText
            separator.width : _root.separatorGap
            textInput.width : _root.entryWidth
            label.width     : _root.labelWidth
            validator       : _root.ipValidator
        }

        TextEntry { id      : _ipAddress_eth0
            textInput.clip  : true
            hasCursor       : false
            textInput.text  : vNetwork.ethernetIP
            textInput.horizontalAlignment: Text.AlignLeft
            label.text      : qsTr("Ethernet")
            separator.text  : _root.separatorText
            separator.width : _root.separatorGap
            textInput.width : _root.entryWidth
            label.width     : _root.labelWidth
            validator       : _root.ipValidator
        }
    }

    CheckListView { id: _checkListView
        y                   : _root.topMargin
        anchors.right       : parent.right
        anchors.rightMargin : _root.rightMargin
        completeVisible     : vCloudSync.isReady
        completeText        : qsTr("Registration complete")
        stepNames           : [
            qsTr("Cloud Service is running" ),
            qsTr("Registering the device"   )
        ]
    }

    TouchRect { id              : _scanButton
        anchors.bottom          : parent.bottom
        anchors.bottomMargin    : Variables.mainMenuHeight * 2 + Variables.minVGap * 2
        anchors.horizontalCenter: parent.horizontalCenter
        text.text               : qsTr("START")
        width                   : 350
        isDefault               : true
        enabled                 : true // ! device device registered
        onClicked               : {
            let mHasNetworkConnection   = (_ipAddress_eth0.isValid || _ipAddress_wlan0.isValid)
            if ( ! mHasNetworkConnection) { _root.notificationText = qsTr("No Network Connection"); return }

            let mHasSerials             = ( _serial_HD != "" && _serial_DG != "")
            if ( ! mHasSerials          ) { _root.notificationText = qsTr("No device Serial"     ); return }
            vCloudSync              .doRegister  ()
        } // start device registration
    }

    Connections { target: vCloudSync
        function onisRunningEntered         ( vValue ) { _checkListView.checkList.setItemExt( 0, true         )}
        function onisRegisterStartEntered   ( vValue ) { _checkListView.checkList.setItemExt( 1, true         )}
        function onisRegisterDoneEntered    ( vValue ) { _checkListView.checkList.setItemExt( 1, vValue       )}
    }

    notificationText: ""
}
