import QtQuick 2.0

import QtQuick.Layouts 1.11
import QtQuick.Controls 1.4
import QGroundControl.Controls 1.0
import QGroundControl.FactControls 1.0
import QGroundControl.ScreenTools   1.0

GridLayout {
    id:_root

    property bool checked: true
    property var missionItem: undefined
    property int availableWidth: 300

    property var _areaData: missionItem.areaData
    property real   _margin: ScreenTools.defaultFontPixelWidth / 2

    width:      availableWidth
    columnSpacing:  _margin
    rowSpacing:     _margin
    columns:        2

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

    onCheckedChanged: checkedChangedHandler()

    ExclusiveGroup{id:areaGroup}

    Repeater{
        id: areaSelector

        property int selectedIndex: -1

        model: _missionItem.areaData.areaList
        delegate: QGCRadioButton {
            text: object.objectName
            checkable: _root.checked
            Layout.fillWidth: true
            Layout.columnSpan: 2

            onCheckedChanged: {
                if (checked){
                    areaSelector.selectedIndex = index
                }
            }

            Component.onCompleted: {
                if (index === 0){
                    checked = true
                }
                object.interactive = Qt.binding(function(){return checked && _root.checked})
            }

        }
    } // area Repeater

    ColumnLayout {
        id:editorParent
        Layout.fillWidth: true
        Layout.columnSpan: 2
    }

    Repeater{
        id:areaEditorRepeater

        model: _missionItem.areaData.areaList
        delegate: Item{
            id:editor
            visible: index == areaSelector.selectedIndex

            property var _visualItem: undefined
            property var geoArea: object

            Component.onCompleted: {
                if (geoArea.editorQML && !_visualItem) {
                    var component = Qt.createComponent(geoArea.editorQML)
                    if (component.status === Component.Error) {
                        console.log("Error loading Qml: ", geoArea.editorQML, component.errorString())
                    } else {
                        _visualItem = component.createObject(editorParent, {
                                                                 geoArea: editor.geoArea,
                                                                 visible: Qt.binding(function(){return editor.visible}),
                                                                 availableWidth: Qt.binding(function(){return editorParent.width})})
                    }
                }
            }

            Component.onDestruction: {
                if (_visualItem) {
                    _visualItem.destroy()
                }
            }
        } // editor
    } // areaEditorRepeater


    QGCButton{
        text: "Check Rules"
        enabled: _root.checked
        Layout.fillWidth: true
        onClicked: {
            _areaData.isCorrect()
        }
    }

    QGCButton{
        text: "Intersection"
        enabled: _root.checked
        Layout.fillWidth: true
        onClicked: {
            _areaData.intersection()
        }
    }

    function checkedChangedHandler(){
        if (_root.checked){
            missionItem.startEditing()
        } else {
            missionItem.stopEditing()
        }
    }
}

