/****************************************************************************
**
** Copyright (C) 2019-2023 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
** Copyright (C) 2019-2023 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.Window 2.12
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.12
import FpsCounter 1.0
import GeneralMagic 2.0

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Record GPS Position Log")

    Component.onCompleted: {
        //! [Set token safely]
        ServicesManager.settings.token = __my_secret_token;
        //! [Set token safely]

        ServicesManager.logLevel = ServicesManager.Error;
        ServicesManager.settings.allowInternetConnection = true;
        ServicesManager.settings.mapLanguage = ServicesSettings.NativeLanguage;

        ServicesManager.dataSourceRecorder.setChunkDurationMinutes = 1;
        ServicesManager.dataSourceRecorder.setContinuousRecording = true;
        ServicesManager.dataSourceRecorder.setDeleteOlderThanKeepMin = false;
        ServicesManager.dataSourceRecorder.setKeepMinMinutes = 60;

        ServicesManager.dataSource.playbackFile = Qt.resolvedUrl("strasbourg.nmea");
        ServicesManager.dataSource.type = DataSource.Type.Playback;

        let updater = ServicesManager.contentUpdater(ContentItem.Type.RoadMap);
        updater.autoApplyWhenReady = true;
        updater.update();
    }
    FpsCounter {
        id: fpsCounter
    }
    //! [MapView code]
    MapView {
        id: mapView
        anchors.fill: parent
        viewAngle: 25
        zoomLevel: 69
        viewPerspective: MapView.ViewPerspective.View3D
        buildingsVisibility: MapView.BuildingsVisibility.Show3D
        detailsQualityLevel: MapView.DetailsQualityLevel.Medium
        gestures: MapView.Gesture.Pan
                  | MapView.Gesture.PanEnableVelocity
                  | MapView.Gesture.Pinch
                  | MapView.Gesture.Rotate
                  | MapView.Gesture.Tilt
        Text {
            anchors.right: parent.right
            anchors.top: parent.top
            text: fpsCounter.fps
        }
        Button {
            id: livePlaybackButton
            enabled: !ServicesManager.dataSourceRecorder.active
            anchors.left: parent.left
            anchors.bottom: parent.bottom
            text: ServicesManager.dataSource.type === DataSource.Type.Live
                  ? "Live - switch to playback" : "Playback - switch to live"
            onClicked: {
                ServicesManager.dataSource.type =
                        ServicesManager.dataSource.type === DataSource.Type.Live
                        ? DataSource.Type.Playback : DataSource.Type.Live;
            }
        }
        ColumnLayout {
            Button {
                id: startStopButton
                text: ServicesManager.dataSourceRecorder.active
                      ? "Stop recording" : "Start recording"
                onClicked: ServicesManager.dataSourceRecorder.active =
                           !ServicesManager.dataSourceRecorder.active;
            }
            Button {
                text: ServicesManager.dataSource.playbackSpeedMultiplier == 9
                      ? "Slower playback" : "Faster playback"
                onClicked: {
                    ServicesManager.dataSource.playbackSpeedMultiplier =
                            ServicesManager.dataSource.playbackSpeedMultiplier == 9 ? 1 : 9;
                }
            }
        }
        Button {
            anchors.right: parent.right
            anchors.bottom: parent.bottom
            text: qsTr("Follow position")
            enabled: !mapView.followingPosition
            onClicked: mapView.followingPosition = true
        }
    }
    //! [MapView code]
}
