import QtQuick          2.3
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.4
import QtQuick.Dialogs  1.2
import QtQuick.Extras   1.4
import QtQuick.Layouts  1.2

import QGroundControl               1.0
import QGroundControl.ScreenTools   1.0
import QGroundControl.Vehicle       1.0
import QGroundControl.Controls      1.0
import QGroundControl.FactSystem    1.0
import QGroundControl.FactControls  1.0
import QGroundControl.Palette       1.0
import QGroundControl.FlightMap     1.0

ColumnLayout {
    id:root

    property int availableWidth: 300
    property var missionItem: undefined       ///< Mission Item for editor
    property bool checked: true

    property real   _margin: ScreenTools.defaultFontPixelWidth / 2
    property var    _generator: 	  	missionItem.generator
    property var 	_generatorEditor: 	undefined

    width:      availableWidth

    Component.onCompleted: {
        console.assert(missionItem !== undefined, "please set the missionItem property")
        _addGeneratorEditor()
    }

    Component.onDestruction: {
        _destroyGeneratorEditor()
    }

    on_GeneratorChanged: {
        _destroyGeneratorEditor()
        _addGeneratorEditor()
    }



    SectionHeader {
        id:     generalHeader
        Layout.fillWidth: true
        text:           qsTr("General")
    }


    GridLayout {
        id: generalGrid
        Layout.fillWidth: true
        columnSpacing:  _margin
        rowSpacing:     _margin
        columns:        2
        visible:        generalHeader.checked

        QGCLabel {
            text: qsTr("Altitude")
            Layout.fillWidth: true
        }
        FactTextField {
            fact:                   missionItem.altitude
            Layout.fillWidth:       true
        }

        QGCLabel { text: qsTr("Relative Altitude!!!") }

        QGCLabel {
            text: qsTr("Variant")
            Layout.columnSpan: 2
            visible: variantRepeater.len > 0
        }
        GridLayout{
            Layout.columnSpan: 2
            Layout.fillWidth: true
            Layout.maximumWidth:  parent.width

            columnSpacing:  _margin
            rowSpacing:     _margin

            ExclusiveGroup{id:variantGroup}
            Repeater{
                id: variantRepeater

                property var names: missionItem.variantNames
                property int len: missionItem.variantNames.length

                model: len
                delegate: QGCRadioButton {
                    checked: index === variant
                    text:  variantRepeater.names[index] ? variantRepeater.names[index]: ""

                    property int variant: missionItem.variant.value

                    onVariantChanged: {
                        if (variant === index){
                            checked = true
                        }
                    }

                    onCheckedChanged: {
                        if (checked && variant !== index){
                            missionItem.variant.value = index
                        }
                    }
                }
            } // variant repeater
        } // variant grid
    } // general grid

    // Generator Editor
    SectionHeader {
        id:     generatorHeader
        Layout.fillWidth: true
        text:           qsTr("Generator")
    }

    GridLayout{
        Layout.fillWidth: true
        columnSpacing:  _margin
        rowSpacing:     _margin
        columns:        2
        visible:        generatorHeader.checked

        QGCComboBox {
            property var names: missionItem.generatorNameList
            property int length: names.length

            enabled: root.checked
            anchors.margins:    ScreenTools.defaultFontPixelWidth
            currentIndex:       missionItem.generatorIndex
            Layout.columnSpan:  2
            model:              missionItem.generatorNameList

            onActivated: {
                if (index != -1){
                    missionItem.switchToGenerator(index)
                }
            }
        }
    }

    ColumnLayout{
        id:generatorEditorParent
        Layout.fillWidth: true
        visible:        generatorHeader.checked
    }

    QGCButton{
        text:qsTr("Reverse")
        onClicked: missionItem.reverseRoute()
        Layout.columnSpan: 2
        Layout.fillWidth: true
    }

    // bussy indicator
    ColumnLayout{
        Layout.fillWidth: true
        spacing:        _margin

        BusyIndicator{
            id: indicator

            property bool calculating: missionItem.calculating

            running: calculating
            visible: calculating || timer.running

            onCalculatingChanged: {
                if(!calculating){
                    // defer hiding
                    timer.restart()
                }
            }

            Timer{
                id: timer
                interval: 1000
                repeat: false
                running: false
            }
        }
    } // indicator column

    function _addGeneratorEditor(){
        if (_generator.editorQml && !_generatorEditor) {
            var component = Qt.createComponent(_generator.editorQml)
            if (component.status === Component.Error) {
                console.log("Error loading Qml: ",
                            _generator.editorQml, component.errorString())
            } else {
                _generatorEditor =
                        component.createObject(
                            generatorEditorParent, {
                                "generator": root._generator,
                                "availableWidth": generatorEditorParent.width,
                            })
            }
        }
    }

    function _destroyGeneratorEditor(){
        if (_generatorEditor){
            _generatorEditor.destroy()
            _generatorEditor = undefined
        }
    }
}
