import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Controls.impl 2.14
import QtQuick.Templates 2.14 as T

T.SpinBox {
    id: control
    from: 0
    value: 123
    to: 100*100
    stepSize: 1
    editable: true
    wheelEnabled : true

    property int decimals: 2
    property int divisor: 100
    property real realValue: value / divisor

    implicitWidth: implicitBackgroundWidth
    implicitHeight: Math.max(implicitContentHeight + topPadding + bottomPadding,
                             implicitBackgroundHeight,
                             up.implicitIndicatorHeight + down.implicitIndicatorHeight)

    padding: 0
    leftPadding: 0
    rightPadding: (control.mirrored ? 0 : (up.indicator ? up.indicator.width : 0))


    validator: DoubleValidator {
        bottom: Math.min(control.from, control.to)
        top:  Math.max(control.from, control.to)
    }

    valueFromText: function(text, locale) {
        return Number.fromLocaleString(locale, text) * divisor
    }

    contentItem: TextInput {
        z: 2
        text: Number(value / divisor).toLocaleString(locale, 'f', control.decimals)
        selectByMouse: true
        onActiveFocusChanged: {
            // When we first gain focus, select everything for clearing.
            if (activeFocus) {
                selectAll()
            }
        }
        onEditingFinished: {
            control.focus = false
        }
        
        color: activePalette.text
        selectionColor: activePalette.highlight
        selectedTextColor: activePalette.highlightedText
        horizontalAlignment: Qt.AlignHCenter
        verticalAlignment: Qt.AlignVCenter

        readOnly: !control.editable
        validator: control.validator
        inputMethodHints: Qt.ImhFormattedNumbersOnly

        Rectangle {
            width: control.width - (control.up.indicator.width - 1)
            height: control.height
            visible: control.activeFocus
            color: "transparent"
            border.color: activePalette.highlight
            border.width: 2
        }
    }

    up.indicator: Rectangle {
        x: control.mirrored ? 0 : parent.width - width
        height: parent.height / 2
        implicitWidth: 21
        implicitHeight: 17
        color: control.up.pressed ? activePalette.mid : activePalette.button

        Rectangle {
            x: (parent.width - width) / 2
            y: (parent.height - height) / 2
            width: parent.width / 3
            height: 2
            color: enabled ? activePalette.buttonText : activePalette.mid
        }
        Rectangle {
            x: (parent.width - width) / 2
            y: (parent.height - height) / 2
            width: 2
            height: parent.width / 3
            color: enabled ? activePalette.buttonText : activePalette.mid
        }
    }

    down.indicator: Rectangle {
        x: control.mirrored ?  0 : parent.width - width
        y: parent.height / 2
        height: parent.height / 2
        implicitWidth: 21
        implicitHeight: 17
        color: control.down.pressed ? activePalette.mid : activePalette.button

        Rectangle {
            x: (parent.width - width) / 2
            y: (parent.height - height) / 2
            width: parent.width / 3
            height: 2
            color: enabled ? activePalette.buttonText : activePalette.mid
        }
    }

    background: Rectangle {
        implicitWidth: 75
        color: enabled ? activePalette.dark : activePalette.button
        border.color: activePalette.button
        border.width: 3
    }
}