/*!
 *
 * Copyright (c) 2021-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    InstitutionalContainer.qml
 * \author  (last)      Nico Ramirez
 * \date    (last)      20-Nov-2025
 * \author  (original)  Nico Ramirez
 * \date    (original)  20-Nov-2025
 *
 */

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

import QtQuick 2.12

LabelUnitContainer { id: _root
    height                                  : Variables.institutionaltContainerHeight

    readonly property alias minValue        : _min.value
    readonly property alias maxValue        : _max.value
    readonly property alias defValue        : _def.value
    readonly property alias defCustomValue  : _defCustom.currentIndex

    readonly property int subcomponentWidth : contentArea.width / 3
    readonly property int none              : -1

    property alias minText                  : _minText.text
    property alias maxText                  : _maxText.text
    property alias defText                  : _defText.text
    property real   min                     : 0
    property real   max                     : 0
    property real   step                    : 0
    property real   decimal                 : 0
    property bool   canOff                  : false
    property bool   minEditable             : true
    property bool   maxEditable             : true
    property bool   defEditable             : true
    property bool  defCustomEditable        : true
    property real   minVal                  : 0
    property real   maxVal                  : 0
    property real   defVal                  : 0
    property int    rejectionMin            : 0
    property int    rejectionMax            : 0
    property int    rejectionDef            : 0
    property var    defModel                : []
    property bool   grabbed                 : false

    signal didChange(int vValue)

    enum Range {
        Min     ,
        Max     ,
        Def
    }

    contentItem : Row { id: _row
        spacing     : 10
        anchors.fill: parent
        Rectangle {
            width   : _root.subcomponentWidth
            height  : _root.height
            color   : Colors.transparent
            radius  : 8.5
            border {
                width: 1
                color: _root.rejectionMin ? Colors.panelInvalidBorderColor : Colors.transparent
            }

            ValueAdjuster { id: _min
                anchors.fill    : parent
                editable        : _root.minEditable
                minimum         : _root.min
                maximum         : _max.value
                step            : _root.step
                value           : _root.minVal
                decimal         : _root.decimal
                isActive        : true
                canOff          : _root.canOff
                visible         : _root.minVal !==  _root.none

                onGrabbedChanged: {
                    _root.grabbed = grabbed
                }

                onDidChange     : function(vValue) {
                    _root.didChange( InstitutionalContainer.Min )
                    value = vValue
                }
            }

            // will show only if area has text only and nothing editable
            Text { id: _minText
                anchors.fill        : parent
                visible             : _root.minVal === _root.none
                color               : Colors.offWhite
                font.pixelSize      : Fonts.fontPixelValueControl
                horizontalAlignment : Text.AlignHCenter
                verticalAlignment   : Text.AlignVCenter
            }
        }

        Rectangle {
            width   : _root.subcomponentWidth
            height  : _root.height
            color   : Colors.transparent
            radius  : 8.5
            border {
                width: 1
                color: _root.rejectionMax ? Colors.panelInvalidBorderColor : Colors.transparent
            }

            ValueAdjuster { id: _max
                anchors.fill    : parent
                editable        : _root.maxEditable
                minimum         : _min.value
                maximum         : _root.max
                step            : _root.step
                value           : _root.maxVal
                decimal         : _root.decimal
                isActive        : true
                canOff          : _root.canOff
                visible         : _root.maxVal !==  _root.none

                onGrabbedChanged: {
                    _root.grabbed = grabbed
                }

                onDidChange     : function(vValue) {
                    _root.didChange( InstitutionalContainer.Max )
                    value = vValue
                }
            }

            // will show only if area has text only and nothing editable
            Text { id: _maxText
                anchors.fill        : parent
                visible             : _root.maxVal === _root.none
                color               : Colors.offWhite
                font.pixelSize      : Fonts.fontPixelValueControl
                horizontalAlignment : Text.AlignHCenter
                verticalAlignment   : Text.AlignVCenter
            }
        }

        Rectangle {
            width   : _root.subcomponentWidth - Variables.defaultMargin // need margin for right side for rejection box
            height  : _root.height
            color   : Colors.transparent
            radius  : 8.5

            border {
                width: 1
                color: _root.rejectionDef ? Colors.panelInvalidBorderColor : Colors.transparent
            }

            ValueAdjuster { id: _def
                anchors.fill    : parent
                editable        : _root.defEditable
                minimum         : _root.min
                maximum         : _root.max
                step            : _root.step
                value           : _root.defVal
                decimal         : _root.decimal
                isActive        : true
                canOff          : _root.canOff
                visible         : _root.defVal !== _root.none && _root.defModel.length === 0

                onGrabbedChanged: {
                    _root.grabbed = grabbed
                }

                onDidChange     : function(vValue) {
                    _root.didChange( InstitutionalContainer.Def )
                    value = vValue
                }
            }

             ValueAdjusterCustom { id: _defCustom
                anchors.fill    : parent
                visible         : _root.defModel.length > 0
                model           : _root.defModel.length === 0 ? [] : _root.defModel
                currentIndex    : _root.defVal
                canOff          : _root.canOff
                editable        : _root.defCustomEditable

                onGrabbedChanged: {
                    _root.grabbed = grabbed
                }
             }

            // will show only if area has text only and nothing editable
            Text { id: _defText
                anchors.fill        : parent
                visible             : _root.defVal === _root.none && _root.defModel.length === 0
                color               : Colors.offWhite
                font.pixelSize      : Fonts.fontPixelValueControl
                horizontalAlignment : Text.AlignHCenter
                verticalAlignment   : Text.AlignVCenter
            }
        }
    }
}
