import QtQuick 2.12

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

HeaderBarPopup { id: _root
    readonly property int   sliderWidth         : 430
    readonly property color sliderBkgndColor    : Colors.dialogShadowColor
    readonly property color valueColor          : Colors.dialogValueColor

    padding         : Variables.defaultMargin

    QtObject { id: _private
        readonly property var textFont      : Qt.font   ({ pixelSize: Fonts.fontPixelDialogText,    weight: Font.Medium     })
        readonly property var valueFont     : Qt.font   ({ pixelSize: 28,                           weight: Font.DemiBold   })
        readonly property int columnWidth   : sliderWidth + padding * 2
        readonly property int columnSpacing : Variables.defaultMargin * 2
        readonly property int itemHeight    : 30
    }

    contentItem: Column { id : _dialogColumn
        anchors.centerIn    : parent
        width               : _private.columnWidth
        spacing             : Variables.defaultMargin

        Column { id: _brightness
            width   : _private.columnWidth
            spacing : _private.columnSpacing

            Item { id: _brightnessTextItem
                height  : _private.itemHeight
                width   : parent.width

                Text { id: _brightnessText
                    anchors.left            : parent.left
                    anchors.verticalCenter  : parent.verticalCenter
                    text                    : ("%1: ").arg(qsTr("Brightness"))
                    color                   : Colors.dialogText
                    font                    : _private.textFont
                }

                Text {  id: _brightnessValue
                    anchors.left    : _brightnessText.right
                    anchors.bottom  : _brightnessText.bottom
                    text            : ("%1").arg(vDevice.brightnesss)
                    color           : _root.valueColor
                    font            : _private.valueFont
                }
            }

            Slider { id : _brightnessSlider
                anchors.horizontalCenter: parent.horizontalCenter
                width                   : _root.sliderWidth
                height                  : 10
                showMinMaxText          : false
                step                    : 1    // no zero
                minimum                 : 1
                maximum                 : 5
                ticks                   : true
                unit                    : ""
                bgColor                 : _root.sliderBkgndColor
                diameter                : 26
                hasBorder               : false
                handleBorderColor       : Colors.highlightProgressBar

                onReleased              : vDevice.brightness = _brightnessSlider.value

                Connections { target: vDevice
                    // in case the value is rejected it will be set to the previous value
                    // also the init value shall be set when navigate to the screen
                    function onBrightnessChanged    ( vValue ) { _brightnessSlider.reset(vValue) }

                    // TODO do something with rejection message
                    function onStatusChanged        ( vValue ) { print("**** Brightness Status: " + vValue) }
                }
            }
        }

        Line { id: _divider
            anchors.horizontalCenter: parent.horizontalCenter
            length                  : parent.width
            color                   : _root.sliderBkgndColor
        }

        Column { id: _volume
            width               : _private.columnWidth
            spacing             : _private.columnSpacing

            Item { id: _volumeTextItem
                height  : _private.itemHeight
                width   : parent.width

                Text {  id: _volumeText
                    anchors.left            : parent.left
                    anchors.verticalCenter  : parent.verticalCenter
                    text                    : ("%1: ").arg(qsTr("Alarm Volume"))
                    color                   : Colors.dialogText
                    font                    : _private.textFont
                }

                Text {  id: _volumeValue
                    anchors.left            : _volumeText.right
                    anchors.bottom  : _volumeText.bottom
                    text            : vAdjustmentAlarmVolume.hdAlarmVolume
                    color           : _root.valueColor
                    font            : _private.valueFont
                }
            }

            Slider { id : _volumeSlider
                anchors.horizontalCenter: parent.horizontalCenter
                width                   : _root.sliderWidth
                height                  : 10
                showMinMaxText          : false
                step                    : 1    // no zero
                minimum                 : 1
                maximum                 : 5
                ticks                   : true
                unit                    : ""
                bgColor                 : _root.sliderBkgndColor
                diameter                : 26
                hasBorder               : false
                handleBorderColor       : Colors.highlightProgressBar

                onReleased              : vAdjustmentAlarmVolume.doAdjustment( _volumeSlider.value )

                Connections { target: vAdjustmentAlarmVolume
                    function onAdjustmentTriggered  ( vValue ) {
                        if ( vAdjustmentAlarmVolume.adjustment_Accepted ) {
                            vSettings   .alarmVolume   = vAdjustmentAlarmVolume.hdAlarmVolume
                        }
                        else {
                            // TODO do something with rejection message
                            print("**** Volume Rejected Reason: " + vAdjustmentAlarmVolume.adjustment_ReasonText)
                        }

                        // regardless of the rejection or acceptance the value will be sent from C++ to be adjusted.
                        _volumeSlider.reset          ( vAdjustmentAlarmVolume.hdAlarmVolume )
                    }
                }
            }
        }
    }
}
