/*!
 *
 * Copyright (c) 2023-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    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


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

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

    readonly property int diameter          : 85

    confirmVisible  : false

    contentItem: Column {
        spacing : 25

        LabelUnitContainer { id: _settingsRootSSHAccess_SSHDRow
            anchors.horizontalCenter        : parent.horizontalCenter
            width                           : Variables.adjustmentLabelUnitContainerWidth
            height                          : Variables.adjustmentLabelUnitContainerHeight
            text                            : qsTr("Enable SSH Login")
            contentArea.anchors.leftMargin  : width * 0.75

            contentItem:  BaseSwitch { id: _settingsRootSSHAccess_SSHDSwitch
                onClicked: {
                    vDevice.rootSSHAccess = toCheckState()
                }
            }
        }

        LabelUnitContainer { id: _settingsRootSSHAccess_RootRow
            anchors.horizontalCenter        : parent.horizontalCenter
            width                           : Variables.adjustmentLabelUnitContainerWidth
            height                          : Variables.adjustmentLabelUnitContainerHeight
            text                            : qsTr("Enable Root Login")
            contentArea.anchors.leftMargin  : width * 0.75

            contentItem: BaseSwitch { id: _settingsRootSSHAccess_RootSwitch
                active              : _settingsRootSSHAccess_SSHDSwitch.checked
                enabled             : _settingsRootSSHAccess_RootSwitch.active
                onClicked: {
                    vDevice.rootSSHAccess = toCheckState()
                }
            }
        }
    }

    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: 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    }
    }
}

