/*!
 *
 * 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    RxProfileContainer.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      11-May-2021
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  21-Oct-2019
 *
 */

// Qt
import QtQuick 2.12

// Project
//  Qml imports
import "qrc:/globals"
import "qrc:/components"
import "qrc:/pages/rxmanager"

    Rectangle {
        id: _root
        property bool onShow: true
        signal loadRxProfile()

        width: ListView.view.width
        height: 345
        color: Colors.mainTreatmentLighterBlue
        radius: 10
        Item{
            id:     _containerHeader
            anchors {
                top     : parent.top
                left    : parent.left
                right   : parent.right
                topMargin   : Variables.defaultMargin
                rightMargin : Variables.defaultMargin
                leftMargin  : Variables.defaultMargin * 2
            }
            height: 50
            Text {
                id: _profileName
                anchors.left        : _containerHeader.left
                text                : profileName
                font.pointSize      : Fonts.fontPixelButton * 1.4
                horizontalAlignment : Text.AlignHCenter
                color               : Colors.pressuresText
            }
            Row{
                id: _rxContainerRow
                anchors.right        : parent.right
                spacing              : 20
                property int rxProfileCount          : vRxProfiles.rxProfiles.rowCount()
                readonly property string editRx      : qsTr( "Edit"         )
                readonly property string duplicateRx : qsTr( "Duplicate"    )
                readonly property string exportRx    : qsTr( "Export"       )
                readonly property string deleteRx    : qsTr( "Delete"       )

                TouchRect { id  : _rxSelectButton
                    text.text   : qsTr("Select")
                    height      : Variables.defaultButtonHeight
                    width       : Variables.defaultButtonWidth
                    visible     : ! _root.onShow

                    onClicked   :{
                        vTreatmentCreate.doLoadRxProfile(vRxProfiles.rxProfiles.get(index), false)
                        _root.loadRxProfile()
                    }
                }

                IconButton { id: _favoriteButton
                    property bool isFavorite: false // Add a property connected to this button that will; affect the model that has the list View of Rx profiles
                    iconSize        : Variables.iconButtonSize
                    iconImageSource :_favoriteButton.isFavorite ? "qrc:/images/iFullHeartFavorite" : "qrc:/images/iEmptyHeartFavorite"

                    onClicked:{
                        isFavorite = !isFavorite
                        // Add signal to change model backend to be favorite or not, eventually connet property to model
                    }
                }
                BaseComboBox { id: _rxProfileMenu
                    model                   : _rxContainerRow.rxProfileCount === 10
                                              ? [ _rxContainerRow.editRx, _rxContainerRow.exportRx,  _rxContainerRow.deleteRx ]
                                              : [ _rxContainerRow.editRx, _rxContainerRow.duplicateRx, _rxContainerRow.exportRx,  _rxContainerRow.deleteRx ]
                    height                  : 50
                    width                   : 50
                    dropDownWidth           : 250
                    delegateHeight          : 65
                    popupX                  : -250
                    displayText             : ""
                    iconSource              : "qrc:/images/iMenuDot"
                    backgroundColor         : Colors.transparent
                    visible                 : _root.onShow

                    onActivated: {
                        const selectedItem = model[currentIndex]

                        // only send  AdjustParametersConfirmRequestData::eCancel when validated
                        // Telling FW user is canceling confirm treatment parameters
                        if ( vTreatmentCreate.parametersValidated ) { vTreatmentCreate.doCancel() }

                        if      ( selectedItem === _rxContainerRow.editRx         )   { print("TODO: Open and edit current model")
                                                                                        vTreatmentCreate.doLoadRxProfile(vRxProfiles.rxProfiles.get(index), true)
                                                                                        _root.loadRxProfile()}
                        else if ( selectedItem === _rxContainerRow.duplicateRx    )   { print("TODO: Open and edit copy of model")
                                                                                        vTreatmentCreate.doLoadRxProfile(vRxProfiles.rxProfiles.get(index), false)
                                                                                        _root.loadRxProfile()}
                        else if ( selectedItem === _rxContainerRow.exportRx       )   { print("TODO: export copy of model")             }
                        else if ( selectedItem === _rxContainerRow.deleteRx       )   { vRxProfiles.doDeleteRxProfile(profileName)      }
                        else                                                          {  /*Unknown state */                             }

                    }
                }
            }

        }
        Column {
            id: _componentColumn
            anchors {
                top         : _containerHeader.bottom
                left        : parent.left
                right       : parent.right
                leftMargin  : Variables.defaultMargin
                rightMargin : Variables.defaultMargin * 2
            }

            Row  {
                id: _componentRow
                spacing: 5
                width: parent.width
                property int componentWidth: parent.width / 8 - 5
                RxProfileComponent{
                                    id: _bloodflowComp
                                    title           : "Blood Flow"
                                    height          : _root.height - (100 + Variables.defaultMargin * 2)
                                    width           : _componentRow.componentWidth
                                    value           : bloodFlow
                                    unitText        : Variables.unitTextFlowRate
                                    }
                RxProfileComponent{
                                    id: _diaflowComp
                                    title           : "Dialysate Flow"
                                    height          : _root.height - (100 + Variables.defaultMargin * 2)
                                    width           : _componentRow.componentWidth
                                    value           : dialysateFlow
                                    unitText        : Variables.unitTextFlowRate
                                    }
                RxProfileComponent{
                                    id: _txDurComp
                                    title           : "Duration"
                                    height          : _root.height - (100 + Variables.defaultMargin * 2)
                                    width           : _componentRow.componentWidth
                                    value           : treatmentDuration
                                    unitText        : Variables.unitTextDuration
                                    }
                RxProfileComponent{
                                    id: _HeparinComp
                                    title           : "Heparin"
                                    height          : _root.height - (100 + Variables.defaultMargin * 2)
                                    width           : _componentRow.componentWidth
                                    value           : (heparinVolume === "NONE" || Number(heparinVolume).toFixed(3) === "0.000" ) ? Variables.emptyEntry : Number(heparinVolume).toFixed(3)
                                    unitText        : Variables.unitTextFluid
                                    }
                RxProfileComponent{
                                    id: _ACComp
                                    title           : "Acid"
                                    height          : _root.height - (100 + Variables.defaultMargin * 2)
                                    width           : _componentRow.componentWidth
                                    value           : acidConcentrate.replace(", ", "\n")
                                    topTextFont     : Fonts.fontPixelRxProfileComponentSmall
                                    unitText        : ""
                                    }
                RxProfileComponent{
                                    id: _BcComp
                                    title           : "Bicarbonate"
                                    height          : _root.height - (100 + Variables.defaultMargin * 2)
                                    width           : _componentRow.componentWidth
                                    value           : bicarbConcentrate.replace(" ", "\n")
                                    topTextFont     : Fonts.fontPixelRxProfileComponentSmall
                                    unitText        : ""
                                    }
                RxProfileComponent{
                                    id: _DialyzerComp
                                    title           : "Dialyzer"
                                    height          : _root.height - (100 + Variables.defaultMargin * 2)
                                    width           : _componentRow.componentWidth
                                    value           : dialyzerType.replace(/((?:[^ ]+ ){1}[^ ]+)\s/, "$1\n")
                                    topTextFont     : Fonts.fontPixelRxProfileComponentSmall
                                    unitText        : ""
                                    }
                RxProfileComponent{
                                    id: _TempComp
                                    title           : "Dialysate Temp"
                                    height          : _root.height - (100 + Variables.defaultMargin * 2)
                                    width           : _componentRow.componentWidth
                                    value           : dialysateTemperature
                                    unitText        : Variables.unitTextTemperature
                                    }
                }
        }
        Text {
            anchors {
                bottom      : parent.bottom
                right       : parent.right
                rightMargin : Variables.defaultMargin * 2
                bottomMargin   : Variables.defaultMargin
            }
            text                : 'Last Modified: ' + lastModifiedDate
            width               : parent.width
            horizontalAlignment : Text.AlignRight
            verticalAlignment   : Text.AlignBottom
            font.pointSize      : Fonts.fontPixelButton - 4
            color               : Colors.pressuresText
        }
    }

