import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.9
import QtQuick.Controls.Material 2.0

import de.itsblue.LedDisplayController 1.0

Dialog {
    id: control

    property bool editing
    property var editingModel
    property var editingModelData

    parent: Overlay.overlay

    x: (parent.width - width) / 2
    y: (parent.height - height) / 2

    width: parent.width * 0.9
    height: Math.min(parent.height * 0.9, control.implicitHeight)

    modal: true

    title: editing ? qsTr("Edit text") : qsTr("New text")

    onAccepted: {
        if(editing) {
            editingModelData.active = activeSwitch.checked
            editingModelData.text = textTextField.value
            editingModelData.runtime = runtimeSpinBox.value
            editingModelData.color = colorColorPicker.value
            editingModelData.alignment = alignmentComboBox.currentIndex
            editingModelData.scroll = scrollSwitch.checked
            editingModelData.scrollDirection = scrollDirectionComboBox.currentIndex
            editingModelData.scrollSpeed = scrollSpeedSpinBox.value
            editingModelData.scrollCount = scrollCountSpinBox.value
        }
        else {
            editingModel.append(textTextField.value,
                                 activeSwitch.checked,
                                 runtimeSpinBox.value,
                                 colorColorPicker.value,
                                 alignmentComboBox.currentIndex,
                                 scrollSwitch.checked,
                                 scrollDirectionComboBox.currentIndex,
                                 scrollSpeedSpinBox.value,
                                 scrollCountSpinBox.value
                                 )
        }
    }

    onDiscarded: {
        editingModel.remove(popup.editingModelData.index)
        popup.close()
    }

    contentItem: Flickable {

        implicitHeight: dataFieldsGridLayout.height
        contentHeight: dataFieldsGridLayout.height

        clip: true
        boundsBehavior: Flickable.OvershootBounds

        ScrollBar.vertical: ScrollBar {
            interactive: false
        }

        ColumnLayout {
            id: dataFieldsGridLayout

            width: control.width * 0.9

            SwitchDelegate {
                id: activeSwitch
                Layout.fillWidth: true
                text: qsTr("Active")
            }

            TextInputDelegate {
                id: textTextField
                Layout.fillWidth: true
                required: true
                placeholderText: qsTr("Enter some text to be displayed")

                text: qsTr("Text")
            }

            SpinBoxDelegate {
                id: runtimeSpinBox
                Layout.fillWidth: true
                editable: true
                from: 1
                to: 3600
                text: qsTr("Runtime (in s)")
            }

            ColorPickerDelegate {
                id: colorColorPicker
                Layout.fillWidth: true
                text: qsTr("Color")
            }

            ComboBoxDelegate {
                id: alignmentComboBox
                Layout.fillWidth: true
                model: [qsTr("left"), qsTr("center"), qsTr("right")]
                text: qsTr("Alignment")
            }

            SwitchDelegate {
                id: scrollSwitch
                Layout.fillWidth: true
                text: qsTr("Scroll")
            }

            ComboBoxDelegate {
                id: scrollDirectionComboBox
                Layout.fillWidth: true
                model: [qsTr("from right to left"), qsTr("from left to right")]
                text: qsTr("Scroll")
            }

            SpinBoxDelegate {
                id: scrollSpeedSpinBox
                Layout.fillWidth: true
                from: 1
                to: 10
                text: qsTr("Scroll speed")
            }

            SpinBoxDelegate {
                id: scrollCountSpinBox
                Layout.fillWidth: true
                from: 1
                editable: true
                text: qsTr("Scroll count")
            }
        }
    }

    footer: DialogButtonBox {

        //  alignment: Qt.AlignHCenter
        buttonLayout: DialogButtonBox.GnomeLayout

        Material.background: "transparent"

        Button {
            flat: true
            enabled: textTextField.value !== ""
            text: qsTr("save")
            DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
        }

        Button {
            flat: true
            text: qsTr("cancel")
            DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
        }

        Button {
            visible: popup.editing
            Material.foreground: Material.Red
            flat: true
            text: qsTr("delete")
            DialogButtonBox.buttonRole: DialogButtonBox.DestructiveRole
        }
    }

    function edit(model, modelData) {
        editingModel = model
        editingModelData = modelData
        editing = true

        reset()

        activeSwitch.checked = editingModelData.active
        textTextField.value = editingModelData.text
        runtimeSpinBox.value = editingModelData.runtime
        colorColorPicker.value = editingModelData.color
        alignmentComboBox.currentIndex = editingModelData.alignment
        scrollSwitch.checked = editingModelData.scroll
        scrollDirectionComboBox.currentIndex = editingModelData.scrollDirection
        scrollSpeedSpinBox.value = editingModelData.scrollSpeed
        scrollCountSpinBox.value = editingModelData.scrollCount

        open()
    }

    function add(model) {
        editingModel = model
        editing = false
        reset()
        open()
    }

    function reset() {
        activeSwitch.checked = true
        textTextField.value = ""
        runtimeSpinBox.value = 1
        colorColorPicker.value = "#ffffff"
        alignmentComboBox.currentIndex = 0
        scrollSwitch.checked = false
        scrollDirectionComboBox.currentIndex = 0
        scrollSpeedSpinBox.value = 5
        scrollCountSpinBox.value = 1
    }
}
