/****************************************************************************
**
** Copyright (C) 2019-2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
** Copyright (C) 2019-2022 General Magic B.V., info@generalmagic.com
**
** BSD License Usage
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Klarälvdalens Datakonsult AB nor the names of
**     its contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
****************************************************************************/

//! [MapView code]
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtQuick.Window 2.12
import GeneralMagic 2.0
import FpsCounter 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Markers")
    header: ToolBar {
        RowLayout {
            anchors.fill: parent
            ToolButton {
                text: qsTr("Add icon")
                onClicked: {
                    let p = map.wgsForScreen(Qt.point(map.width / 2, map.height / 2));
                    let marker = ServicesManager.createMarker();
                    marker.append(p);
                    let renderSettings = ServicesManager.createMarkerRenderSettings();
                    renderSettings.icon = ServicesManager.createIconFromFile("qrc:/kdab.png")
                    renderSettings.imageSize = 75;
                    let list = map.markerCollection.getExtendedList(MarkerList.Type.Point);
                    list.append(marker, renderSettings);
                }
            }

            ToolButton {
                text: qsTr("Add a polyline")
                onClicked: {
                    let p1 = map.wgsForScreen(Qt.point(0, 0));
                    let p2 = map.wgsForScreen(Qt.point(map.width, map.height));
                    let marker = ServicesManager.createMarker();
                    marker.append(p1);
                    marker.append(p2);

                    let renderSettings = ServicesManager.createMarkerRenderSettings();
                    renderSettings.polylineInnerColor = "red";
                    renderSettings.polylineOuterColor = "green";
                    renderSettings.polylineInnerSizeMM = 3;
                    renderSettings.polylineOuterSizeMM = 1;

                    let list = map.markerCollection.getExtendedList(MarkerList.Type.Polyline);
                    list.append(marker, renderSettings);
                }
            }

            ToolButton {
                text: qsTr("Add a polyline collection")
                onClicked: {
                    // Fist we need a MarkerList, let's create one
                    let list = ServicesManager.createMarkerList(MarkerList.Type.Polyline);

                    // next we create our "star"
                    {
                        let marker = ServicesManager.createMarker();
                        marker.append(map.wgsForScreen(Qt.point(0, 0))); // top left
                        marker.append(map.wgsForScreen(Qt.point(map.width, map.height)));  // bottom right
                        list.append(marker);
                    }

                    {
                        let marker = ServicesManager.createMarker();
                        marker.append(map.wgsForScreen(Qt.point(map.width, 0))); // top right
                        marker.append(map.wgsForScreen(Qt.point(0, map.height)));  // bottom left
                        list.append(marker);
                    }

                    {
                        let marker = ServicesManager.createMarker();
                        marker.append(map.wgsForScreen(Qt.point(map.width/2, 0))); // vertical top
                        marker.append(map.wgsForScreen(Qt.point(map.width/2, map.height)));  // vertical bottom
                        list.append(marker);
                    }

                    {
                        let marker = ServicesManager.createMarker();
                        marker.append(map.wgsForScreen(Qt.point(0, map.height/2))); // horizontal left
                        marker.append(map.wgsForScreen(Qt.point(map.width, map.height/2)));  //  horizontal right
                        list.append(marker);
                    }


                    // we also need a MarkerCollectionRenderSettings for this list
                    let renderSettings = ServicesManager.createMarkerListRenderSettings();
                    renderSettings.polylineInnerColor = "red";
                    renderSettings.polylineOuterColor = "green";
                    renderSettings.polylineInnerSizeMM = 3;
                    renderSettings.polylineOuterSizeMM = 1;
                    // last step is to add the MarkerList to our MapView MarkerCollection
                    map.markerCollection.appendList(list, renderSettings);

                    // As you can see, instead to add each item with it's own rendering settings we're adding
                    // them to a list and then we're setting the renderSettings to that list.
                    // This technique is more efficient when rendering then the previous way.
                }
            }

            ToolButton {
                text: qsTr("Add a polygon")
                onClicked: {
                    let center = map.wgsForScreen(Qt.point(map.width / 2, map.height / 2));
                    let radius = center.distance(map.wgsForScreen(Qt.point(map.width / 3, map.height / 3)));
                    let marker = ServicesManager.createCirclePolygonMarker(center, radius);

                    let renderSettings = ServicesManager.createMarkerRenderSettings();
                    renderSettings.polygonFillColor = Qt.rgba(0, 0, 1, 0.5);
                    renderSettings.polylineOuterColor = "orange";
                    renderSettings.polylineOuterSizeMM = 2;

                    let list = map.markerCollection.getExtendedList(MarkerList.Type.Polygon);
                    list.append(marker, renderSettings);
                }
            }

            ToolButton {
                text: qsTr("Add a polygon collection")
                onClicked: {
                    // same as polyline collection, but using polygons instead
                    let list = ServicesManager.createMarkerList(MarkerList.Type.Polygon);

                    {
                        // create a triangle
                        let marker = ServicesManager.createMarker();
                        marker.append(map.wgsForScreen(Qt.point(map.width / 2, 0)));
                        marker.append(map.wgsForScreen(Qt.point(0, map.height)));
                        marker.append(map.wgsForScreen(Qt.point(map.width , map.height)));
                        list.append(marker);
                    }

                    {
                        // create a rectrangle
                        let topLeft = map.wgsForScreen(Qt.point(0, 0));
                        let bottomRight = map.wgsForScreen(Qt.point(50, 50));
                        list.append(ServicesManager.createRectanglePolygonMarker(topLeft, bottomRight));
                    }

                    {
                        // create a circle
                        let center = map.wgsForScreen(Qt.point(map.width - 50, 25));
                        let radius = center.distance(map.wgsForScreen(Qt.point(map.width, 25)));
                        list.append(ServicesManager.createCirclePolygonMarker(center, radius));
                    }

                    let renderSettings = ServicesManager.createMarkerListRenderSettings();
                    renderSettings.polygonFillColor = "blue"
                    renderSettings.polylineOuterColor = "orange";
                    renderSettings.polylineOuterSizeMM = 2;

                    map.markerCollection.appendList(list, renderSettings);
                }
            }

            Item {
                Layout.fillWidth: true
            }
        }
    }

    property var updater: ServicesManager.contentUpdater(ContentItem.Type.RoadMap)
    Component.onCompleted: {
        //! [Set token safely]
        ServicesManager.settings.token = __my_secret_token;
        //! [Set token safely]

        ServicesManager.logLevel = ServicesManager.Error;
        ServicesManager.settings.allowInternetConnection = true; // enable connection to online services

        updater.autoApplyWhenReady = true;
        updater.update();
    }

    FpsCounter {
        id: fpsCounter
    }

    MapView {
        id: map
        anchors.fill: parent
        viewAngle: 25
        zoomLevel: 69
        viewPerspective: MapView.ViewPerspective.View3D
        buildingsVisibility: MapView.BuildingsVisibility.Show3D
        detailsQualityLevel: MapView.DetailsQualityLevel.Medium
        onCursorSelectionUpdated: {
            console.log("Got " + map.markersCursorSelection.length + " matched markers");
            for (let i = 0; i < map.markersCursorSelection.length; ++i) {
                let markerMatcher = map.markersCursorSelection[i];
                console.log(markerMatcher.type);
                console.log(markerMatcher.marker.uid);
                console.log(markerMatcher.markerList.uid);
            }
        }

        Text {
            anchors.left: parent.left
            anchors.top: parent.top
            text: fpsCounter.fps
        }
    }
}
//! [MapView code]
