/*!
 *
 * Copyright (c) 2020-2025 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    AcidConcentrateAdjustment.qml
 * \author  (last)      Nico Ramirez
 * \date    (last)      25-Aug-2025
 * \author  (original)  Nico Ramirez
 * \date    (original)  25-Aug-2025
 *
 */

// Qt
import QtQuick 2.12

// Project

//  Qml imports
import "qrc:/globals"
import "qrc:/components"
import "qrc:/compounds"
import "qrc:/pages/pretreatment/create"

ModalDialog { id : _root
    property int controlWidth   : 770
    property int controlHeight  : 86
    readonly property string adjustment: ("%1 %2, %3 %4, 1 Mg") .arg(_potassiumValueControl.value.toFixed(Variables.potassiumPrecision))
                                                                .arg(Variables.unitTextPotassium)
                                                                .arg(_calciumValueControl.value.toFixed(Variables.calciumPrecision))
                                                                .arg(Variables.unitTextCalcium)

    width           : 1000
    height          : 600
    onVisibleChanged: visible ? vTreatmentRanges.doPopulateAcidConcentrate( vTreatmentCreate.acidConcentrateSet,
                                                                            vTreatmentCreate.acidConcentrate) :
                                notificationText = ""

    Connections { target: vTDOpMode
        function onPreTreatmentChanged() { if (vTDOpMode.preTreatment) { _root.close() } }
    }

    Item { id : _contentItem
        anchors.fill: parent

        TitleText { id: _titleText
            anchors {
                top             : parent.top
                topMargin       : Variables.defaultMargin
                horizontalCenter: parent.horizontalCenter
            }
            text            : qsTr("Acid Concentrate")
            font.pixelSize  : Fonts.fontPixelTitle
            font.weight     : Font.Medium
        }

        CloseButton { id: _closeButton
            onClicked: {
                _potassiumValueControl.clear()
                _calciumValueControl.clear()
                _root.close()
            }
        }

        LabelUnitContainer { id: _potassium
            anchors {
                verticalCenter: parent.verticalCenter
                verticalCenterOffset: -100
                horizontalCenter: parent.horizontalCenter
            }

            text        : qsTr("Potassium")
            unitText    : Variables.unitTextPotassium
            width           : _root.controlWidth
            height          : _root.controlHeight

            contentItem : ValueAdjuster{ id: _potassiumValueControl
                minimum         : vInstitutionalRecord.acidConcentratePotassiumMin
                maximum         : vInstitutionalRecord.acidConcentratePotassiumMax
                step            : vTreatmentRanges.acidConcentratePotassiumRes
                defaultValue    : vTreatmentRanges.acidConcentratePotassiumDef
                decimal         : Variables.potassiumPrecision
                value           : vTreatmentRanges.potassium
                isActive        : vTreatmentRanges.potassiumSet
                onDidActiveChange   : function(vState) { vTreatmentRanges.potassiumSet  = vState    }
                onDidChange         : function(vValue) { vTreatmentRanges.potassium     = vValue    }
            }
        }

        LabelUnitContainer { id: _calcium
            anchors {
                top             : _potassium.bottom
                topMargin       : Variables.defaultMargin
                horizontalCenter: parent.horizontalCenter
            }

            text        : qsTr("Calcium")
            unitText    : Variables.unitTextCalcium
            width           : _root.controlWidth
            height          : _root.controlHeight

            contentItem : ValueAdjuster { id: _calciumValueControl
                minimum         : vInstitutionalRecord.acidConcentrateCalciumMin
                maximum         : vInstitutionalRecord.acidConcentrateCalciumMax
                step            : vTreatmentRanges.acidConcentrateCalciumRes
                defaultValue    : vTreatmentRanges.acidConcentrateCalciumDef
                decimal         : Variables.calciumPrecision
                value           : vTreatmentRanges.calcium
                isActive        : vTreatmentRanges.calciumSet
                onDidActiveChange   : function(vState) { vTreatmentRanges.calciumSet    = vState    }
                onDidChange         : function(vValue) { vTreatmentRanges.calcium       = vValue    }
            }
        }

        TouchRect { id : _saveButton
            anchors {
                bottom          : parent.bottom
                bottomMargin    : notification.visible ? Variables.defaultMargin * 5 : Variables.defaultMargin * 2
                horizontalCenter: parent.horizontalCenter
            }
            text.text   : qsTr("Save")
            isDefault   : true
            width       : 250
            height      : 75
            pixelSize   : 30
            enabled     : _potassiumValueControl.isActive && _calciumValueControl.isActive

            onClicked   : {
                if ( vTreatmentCreate.acidConcentrateRejectReason ) { vTreatmentCreate.acidConcentrateRejectReason = Variables.noRejectReason }
                vTreatmentRanges.doSaveAcidConcentrate(_root.adjustment)
            }

            Behavior on anchors.bottomMargin { NumberAnimation { duration: 300} }
        }

        Connections { target: vTreatmentRanges
            function onAcidConcentrateAcceptedChanged ( vAccepted ) {
                if ( vAccepted ) {
                    _potassiumValueControl.clear()
                    _calciumValueControl.clear()
                    vTreatmentCreate.acidConcentrateSet = true
                    _root.accept()
                }
                else {
                    notificationText = qsTr("Duplicate Acid Concentrate Entry")
                }
            }

            function onDidPopulateAcidConcentrate () {
                _potassiumValueControl  .refresh()
                _calciumValueControl    .refresh()
                vTreatmentRanges.potassiumSet   = true
                vTreatmentRanges.calciumSet     = true
                notificationText = qsTr("Duplicate Acid Concentrate Entry")
            }
        }
    }
}
