/****************************************************************************
**
** 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."
****************************************************************************/

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

import GeneralMagic 2.0

TransparentPage {
    anchors.fill: parent
    color: Qt.rgba(0, 0, 0, 0.5)
    id: root
    property var contentItem

    MapView {
        id: map
        anchors.fill: parent
        viewAngle: 25
        zoomLevel: 69
        viewPerspective: MapView.ViewPerspective.View3D
        buildingsVisibility: MapView.BuildingsVisibility.Show3D

        Flickable {
            anchors.fill: parent
            anchors.margins: 50
            contentWidth: content.width
            contentHeight: content.height

            ColumnLayout {
                id: content
                spacing: 5
                Text {
                    Layout.fillWidth: true
                    color: "green"
                    text: "Name :" + contentItem.name
                }
                Text {
                    Layout.fillWidth: true
                    color: "green"
                    text: "Id :" + contentItem.id
                }
                Text {
                    Layout.fillWidth: true
                    color: "green"
                    text: "Info :" + contentItem.info
                }
                Text {
                    Layout.fillWidth: true
                    color: "green"
                    text: "CountryCodes :" + contentItem.countryCodes
                }
                Text {
                    Layout.fillWidth: true
                    color: "green"
                    text: "Language :" + contentItem.language
                }
                Text {
                    Layout.fillWidth: true
                    color: "green"
                    text: "FilePath :" + contentItem.filePath
                }
                Text {
                    Layout.fillWidth: true
                    color: "green"
                    text: "ClientVersion :" + contentItem.clientVersion.major + "." + contentItem.clientVersion.minor
                }
                Text {
                    Layout.fillWidth: true
                    color: "green"
                    text: "TotalSize :" + contentItem.totalSize
                }
                Text {
                    Layout.fillWidth: true
                    color: "green"
                    text: "AvailableSize :" + contentItem.availableSize
                }
                Text {
                    Layout.fillWidth: true
                    color: "green"
                    text: "Completed :" + contentItem.completed
                }
                Text {
                    Layout.fillWidth: true
                    color: "green"
                    text: "Status :" + contentItem.status
                }
                Text {
                    Layout.fillWidth: true
                    color: "green"
                    text: "DownloadProgress :" + contentItem.downloadProgress
                }
                Text {
                    Layout.fillWidth: true
                    color: "green"
                    text: "IsImagePreviewAvailable :" + contentItem.imagePreviewAvailable
                }
                IconView {
                    visible: root.contentItem.imagePreviewAvailable
                    iconSource: root.contentItem.imagePreview
                    Layout.maximumHeight: 32
                    Layout.maximumWidth: 32
                    width: 32
                    height: 32
                }
                RowLayout {
                    Button {
                        text: switch (root.contentItem.status) {
                              case ContentItem.Status.Unavailable:
                                  return "Download";
                              case ContentItem.Status.Completed:
                                  return "Completed";
                              case ContentItem.Status.Paused:
                                  return "Paused";
                              case ContentItem.Status.DownloadQueued:
                                  return "Queued";
                              case ContentItem.Status.DownloadWaiting:
                              case ContentItem.Status.DownloadWaitingFreeNetwork:
                                  return "Waiting";
                              case ContentItem.Status.DownloadRunning:
                                  return "Downloading";
                              case ContentItem.Status.UpdateWaiting:
                                  return "Update Waiting";
                              default:
                                  return "Unknown status = " + root.contentItem.status
                              }
                        enabled: root.contentItem.status === ContentItem.Status.Unavailable
                        onClicked: root.contentItem.asyncDownload();
                    }
                    Button {
                        text: "Cancel"
                        enabled: root.contentItem.status >= ContentItem.Status.DownloadQueued
                        onClicked: root.contentItem.cancelDownload();
                    }
                    Button {
                        text: "Pause"
                        enabled: root.contentItem.status >= ContentItem.Status.DownloadQueued
                        onClicked: root.contentItem.pauseDownload();
                    }
                    Button {
                        text: "Resume"
                        enabled: root.contentItem.status === ContentItem.Status.Paused
                        onClicked: root.contentItem.asyncDownload();
                    }
                    Button {
                        text: "Delete"
                        enabled: root.contentItem.canDeleteContent
                        onClicked: {
                            stackView.pop();
                            root.contentItem.deleteContent();
                        }
                    }
                    Button {
                        text: "Apply Map Style"
                        enabled: (root.contentItem.type === ContentItem.Type.ViewStyleHighDPI || root.contentItem.type === ContentItem.Type.ViewStyleNormalDPI) &&  root.contentItem.status === ContentItem.Status.Completed
                        onClicked: {
                            map.styleId = root.contentItem.id;
                            map.stylePath = root.contentItem.filePath;
                            map.zoomOut();
                            map.zoomIn();
                        }
                    }
                    Button {
                        text: "Back"
                        onClicked: {
                           stackView.pop();
                        }
                    }
                }
            }
        }
    }
}
