/*!
 *
 * 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    SettingsRootSSHAccess.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      30-Aug-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  18-Jul-2023
 *
 */

// Qt
import QtQuick                  2.12
import QtQuick.Controls         2.12 // Switch


//  Qml imports
import "qrc:/globals"

/*!
 * \brief   SettingsRootSSHAccess is the screen
 *          which enables the Ro Water Input
 */
SettingsBase    { id: _root
    itemIndex       : SettingsStack.RootSSHAccess

    readonly property int diameter          : 85

    confirmVisible  : false

    Column {
        anchors.centerIn: parent
        Row { id: _settingsRootSSHAccess_SSHDRow
            Text                            { id: _settingsRootSSHAccess_SSHDLabel
                text                            : qsTr("Enable SSH Login")
                width                           : 300
                height                          : _settingsRootSSHAccess_SSHDSwitch.height
                color                           : Colors.white
                font.pixelSize                  : Fonts.fontPixelButton
                verticalAlignment               : Text.AlignVCenter
                horizontalAlignment             : Text.AlignLeft
            }

            Switch                          { id: _settingsRootSSHAccess_SSHDSwitch
                property bool active            : true

                width                           : _root.diameter
                height                          : _root.diameter

                indicator                       : Rectangle {
                    implicitWidth               : Variables.sliderCircleDiameter * 1.7
                    implicitHeight              : Variables.sliderCircleDiameter - ( Variables.progressbarHandlerBorderWidth * 2 )
                    radius                      : implicitHeight
                    anchors.centerIn            : parent
                    color                       : _settingsRootSSHAccess_SSHDSwitch.checked ? Colors.backgroundButtonSelect  : Colors.createTreatmentInactive
                    border.color                : _settingsRootSSHAccess_SSHDSwitch.checked ? Colors.borderButton            : Colors.createTreatmentInactive
                    Rectangle               {
                        property real diameter  : Variables.sliderCircleDiameter
                        x                       : _settingsRootSSHAccess_SSHDSwitch.checked ? parent.width - width : 0
                        anchors.verticalCenter  : parent.verticalCenter
                        width                   : diameter
                        height                  : diameter
                        radius                  : diameter
                        color                   : _settingsRootSSHAccess_SSHDSwitch.active ? Colors.highlightProgressBar : Colors.createTreatmentInactive
                        border              {
                            width               : Variables.progressbarHandlerBorderWidth
                            color               : Colors.textMain
                        }
                    }
                }

                contentItem                     : Text {
                    width                       : parent.width
                    height                      : parent.height
                    font.pixelSize              : Fonts.fontPixelButton
                    color                       : _settingsRootSSHAccess_SSHDSwitch.active  ? Colors.textMain : Colors.textDisableButton
                    verticalAlignment           : Text.AlignTop
                    horizontalAlignment         : Text.AlignHCenter
                }
            }
        }

        Row { id: _settingsRootSSHAccess_RootRow
            Text                            { id: _settingsRootSSHAccess_RootLabel
                text                            : qsTr("Enable Root Login")
                width                           : 300
                height                          : _settingsRootSSHAccess_RootSwitch.height
                color                           : Colors.white
                font.pixelSize                  : Fonts.fontPixelButton
                verticalAlignment               : Text.AlignVCenter
                horizontalAlignment             : Text.AlignLeft
            }

            Switch                          { id: _settingsRootSSHAccess_RootSwitch
                property bool active            : _settingsRootSSHAccess_SSHDSwitch.checked
                enabled                         : _settingsRootSSHAccess_RootSwitch.active
                width                           : _root.diameter
                height                          : _root.diameter

                indicator                       : Rectangle {
                    implicitWidth               : Variables.sliderCircleDiameter * 1.7
                    implicitHeight              : Variables.sliderCircleDiameter - ( Variables.progressbarHandlerBorderWidth * 2 )
                    radius                      : implicitHeight
                    anchors.centerIn            : parent
                    color                       : _settingsRootSSHAccess_RootSwitch.checked ? Colors.backgroundButtonSelect  : Colors.createTreatmentInactive
                    border.color                : _settingsRootSSHAccess_RootSwitch.checked ? Colors.borderButton            : Colors.createTreatmentInactive
                    Rectangle               {
                        property real diameter  : Variables.sliderCircleDiameter
                        x                       : _settingsRootSSHAccess_RootSwitch.checked ? parent.width - width : 0
                        anchors.verticalCenter  : parent.verticalCenter
                        width                   : diameter
                        height                  : diameter
                        radius                  : diameter
                        color                   : _settingsRootSSHAccess_RootSwitch.active ? Colors.highlightProgressBar : Colors.createTreatmentInactive
                        border              {
                            width               : Variables.progressbarHandlerBorderWidth
                            color               : Colors.textMain
                        }
                    }
                }

                contentItem                     : Text {
                    width                       : parent.width
                    height                      : parent.height
                    font.pixelSize              : Fonts.fontPixelButton
                    color                       : _settingsRootSSHAccess_RootSwitch.active  ? Colors.textMain : Colors.textDisableButton
                    verticalAlignment           : Text.AlignTop
                    horizontalAlignment         : Text.AlignHCenter
                }
            }
        }
    }

    function toCheckState() {
        let sshd = _settingsRootSSHAccess_SSHDSwitch.checked
        let root = _settingsRootSSHAccess_RootSwitch.checked
        if      ( ! sshd )  return Qt.       Unchecked
        else if ( ! root )  return Qt.PartiallyChecked
        else                return Qt.         Checked
    }

    function fromCheckState( vValue ) {
        let sshd = false
        let root = false
        switch ( vValue ) {
        case Qt.       Unchecked:
            break;
        case Qt.PartiallyChecked:
            sshd = true
            root = false
            break;
        case Qt.         Checked:
            sshd = true
            root = true
            break;
        }
        _settingsRootSSHAccess_SSHDSwitch.checked = sshd
        _settingsRootSSHAccess_RootSwitch.checked = root
    }

    Connections { target: _settingsRootSSHAccess_SSHDSwitch
        function onClicked() {
            vDevice.rootSSHAccess = toCheckState()
        }
    }
    Connections { target: _settingsRootSSHAccess_RootSwitch
        function onClicked() {
            vDevice.rootSSHAccess = toCheckState()
        }
    }
    Connections { target: vDevice
        // in case the value is rejecte it will be set to the previous value
        // also the init value shall be set when navigate to the screen
        function onRootSSHAccessChanged ( vValue ) { fromCheckState         ( vValue )  }
        function onStatusChanged        ( vValue ) { _root.notificationText = vValue    }
    }
}

