/*!
 *
 * Copyright (c) 2021-2022 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)      23-May-2021
 * \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"

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

    labelWidth  : 275
    entryWidth  : 100

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

    confirmEnabled  : isValid
    firstFocusInput : _hours
    notificationText: vDateTime.status
    onConfirmClicked: {
        vDateTime.doConfirm(
                    _year   .textInput.text ,
                    _month  .textInput.text ,
                    _day    .textInput.text ,
                    _hours  .textInput.text ,
                    _minutes.textInput.text )
    }

    onVisibleChanged: { // it seems entering in the TextInput by keyboard breaks the binding
        _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
    }

    Column { id: _container
        spacing: 70
        y : Qt.inputMethod.visible && _keyboard.visible ? topMarginContent : ( (_root.height - _container.height) / 2 )
        Behavior on y { NumberAnimation { duration: Variables.keybardAnimationDuration } }
        anchors.horizontalCenter: parent.horizontalCenter

        Row { // time
            spacing: 5
            Text { id: _timeLabel
                width: labelWidth
                text: qsTr("Time (HH:MM)")
                color: Colors.textMain
                font.pixelSize: Fonts.fontPixelTextRectExtra
            }

            TextEntry { id      : _hours
                nextInput       : _minutes
                label.visible   : false
                width           : entryWidth
                validator       : IntValidator {
                    bottom      : 0
                    top         : 23
                }
            }

            Text { id           : _colon
                text            : qsTr(":")
                color           : Colors.textMain
                font.pixelSize  : Fonts.fontPixelTextRectExtra
            }

            TextEntry { id      : _minutes
                nextInput       : _month
                label.visible   : false
                width           : entryWidth
                validator       : IntValidator {
                    bottom      : 0
                    top         : 59
                }
            }
        }

        Row { // date
            spacing: 5
            Text { id           : _dateLabel
                width           : labelWidth
                text            : qsTr("Date (MM/DD/YYYY)")
                color           : Colors.textMain
                font.pixelSize  : Fonts.fontPixelTextRectExtra
            }

            TextEntry { id      : _month
                nextInput       : _day
                label.visible   : false
                width           : entryWidth
                validator       : IntValidator {
                    bottom      : 1
                    top         : 12
                }
            }

            Text { id           : _slashMonthDay
                text            : qsTr("/")
                color           : Colors.textMain
                font.pixelSize  : Fonts.fontPixelTextRectExtra
            }

            TextEntry { id      : _day
                nextInput       : _year
                label.visible   : false
                width           : entryWidth
                validator       : IntValidator {
                    bottom      : 1
                    top         : 31
                }
            }

            Text { id           : _slashDayYear
                text            : qsTr("/")
                color           : Colors.textMain
                font.pixelSize  : Fonts.fontPixelTextRectExtra
            }

            TextEntry { id      : _year
                label.visible   : false
                width           : entryWidth
                validator       : IntValidator {
                    bottom      : 1970
                    top         : 2100 // seems date command is not accepting more than 2100
                }
            }
        }
    }
}
