/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

import QtQuick          2.3
import QtQuick.Controls 1.2
import QtLocation       5.3
import QtPositioning    5.3

import QGroundControl               1.0
import QGroundControl.ScreenTools   1.0
import QGroundControl.Palette       1.0
import QGroundControl.Controls      1.0
import QGroundControl.FlightMap     1.0

/// Draggable Coordinate Item visuals
Item {
    id: _root

    property var    map        ///< Map control to place item in
    property var    qgcView    ///< QGCView to use for popping dialogs
    property var    coordinate
    property int    sequenceNumber
    property bool   checked
    property string label: "Reference"

    property var    _itemVisual: undefined
    property var    _dragArea: undefined

    signal clicked()
    signal released()
    signal entered()
    signal exited()

    signal dragStart()
    signal dragStop()
    signal dragClicked()
    signal dragReleased()

    function hideItemVisuals() {
        if (_itemVisual) {
            map.removeMapItem(_itemVisual)
            _itemVisual.destroy()
            _itemVisual = undefined
        }
    }

    function showItemVisuals() {
        if (!_itemVisual) {
            _itemVisual = indicatorComponent.createObject(map)
            map.addMapItem(_itemVisual)
        }
    }

    function hideDragArea() {
        if (_dragArea) {
            _dragArea.destroy()
            _dragArea = undefined
        }
    }

    function showDragArea() {
        if (!_dragArea) {
            _dragArea = dragAreaComponent.createObject(map)
        }
    }

    Component.onCompleted: {
        showItemVisuals()
        if (checked && map.planView) {
            showDragArea()
        }
    }

    Component.onDestruction: {
        hideDragArea()
        hideItemVisuals()
    }

    onCheckedChanged: {
        if (checked) {
            showDragArea()
        } else {
            hideDragArea()
        }
    }

    // Control which is used to drag items
    Component {
        id: dragAreaComponent

        CoordinateIndicatorDrag {
            mapControl:     _root.map
            itemIndicator:  _itemVisual
            Component.onCompleted: itemCoordinate = _root.coordinate

            onItemCoordinateChanged: {
                _root.coordinate = itemCoordinate
            }

            onDragStart:    _root.dragStart()
            onDragStop:     _root.dragStop()
            onClicked:  _root.dragClicked()
            onReleased: _root.dragReleased()
        }
    }

    Component {
        id: indicatorComponent

        CoordinateIndicator {
            label:          _root.label
            checked:        _root.checked
            z:              QGroundControl.zOrderMapItems
            sequenceNumber: _root.sequenceNumber
            coordinate:     _root.coordinate

            onClicked:  _root.clicked()
            onReleased: _root.released()
            onEntered:  _root.entered()
            onExited:   _root.exited()
        }
    }
}
