/*!
 *
 * Copyright (c) 2021-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    SettingsStack.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      18-Jul-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  16-Apr-2021
 *
 */

// Qt
import QtQuick                  2.12

// Project
import Gui.Actions 0.1

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

/*!
 * \brief   SettingsStack is the screen
 * which contains the main stack for all the Settings screens
 * This is the screen which will show up when "Settings" option
 * is selected from the main menu.
 */
StackItem { id : _root
    objectName: "_SettingsStack"

    stackView.initialItem   : _settingsHome

    property bool   serviceMode : false

    enum ItemsIndex {
        Information         ,
        VolumeBrightness    ,
        WiFi                ,
        Bluetooth           ,
        DGCleaning          ,
        DGScheduling        ,
        ServicePassword     ,
        SetDateTime         ,
        ExportLogs          ,
        Language            ,
        RoInput             ,
        Calibration         ,
        DeviceConfiguration ,
        DeviceRegistration  ,
        SWUpdate            ,
        RootSSHAccess       ,
        FactoryReset        ,
        Decommission
    }
    property var itemsText  : [
        qsTr("Information"                  ), // Information
        qsTr("Volume And Brightness"        ), // VolumeBrightness
        qsTr("Wi-Fi"                        ), // WiFi
        qsTr("Bluetooth Cuff"               ), // Bluetooth
        qsTr("DG Cleaning"                  ), // DGCleaning
        qsTr("DG Scheduling"                ), // DGScheduling
        qsTr("Service"                      ), // servicePassword
        qsTr("Set Date And Time"            ), // SetDateTime
        qsTr("Export Logs"                  ), // ExportLogs
        qsTr("Set Language"                 ), // Language
        qsTr("Water Input Mode"             ), // RoInput
        qsTr("Calibration"                  ), // Calibration
        qsTr("Device Configuration"         ), // Device Configuration
        qsTr("Device Registration"          ), // DeviceRegistration
        qsTr("Software Update"              ), // SWUpdate
        qsTr("Enable Root SSH"              ), // RootSSHAccess
        qsTr("Factory Reset"                ), // FactoryReset
        qsTr("Decommissioning"              ), // Decommission
    ]
    property var itemsEnabled  : [
        true                                    , // Information
        true                                    , // VolumeBrightness
        true                                    , // WiFi
        true                                    , // Bluetooth
        true                                    , // DGCleaning
        true                                    , // DGScheduling
        true                                    , // ServicePassword
        true                                    , // SetDateTime
        true                                    , // ExportLogs
        false                                   , // Language
        true                                    , // RoInput
        false                                   , // Calibration
        _GuiView.manufactMode                   , // Device Configuration
        true                                    , // DeviceRegistration
        false                                   , // SWUpdate
        serviceMode                             , // RootSSHAccess
        serviceMode                             , // FactoryReset
        serviceMode                             , // Decommission
    ]
    property var itemsVisible  : [
        true                                    , // Information
        true                                    , // VolumeBrightness
        true                                    , // WiFi
        true                                    , // Bluetooth
      ! _GuiView.manufactSetup                  , // DGCleaning
        false /* serviceMode phase 1B */        , // DGScheduling
      ! serviceMode && ! _GuiView.manufactSetup , // ServicePassword
        serviceMode                             , // SetDateTime
        true                                    , // ExportLogs
        false /* serviceMode phase 1 */         , // Language
      ! _GuiView.manufactSetup                  , // RoInput
        false /* serviceMode phase 1 */         , // Calibration
        _GuiView.manufactMode && serviceMode    , // Device Configuration  // && serviceMode added to make sure the service mode is confirmed by HD
        _GuiView.manufactMode && serviceMode    , // DeviceRegistration    // && serviceMode added to make sure the service mode is confirmed by HD
        false /* serviceMode phase 1 */         , // SWUpdate
        serviceMode                             , // RootSSHAccess
        serviceMode                             , // FactoryReset
        serviceMode                             , // Decommission
    ]

    SettingsHome { id   : _settingsHome
        title           : _GuiView.manufactSetup ? qsTr("Manufacturing Setup") : serviceMode ? qsTr("Service") : qsTr("Device Settings")
        backVisible     : false
        confirmVisible  : serviceMode
        confirmText.text: _GuiView.manufactSetup ? qsTr("QUIT") : qsTr("SHUTDOWN")
        onConfirmClicked: {
            if ( _GuiView.manufactSetup ) {
                _GuiView.doQuitApplication()
            }
            else {
                _GuiView.doActionTransmit(GuiActions.ID_PowerOff, GuiActions.NoData)
            }
        }
        itemsText       : _root.itemsText
        itemsEnabled    : _root.itemsEnabled
        itemsVisible    : _root.itemsVisible
        onItemClicked   : {
            switch (vIndex) {
            case SettingsStack.Information:
                push(_settingsInformation)
                break

            case SettingsStack.VolumeBrightness:
                vDevice.doInitBrightness()
                push(_settingsVolumeBrightness)
                break

            case SettingsStack.WiFi:
                vNetwork.doInit()
                push( _settingsWiFi )
                // _settingsWiFi.setFocus() // No keyboard popup.
                break

            case SettingsStack.Bluetooth:
                push( _settingsBluetooth )
                break

            case SettingsStack.DGCleaning:
                push( _settingsDGCleaning )
                break

            case SettingsStack.DGScheduling:
                push( _settingsDGScheduling )
                break

            case SettingsStack.ServicePassword:
                push(_servicePassword)
                _servicePassword.setFocus()
                break

            case SettingsStack.SetDateTime:
                vDateTime.doInit()
                push( _settingsDateTime )
                _settingsDateTime.setFocus()
                break

            case SettingsStack.ExportLogs:
                push( _settingsExportLogs )
                break

            case SettingsStack.RoInput:
                push( _settingsRoInput )
                break

            case SettingsStack.DeviceConfiguration:
                push( _settingsDeviceConfiguration )
                break

            case SettingsStack.DeviceRegistration:
                push( _settingsDeviceRegistration )
                break

            case SettingsStack.RootSSHAccess:
                vDevice.doInitRootSSHAccess()
                push( _SettingsRootSSHAccess)
                break

            case SettingsStack.FactoryReset:
                push( _serviceFactoryReset )
                break

            case SettingsStack.Decommission:
                push( _serviceDecommission )
                break

            default:
                console.debug("Unknown Index", vIndex)
                break
            }
        }
    }

    SettingsInformation         { id: _settingsInformation          }
    SettingsVolumeBrightness    { id: _settingsVolumeBrightness     }
    SettingsWiFi                { id: _settingsWiFi                 }
    SettingsBluetoothCuff       { id: _settingsBluetooth            }
    SettingsDGCleaning          { id: _settingsDGCleaning           }
    SettingsDGScheduling        { id: _settingsDGScheduling         }
    SettingsServicePassword     { id: _servicePassword              }
    SettingsDateTime            { id: _settingsDateTime             }
    SettingsExportLogs          { id: _settingsExportLogs           }
    SettingsROInput             { id: _settingsRoInput              }
    SettingsManufacturingSetup  { id: _settingsDeviceConfiguration  }
    SettingsDeviceRegistration  { id: _settingsDeviceRegistration   }
    SettingsRootSSHAccess       { id: _SettingsRootSSHAccess        }
    SettingsFactoryReset        { id: _serviceFactoryReset          }
    SettingsDecommission        { id: _serviceDecommission          }

    function gotoServiceMode( vservice ) {
        //DEBUG console.log (" 0 ---------- ", _GuiView.manufactMode, _GuiView.manufactSetup, vservice, stackView.initialItem, stackView.currentItem, stackView.depth)
        if ( ! _servicePassword.isPassword_Accepted ) return
        serviceMode      = vservice
        if ( vservice )
            _mainMenu.hidden = true
        else
            if ( _settingsHome.visible )
                _mainMenu.hidden = false
    }

    Connections { target: vAdjustmentServiceMode
        function onAdjustmentTriggered              ( vValue ) {
            if ( vAdjustmentServiceMode.adjustment_Accepted ) {
                _settingsHome.notificationText = ""
            }
            else {
                _settingsHome.notificationText = vAdjustmentServiceMode.text()
            }
        }
    }

    Connections { target: vHDOperationMode
        function onServiceChanged                   ( vValue ) { gotoServiceMode(                         vValue  )}
        function onStandbyWaitDisinfectChanged      ( vValue ) { page( _settingsHome                    , vValue  )}
    }

    Connections { target: _GuiView
        function onManufactModeChanged( vPass ) {
            if ( _GuiView.manufactMode )  {
                vSettings.checkServicePasswordSet()
                stackView.initialItem =  _servicePassword
            }
            else {
                stackView.initialItem = _settingsHome
            }
            push(stackView.initialItem)

            //DEBUG console.log (" 1 ---------- ", _GuiView.manufactMode, _GuiView.manufactSetup, vPass, stackView.initialItem, stackView.currentItem, stackView.depth)
            if ( _GuiView.manufactSetup ) {
                _settingsHome.notificationText = vPass ? "" : qsTr("Application POST Failed")
            }
        }
    }

    onVisibleChanged: {
        if (visible) {
            _mainMenu.hidden = serviceMode || _GuiView.manufactSetup
        }
        else {
            stackView.initialItem = null
        }
    }
}
