/*!
 *
 * Copyright (c) 2025-2026 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    TreatmentHDFComponent.qml
 * \author  (last)      Nico Ramirez
 * \date    (last)      2-Feb-2026
 * \author  (original)  Nico Ramirez
 * \date    (original)  2-Feb-2026
 *
 */

import QtQuick 2.15

import "qrc:/globals"
import "qrc:/components"
import "qrc:/compounds"
import "qrc:/pages/treatment"

TreatmentSection { id: _root
    color           : Colors.panelBackgroundColor
    header.color    : Colors.heparinSectionHeader
    enableDropShadow: false
    header.title    :  qsTr("Hemodiafiltration") + " (" + qsTr("HDF") + ")"

    contentArea.anchors {
        topMargin   : Variables.defaultMargin
        leftMargin  : Variables.defaultMargin * 2
        rightMargin : Variables.defaultMargin
        bottomMargin: Variables.defaultMargin
    }
    border {
        width: 1
        color: Colors.heparinPanelBorderColor
    }
    header.border {
        width: 1
        color: Colors.heparinPanelBorderColor
    }

    readonly property int topTextFontSize           : 25
    readonly property int bottomTextFontSize        : 60
    readonly property real estimatedSubstitutionVal : 23.0    // TODO update later
    readonly property int substitutionRate          : 125   // TODO update later

    readonly property real minimum      : vTreatmentRanges.substitutionVolumeMin
    readonly property real maximum      : vTreatmentRanges.substitutionVolumeMax
    readonly property real resolution   : vTreatmentRanges.substitutionVolumeRes

    readonly property real substitutionValue: 10.0 // TODO update later

    readonly property string statusText     : qsTr("Active")
    /*isDispensing  ? qsTr("Active")    :
          isPaused      ? qsTr("Pause")     :
          isCompleted   ? qsTr("Complete")  :  qsTr("Active")*/

    readonly property string buttonText     : qsTr("Pause Substitution Flow")
    property color  statusColor             : Colors.transparent


    readonly property int cellHeight: contentItem.height
    readonly property int cellWidth : contentItem.width / 2 - _row.spacing

    signal substitutionFlowClicked()

    contentItem: Row { id: _row
        spacing: Variables.defaultMargin * 2

        Item { id: _leftColumnContent
            width   : _root.cellWidth
            height  : _root.cellHeight

            LabelValue { id: _estimatedSubstitutionVol
                anchors {
                    top     : parent.top
                    left    : parent.left
                }
                height                  : parent.height / 3
                width                   : parent.width * 0.2
                topText                 : qsTr("Estimated Final Substitution Volume")
                topTextFont.pixelSize   : topTextFontSize
                topTextFont.weight      : Font.Medium
                bottomText              : (_root.estimatedSubstitutionVal).toFixed(Variables.substitutionPrecision)  //vTreatmentUltrafiltration.setVolume.toFixed(Variables.substitutionPrecision)
                bottomTextFont.pixelSize: bottomTextFontSize
                bottomTextFont.weight   : Font.Normal
                leftAlign               : true
                unitText                : Variables.unitVolume
                bottomTextTopMargin     : Variables.defaultMargin
            }

            LabelValue { id: _substitutionRate
                anchors {
                    top         : _estimatedSubstitutionVol.bottom
                    topMargin   : Variables.defaultMargin * 2
                    left        : parent.left
                }
                height                  : parent.height / 3
                width                   : parent.width * 0.2
                topText                 : qsTr("Substitution Rate")
                topTextFont.pixelSize   : topTextFontSize
                topTextFont.weight      : Font.Medium
                bottomText              : _root.substitutionRate //vTreatmentUltrafiltration.setVolume.toFixed(Variables.substitutionPrecision)
                bottomTextFont.pixelSize: bottomTextFontSize
                bottomTextFont.weight   : Font.Normal
                leftAlign               : true
                unitText                : Variables.unitTextFlowRate
                bottomTextTopMargin     : Variables.defaultMargin
            }

            ProgressBar { id: _progressbar
                anchors {
                    verticalCenter  : parent.verticalCenter
                    right           : parent.right
                }
                width           : parent.width * 0.6
                height          : 20
                decimal         : Variables.substitutionPrecision
                minimum         : _root.minimum
                maximum         : vTreatmentCreate.substitutionFluidVolume
                value           : _root.substitutionValue
                unitText        : Variables.unitVolume
                color           : Colors.progressBarUltrafiltration
                bgColor         : Colors.heparinSectionHeader
                radius          : height
                showMarker      : false
                minText.color: "#818181"
                minText.font {
                    pixelSize   : 18
                    weight      : Font.Normal
                }

                maxText.color: "#818181"
                maxText.font {
                    pixelSize   : 18
                    weight      : Font.Normal
                }

                Text { id: _valueText
                    anchors {
                        bottom      : parent.top
                        bottomMargin   : Variables.defaultMargin
                        right       : parent.right
                    }
                    text            : _root.substitutionValue.toFixed(Variables.substitutionPrecision) + " " + Variables.unitVolume
                    color           : Colors.white
                    font.pixelSize  : 40
                    font.weight     : Font.DemiBold
                }

                Text { id: _substituionVolume
                    anchors {
                        top         : parent.bottom
                        topMargin   : Variables.defaultMargin * 2.5
                        right       : parent.right
                    }
                    text            : qsTr("Substitution Volume")
                    color           : Colors.textTextRectLabel
                    font.pixelSize  : 20
                    font.weight     : Font.Medium
                }
            }

            Rectangle { id: _status
                anchors {
                    top         : parent.top
                    right       : parent.right
                }
                width   : 100
                height  : 30
                radius  : height
                color   : _root.statusColor
                Text { id: _statusText
                    anchors.centerIn: parent
                    text            : _root.statusText
                    color           : Colors.white
                    font.pixelSize  : Fonts.fontPixelContainerUnitSmall
                    font.weight     : Font.DemiBold
                }
            }

            TouchRect { id : _substitutionFlowButton
                width   : text.implicitWidth + Variables.defaultMargin * 2
                height  : 60
                text    {
                    text            : _root.buttonText
                    font.pixelSize  : Fonts.fontPixelConfirm
                    font.weight     : Font.Medium
                }

                isDefault: true

                anchors {
                    bottom          : parent.bottom
                    horizontalCenter: parent.horizontalCenter
                }

                onClicked: substitutionFlowClicked()
            }
        }

        Column { id: _rightColumn
            leftPadding : Variables.defaultMargin
            width       : _root.cellWidth
            height      : _root.cellHeight
            spacing     : Variables.defaultMargin

            Item  { id: _autoSub
                width   : _rightColumn.width
                height  : _rightColumn.height / 4

                Text { id: _autoSubText
                    anchors {
                        verticalCenter  : _autoSub.verticalCenter
                        right           : _autoSub.right
                    }
                    text    : qsTr("Auto Sub")
                    color   : Colors.offWhite
                    height  : Variables.contentHeight
                    width   : implicitWidth
                    verticalAlignment   : Text.AlignVCenter

                    font {
                        pixelSize       : 22
                        family          : Fonts.fontFamilyFixed
                        weight          : Font.Medium
                    }
                }

                BaseSwitch { id: _autoSubSwitch
                    anchors {
                        verticalCenter  : _autoSubText.verticalCenter
                        right           : _autoSubText.left
                        rightMargin     :  Variables.defaultMargin
                    }
                    checked : true  // TODO
                    height  : Variables.contentHeight

                    onClicked: {
                        print (("Auto Sub Switch set to %1").arg(_autoSubSwitch.checked ? "On" : "Off"))
                    }
                }
            }

            LabelUnitContainer { id: _treatmentMode
                objectName      : "_treatmentMode"
                text            : qsTr("Treatment Mode")
                width           : _rightColumn.width
                height          : Variables.adjustmentLabelUnitContainerHeight

                contentItem : ValueAdjusterCustom { id: _treatmentModeControl
                    value       : vTreatmentCreate.hdfTreatmentMode
                    model       : vTreatmentRanges.hdfTreatmentModeOptions
                    editable    : false
                    onDidChange : function(vValue) {
                        vTreatmentCreate.hdfTreatmentMode = vValue
                    }
                }
            }

            LabelUnitValueAdjuster { id: _prescribedFluidVolume
                objectName          : "_prescribedFluidVolume"
                text                : qsTr("Prescribed Sub. Fluid Volume")
                unitText            : Variables.unitVolume
                decimal             : Variables.substitutionPrecision
                valueTextColor      : Colors.offWhite
                isActive            : true
                editable            : false
                minimum             : _root.minimum
                maximum             : _root.maximum
                step                : _root.resolution
                width               : _rightColumn.width
                value               : vTreatmentCreate.substitutionFluidVolume
                defaultValue        : vTreatmentRanges.substitutionVolumeDef
            }
        }
    }
}
