/*!
 *
 * 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    SettingsDateTime.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:/globals"
import "qrc:/components"
import "qrc:/compounds"

/*!
 * \brief   SettingsDateTimeSet is used to adjust the
 * date and time on the device
 */
SettingsBase    { id: _root
    itemIndex       : SettingsStack.SetDateTime

    labelWidth  : 275
    entryWidth  : 100

    readonly property bool  isValid     :
        _year   .textInput.acceptableInput &&
        _month  .textInput.acceptableInput &&
        _day    .textInput.acceptableInput &&
        _hours  .textInput.acceptableInput &&
        _minutes.textInput.acceptableInput

    readonly property color entryTextColor : _ntpSwitch.checked ? Colors.borderDisableButton : Colors.textMain

    confirmEnabled  : isValid
    notificationText: vDateTime.status
    onConfirmClicked: {
        vDevice.doDateTime(
                    _year   .textInput.text ,
                    _month  .textInput.text ,
                    _day    .textInput.text ,
                    _hours  .textInput.text ,
                    _minutes.textInput.text ,
                    _ntpSwitch.checked      )
    }

    onVisibleChanged: { // it seems entering in the TextInput by keyboard breaks the binding
        refreshDateTime()
    }

    function refreshDateTime() {
        _year   .textInput.text = vDateTime.year
        _month  .textInput.text = vDateTime.month
        _day    .textInput.text = vDateTime.day
        _hours  .textInput.text = vDateTime.hour
        _minutes.textInput.text = vDateTime.minute
    }

    Connections { target: vDevice
        //  ... WILL BE UPDATE IN LDT-2909
        // send fw date/time after succesfully settings it in shell script
        function onDateTimeChanged  ( vValue ) { vDateTime.doConfirm(
                                                    _year   .textInput.text ,
                                                    _month  .textInput.text ,
                                                    _day    .textInput.text ,
                                                    _hours  .textInput.text ,
                                                    _minutes.textInput.text ,
                                                    _ntpSwitch.checked      ) }
    }

    Connections { target: vDateTime
        function onDidRefreshUIFields ( ) { refreshDateTime()  }

    }

    contentItem: Column { id: _container
        spacing : 25

        LabelUnitContainer { id: _ntpSettings
            anchors.horizontalCenter        : parent.horizontalCenter
            width                           : Variables.adjustmentLabelUnitContainerWidth
            height                          : Variables.adjustmentLabelUnitContainerHeight
            unitText                        : qsTr("Network Time Protocol")
            text                            : qsTr("NTP")
            contentArea.anchors.leftMargin  : width * 0.75

            contentItem: BaseSwitch { id: _ntpSwitch
                checked : vDateTime.ntp
            }
        }

        LabelUnitContainer { id: _timeContainer
            anchors.horizontalCenter        : parent.horizontalCenter
            width                           : Variables.adjustmentLabelUnitContainerWidth
            height                          : Variables.adjustmentLabelUnitContainerHeight
            text                            : qsTr("Time")
            unitText                        : vDateTime.timeFormat
            enabled                         : ! _ntpSwitch.checked

            contentItem: Row { // time
                anchors.centerIn: parent

                TextEntry { id: _hours
                    nextInput           : _minutes
                    line.visible        : false
                    width               : entryWidth
                    showPlaceHolderText : true
                    showDisabledColor   : true
                    validator           : IntValidator {
                        bottom  : 0
                        top     : 23
                    }
                    textInput.font.pixelSize: Fonts.fontPixelValueControl
                }

                Label {
                    text    : qsTr(":")
                    color   : _root.entryTextColor
                    width   : 10
                }

                TextEntry { id: _minutes
                    nextInput           : _month
                    line.visible        : false
                    width               : entryWidth
                    showPlaceHolderText : true
                    showDisabledColor   : true
                    validator           : IntValidator {
                        bottom  : 0
                        top     : 59
                    }
                    textInput.font.pixelSize: Fonts.fontPixelValueControl
                }
            }
        }

        LabelUnitContainer { id: _dateContainer
            anchors.horizontalCenter        : parent.horizontalCenter
            width                           : Variables.adjustmentLabelUnitContainerWidth
            height                          : Variables.adjustmentLabelUnitContainerHeight
            text                            : qsTr("Date")
            unitText                        : vDateTime.dateFormat
            enabled                         : ! _ntpSwitch.checked

            contentItem: Row { // time
                anchors.centerIn: parent

                TextEntry { id: _month
                    nextInput           : _day
                    line.visible        : false
                    width               : entryWidth
                    showPlaceHolderText : true
                    showDisabledColor   : true
                    validator           : IntValidator {
                        bottom      : 1
                        top         : 12
                    }
                    textInput.font.pixelSize: Fonts.fontPixelValueControl
                }

                Label { id  : _slashMonthDay
                    text    : qsTr("/")
                    color   : _root.entryTextColor
                    width   : 5
                }

                TextEntry { id: _day
                    nextInput           : _year
                    line.visible        : false
                    width               : entryWidth
                    showPlaceHolderText : true
                    showDisabledColor   : true
                    validator           : IntValidator {
                        bottom      : 1
                        top         : 31
                    }
                    textInput.font.pixelSize: Fonts.fontPixelValueControl
                }

                Label { id  : _slashDayYear
                    text    : qsTr("/")
                    color   : _root.entryTextColor
                    width   : 5
                }

                TextEntry { id: _year
                    line.visible        : false
                    width               : entryWidth
                    showPlaceHolderText : true
                    showDisabledColor   : true
                    validator           : IntValidator {
                        bottom      : 1970
                        top         : 2100 // seems date command is not accepting more than 2100
                    }
                    textInput.font.pixelSize: Fonts.fontPixelValueControl
                }
            }
        }
    }
}
