/****************************************************************************
**
** Copyright (C) 2020-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
** Copyright (C) 2020-2021 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."
****************************************************************************/

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12

// This example shows how to programmatically select 2 or more
// landmarks, or waypoints, and then center the group of waypoints
// in the viewport such that their enclosing bounding box fills the
// viewport vertically or horizontally or both.

// This code between the start and end markers is copied into the doc.

// setup codesnippet starts here
//! [LandmarkList plumbing]

import QtQuick.Window 2.12
import GeneralMagic 2.0

Window {

    visible: true
    width: 640
    height: 480
    title: qsTr("Programmatic CenterOnLandmarkList Example")

    property var updater: ServicesManager.contentUpdater(ContentItem.Type.RoadMap)
    Component.onCompleted: {

        //! [Set token safely]
        ServicesManager.settings.token = __my_secret_token;
        //! [Set token safely]

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

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

    //! [LandmarkList plumbing]
    // setup codesnippet ends here

    // routing setup codesnippet begins here
    //! [LandmarkList routing]

    LandmarkList {

        // This is the waypoint list for the route,
        // including the departure and destination.

        id: routingWaypoints
    }

    function addPoi(lat, lon, marker)
    {
        //Creates a landmark that is owned by the routingWaypoints, for efficiency.
        //This is required because
        //routingWaypoints.append(lmk);
        //will clone the landmark unless the owner is the routingWaypoints list itself.
        let lmk = ServicesManager.createLandmark(routingWaypoints);
        let coords = ServicesManager.createCoordinates(lat,lon,0);//lat,lon,alt
        lmk.coordinates = coords;
        marker.append(coords);
        routingWaypoints.append(lmk);
        console.log("Waypoint added #########");
    }

    //! [LandmarkList routing]
    // routing setup codesnippet ends here

    RoutingService {
        id: routingService
        type: Route.Type.Fastest
        transportMode: Route.TransportMode.Car

        // The list of waypoints is made available to the routing service.

        waypoints: routingWaypoints
        onFinished: {
            mapView.routeCollection.set(routeList);
            mapView.centerOnRouteList(routeList);
        }
    }

    // map view codesnippet starts here
    //! [LandmarkList mapView]

    MapView {
        id: mapView
        anchors.fill: parent
        viewAngle: 25
        cursorVisibility: false

    //! [LandmarkList mapView]
    // map view codesnippet ends here

        // The control buttons at the bottom.

        RowLayout {

            anchors.left: parent.left
            anchors.right: parent.right
            anchors.bottom: parent.bottom
            spacing: 20

            // compute routing codesnippet begins here
            //! [LandmarkList compute routing]

            Button {
                text: "Center on landmark list"
                enabled: ServicesManager.settings.connected
                onClicked: {
                    let marker = ServicesManager.createMarker();

                    routingWaypoints.clear();//clear the list before adding points;
                    addPoi(48.86144,2.33412, marker);//lat,lon,marker
                    addPoi(48.96144,2.43412, marker);//lat,lon,marker
                    addPoi(48.93144,2.23412, marker);//lat,lon,marker
                    addPoi(48.76144,2.23412, marker);//lat,lon,marker
                    addPoi(48.73144,2.43412, marker);//lat,lon,marker
                    addPoi(48.76144,2.53412, marker);//lat,lon,marker
                    addPoi(48.93144,2.53412, marker);//lat,lon,marker

                    //show landmarks on map as point markers
                    let renderSettings = ServicesManager.createMarkerRenderSettings();
                    let list = mapView.markerCollection.getExtendedList(MarkerList.Type.Point);
                    list.append(marker, renderSettings);

                    //center on list of waypoints/landmarks
                    mapView.centerOnGeographicArea(routingWaypoints.boundingBox)
                    //center on route plotted from list of waypoints/landmarks
                    //routingService.update()
                }
            }

            //! [LandmarkList compute routing]
            // compute routing codesnippet ends here

            Item {
                Layout.fillWidth: true
            }
        }

    // map view codesnippet closing parenthesis starts here
    //! [LandmarkList mapView end]
    }
    //! [LandmarkList mapView end]
    // map view codesnippet closing parenthesis ends here

// setup codesnippet closing parenthesis starts here
//! [LandmarkList plumbing end]
}
//! [LandmarkList plumbing end]
// setup codesnippet closing parenthesis ends here
