/*!
 *
 * Copyright (c) 2019-2020 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 TreatmentHome.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      16-Apr-2021
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  27-Jan-2020
 *
 */

// Qt
import QtQuick 2.12

// Project

//  Qml imports
import "qrc:/globals"
import "qrc:/components"
import "qrc:/pages/treatment/sections"

/*!
 * \brief   ManagerHome is the screen
 * which is the default screen in the "Manager" stack
 */
ScreenItem { id: _root
    property int          bloodFlow_MeasuredFlow_Precision: 0
    property int dialysateInletFlow_MeasuredFlow_Precision: 0

    property int areaWidth      : 400
    property int areaHeightRow1 : 170
    property int areaHeightRow2 : 225
    property int areaHeightRow3 : 200

    property int leftColumnX    :  40
    property int rightColumnX   : Variables.applicationWidth - leftColumnX - areaWidth

    property int row1Y          : 100
    property int row2Y          : 290 // 320
    property int row3Y          : 540 // 570

    property int lineLength     : 450
    property int leftLinesX     :   0
    property int rightLinesX    : Variables.applicationWidth - lineLength
    property int row1LineY      : 275
    property int row2LineY      : 525

    signal backClicked()

    signal sectionFlowClicked()
    signal sectionVitalsClicked()
    signal sectionPressuresClicked()
    signal sectionTimeClicked()
    signal sectionUltrafiltrationClicked()

    // ---------- COLUMN LEFT
    TreatmentFlows              { id: _flowsTouchArea
        x       : leftColumnX
        y       : row1Y
        width   : areaWidth
        height  : areaHeightRow1
        onClicked: {
            sectionFlowClicked()
        }
    }
    Line                        { x: 0; y: row1LineY; length: lineLength }
    TreatmentVitals             { id: _vitalsTouchArea
        x       : leftColumnX
        y       : row2Y
        width   : areaWidth
        height  : areaHeightRow2
        onClicked: {
            sectionVitalsClicked()
        }
    }
    Line                        { x: 0; y: row2LineY; length: lineLength }
    TreatmentPressures          { id: _pressuresTouchArea
        arterialMinimum     : Variables.arterialValueMin
        arterialMaximum     : Variables.arterialValueMax
        venousMinimum       : Variables.venousValueMin
        venousMaximum       : Variables.venousValueMax

        arterilPressure     : vTreatmentPressureOcclusion.arterialPressure
        venousPressure      : vTreatmentPressureOcclusion.venousPressure

        x                   : leftColumnX
        y                   : row3Y
        width               : areaWidth
        height              : areaHeightRow3

        onClicked: {
            sectionPressuresClicked()
        }
    }

    // ---------- COLUMN RIGHT
    TreatmentUltrafiltration    { id: _ultrafiltrationTouchArea;
        x           : rightColumnX
        y           : row1Y
        width       : areaWidth
        height      : areaHeightRow1
        onClicked   : {
            sectionUltrafiltrationClicked()
        }
    }
    Line                        { x: rightLinesX; y: row1LineY; length: lineLength }
    TreatmentSaline             { id: _salineTouchArea
        x           : rightColumnX
        y           : row2Y
        width       : areaWidth
        height      : areaHeightRow2
    }
    Line                        { x: rightLinesX; y: row2LineY; length: lineLength; }
    TreatmentHeparin            { id: _heparinTouchArea
        x           : rightColumnX
        y           : row3Y
        width       : areaWidth
        height      : areaHeightRow3
    }

    // ---------- Center
    TreatmentTime               { id: _treatmentTime
        onClicked: {
            sectionTimeClicked()
        }
    }

    onVisibleChanged: {
        if (visible) {
            _mainMenu.hidden = true
            _treatmentMenu.hidden = false
        }
    }

    Connections { target: _pressuresTouchArea
        Component.onCompleted: { // initial update of the pressure section
            _pressuresTouchArea.setBounds(
                        vTreatmentAdjustmentPressuresLimits.arterialLimitLow  ,
                        vTreatmentAdjustmentPressuresLimits.arterialLimitHigh ,
                        vTreatmentAdjustmentPressuresLimits.venousLimitLow    ,
                        vTreatmentAdjustmentPressuresLimits.venousLimitHigh   )
        }
    }

    Connections { target: vTreatmentAdjustmentPressuresLimits
        onAdjustmentTriggered   : { // updates the pressure section if only it has been accepted
            if(vTreatmentAdjustmentPressuresLimits.adjustment_Accepted) {
                _pressuresTouchArea.setBounds(
                            vTreatmentAdjustmentPressuresLimits.arterialLimitLow  ,
                            vTreatmentAdjustmentPressuresLimits.arterialLimitHigh ,
                            vTreatmentAdjustmentPressuresLimits.venousLimitLow    ,
                            vTreatmentAdjustmentPressuresLimits.venousLimitHigh   )
            }
        }
    }
}
