import QtQuick 2.12

import "qrc:/components"
import "qrc:/globals"

Rectangle { id: _root

    property alias menuHidden               : _headerMenu.hidden
    property alias headerMenuIndex          : _headerMenu.index
    property alias titleText                : _title.text
    property alias loggedUser               : _loggedInUser.text
    readonly property int currentScreen     : _headerMenu.currentScreen
    property alias statusColor              : _headerMenu.statusColor

    width   : Variables.applicationWidth
    height  : Variables.headerHeight
    clip    : true

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

    function vitalsRecorded() { _vitalsButton.pulseAnimation.start() }

    MouseArea { id: _mouseArea
        anchors.fill    : parent
        // TODO: disable this later. this is only for diagnostic purpose.
        onDoubleClicked : {
            if ( _GuiView.dryDemoMode ) {
                let dryDemoTempID = 99
                let id = vConfirm.id
                vConfirm.id = dryDemoTempID
                vConfirm.doConfirm( true  )
                vConfirm.id = id
            }
            else {
                _diagnosticsDialog.open()
            }
        }
    }

    Text { id: _loggedInUser
        color                   : Colors.textMain
        anchors.top             : parent.top
        anchors.horizontalCenter: parent.horizontalCenter
        horizontalAlignment     : Text.AlignHCenter
        verticalAlignment       : Text.AlignVCenter
        font.pixelSize          : Fonts.fontHeaderbarUser
    }

    Text { id: _title
        color               : Colors.textMain
        anchors.centerIn    : parent
        horizontalAlignment : Text.AlignHCenter
        verticalAlignment   : Text.AlignVCenter
        font.pixelSize      : Fonts.fontHeaderbarTitle
        font.weight         : Font.DemiBold

    }

    Item { id: _dateTimeItem
        width   : Variables.headerBarDateTimeWidth
        anchors {
            top     : parent.top
            left    : parent.left
            bottom  : parent.bottom
        }

        Column { id: _dateTimeColumn
            anchors.fill: parent

            Text { id: _timeText
                color   : Colors.textMain
                width   : parent.width
                height  : parent.height * 3/5
                horizontalAlignment : Text.AlignHCenter
                verticalAlignment   : Text.AlignVCenter
                text            : vDateTime.time
                font.pixelSize  : Fonts.fontHeaderbarTime
                font.weight     : Font.DemiBold

            }

            Text { id: _dateText
                color   : Colors.textMain
                width   : parent.width
                height  : parent.height * 2/5
                horizontalAlignment : Text.AlignHCenter
                verticalAlignment   : Text.AlignVCenter
                text    : vDateTime.date
                font.pixelSize: Fonts.fontHeaderbarDate
            }
        }
    }

    Text { id: _timeZone    // TEST : Current timezone
        color   : Colors.textMain
        anchors {
            top         : parent.top
            left        : parent.left
            leftMargin  : parent.width / 5
        }
        horizontalAlignment : Text.Alignleft
        verticalAlignment   : Text.AlignBottom

        height  : 15
        width   : _root.width / 5
        text    : vDateTime.timezone
        font.pixelSize: Fonts.fontHeaderbarTimezone
    }

    MainMenu { id: _headerMenu
        anchors {
            left        : _dateTimeItem.right
            bottom      : parent.bottom
            bottomMargin: hidden || disable ? height * -1 : 0
        }
        height  : parent.height
        width   : parent.width / 2 // gives the width of the headerbar menu
        hidden          : true
        titlePixelSize  : 32
        backgroundColor : Colors.transparent
        highlightHeight : 15
        isMainTreatment : true
        onHiddenChanged: { if (hidden) { index = 0 } }
    }

    Row { id: _headerButtonRow
        height      : parent.height
        spacing     : 25
        anchors {
            verticalCenter: parent.verticalCenter
            right         : parent.right
            rightMargin   :  _headerButtonRow.spacing
        }

        VitalsButton { id  : _vitalsButton
            extraSpace      : _headerButtonRow.spacing
            visible         : vTDOpMode.preTreatment        ||
                              vTDOpMode.inTreatment         ||
                              vTDOpMode.postTreatment       ||
                              vTDOpMode.validateParameters

            onPressed       : _treatmentAdjustmentVitals.open()
        }

        IconButton { id  : _prescriptionButton
            iconSize        : Variables.headerIconDiameter
            iconImageSource : "qrc:/images/iPrescription"
            extraSpace      : _headerButtonRow.spacing
            visible         : vTDOpMode.preTreatment        ||
                              vTDOpMode.inTreatment         ||
                              vTDOpMode.postTreatment

            onPressed       : _headerbarPrescription.open()
        }

        IconButton { id  : _wifiButton
            iconSize        : Variables.headerIconDiameter
            iconImageSource : "qrc:/images/iWifi"
            extraSpace      : _headerButtonRow.spacing

            onPressed       : _headerbarWifi.openDialog(_wifiButton)
        }

        IconButton { id  : _cloudSyncButton
            iconSize        : Variables.headerIconDiameter
            iconImageSource : "qrc:/images/iCloudSync"
            extraSpace      : _headerButtonRow.spacing
            enabled         : false

            onPressed       : print("CloudSync button pressed!")
        }

        IconButton { id  : _storageButton
            iconSize        : Variables.headerIconDiameter
            iconImageSource : "qrc:/images/iStorage"
            extraSpace      : _headerButtonRow.spacing

            onPressed       : _headerbarStorage.openDialog(_storageButton)
        }

        IconButton { id  : _settingsButton
            iconSize        : Variables.headerIconDiameter
            iconImageSource : "qrc:/images/iSettings"
            extraSpace      : _headerButtonRow.spacing

            onPressed       : _headerbarSettings.openDialog(_settingsButton)
        }

        IconButton { id  : _informationButton
            iconSize        : Variables.headerIconDiameter
            iconImageSource : "qrc:/images/iInformation"
            extraSpace      : _headerButtonRow.spacing

            onPressed       : _headerbarInformation.openDialog(_informationButton)
        }
    }
}

