/*!
 *
 * Copyright (c) 2019-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    MainMenu.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      18-Jul-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  17-Oct-2019
 *
 */

// Qt
import QtQuick 2.12

// Project
//  Qml imports
import "qrc:/globals"

/*!
 * \brief   The MainMenu Component
 * which contains three [Treatment, Manager, Settings]
 */
Rectangle { id: _root

    property alias          index           : _listView.currentIndex
    property bool           hidden          : false
    property bool           disable         : false
    property int            titlePixelSize  : 40
    property color          backgroundColor : Colors.backgroundMainMenu
    property int            highlightHeight : 10
    property bool           isMainMenu      : false
    property bool           isMainTreatment : false
    readonly property int   currentScreen   : isMainTreatment ? _mainTreatmentModel.get(index).screen : 0
    property color          statusColor     : Colors.transparent

    width                                   : parent.width
    height                                  : Variables.mainMenuHeight
    color                                   : _root.backgroundColor
    clip                                    : true

    enum TreatmentScreen {
        Treatment   = 0,
        Trends      = 1,
        Heparin     = 2,
        HDF         = 3
    }

    gradient: Gradient {
        GradientStop { position: 0.2; color: Qt.lighter(Colors.backgroundMain, 1.2) }
        GradientStop { position: 0.8; color: Qt.darker (Colors.backgroundMain, 1.2) }
    }

    signal itemPressed(int vIndex)

    ListModel { id: _mainTreatmentModel
        ListElement {   text: qsTr("Treatment");    visible: true;      screen:  0                                  }   // MainMenu.Treatment
        ListElement {   text: qsTr("Trends");       visible: true;      screen:  1                                  }   // MainMenu.Trends
        ListElement {   text: qsTr("Heparin");      visible: true; /*TODO: vSettings.heparinSyringePump */   screen: 2   }   // MainMenu.Heparin
        ListElement {   text: qsTr("HDF");          visible: true;      screen:  3                                  }   // MainMenu.HDF
    }

    ListModel { id: _mainMenuModel
        ListElement {   text: qsTr("Treatment")     ; visible: true   }
        ListElement {   text: qsTr("Prescriptions") ; visible: true   }
        ListElement {   text: qsTr("Settings")      ; visible: true   }
    }

    ListView { id: _listView
        property alias  index                   : _listView.currentIndex

        anchors.fill                            : parent
        anchors.leftMargin                      : 30
        anchors.rightMargin                     : anchors.leftMargin
        model                                   : isMainMenu        ? _mainMenuModel        :
                                                  isMainTreatment   ? _mainTreatmentModel   :
                                                                      null
        currentIndex                            : 0
        highlightFollowsCurrentItem             : true
        highlightMoveDuration                   : 500
        highlightMoveVelocity                   : -1
        orientation                             : ListView.Horizontal
        interactive                             : false

        onCurrentIndexChanged: itemPressed(currentIndex)

        highlight: Rectangle { id: _highlightRectangle
            objectName: "_highlightRectangle"
            color: "transparent"

            Rectangle {
                id: _highlightedItem
                objectName: "highlightedItem"
                anchors {
                    horizontalCenter: parent.horizontalCenter
                    bottom: parent.bottom
                    bottomMargin: (height / 2) * -1
                }
                height  : _root.highlightHeight
                width   : parent.width / 2
                radius  : 10
                color   : Colors.backgroundButtonSelect
            }
        }

        delegate: Item { id: _delegateControl
            objectName  : "delegateControl"
            width       : ListView.view.count ? model.visible ? ListView.view.width / ListView.view.count : 0 : 0
            height      : ListView.view.height
            visible     : model.visible

            TouchRect { id : _touchRect
                objectName: "_touchRect" + index
                animated    : false
                anchors.fill: parent
                text.text   : model.text
                border.width: 0
                pixelSize   : titlePixelSize

                Rectangle { id: _heparinStatus
                    anchors {
                        left        : parent.left
                        leftMargin  : Variables.defaultMargin * 2
                        top         : parent.top
                        topMargin   : 10
                    }

                    height  : 15
                    width   : height
                    radius  : height
                    visible : _root.isMainTreatment && model.screen === MainMenu.Heparin
                    color   : _root.statusColor
                }

                onPressed: {
                    if (_listView.currentIndex !== index) {
                        _listView.currentIndex = index
                    }
                }
            }
        }
    }
    Behavior on anchors.bottomMargin { PropertyAnimation { } }
}
