/*
    Speed Climbing Stopwatch - Simple Stopwatch for Climbers
    Copyright (C) 2018  Itsblue Development - Dorian Zeder

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, version 3 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import QtQuick 2.9
import QtMultimedia 5.8
import QtQuick.Controls 2.12
import QtGraphicalEffects 1.0
import QtQuick.Layouts 1.0

import "."
import "./components"
import "./ProfilesDialog"
import "./SettingsDialog"
import "./components/layout"
import "./MainPage"
//import QtQuick.Layouts 1.11

import de.itsblue.ScStw 2.0
import de.itsblue.ScStw.Styling 2.0
import de.itsblue.ScStw.Styling.Components 1.0
import de.itsblue.ScStwApp 2.0

ApplicationWindow {
    visible: true
    width: 540
    height: 960

    title: "ScStwApp"

    Page {
        id: app
        anchors.fill: parent

        state: scStwRemoteRace.state

        Rectangle {
            id: backgroundRect
            anchors.fill: parent
            color: appTheme.theme.colors.background

            Behavior on color {
                ColorAnimation {
                    duration: 200
                }
            }
        }

        ScStw {
            id: scStw
        }

        ScStwClient {
            id: scStwClient

            property ScStwSetting ipSetting: scStwAppSettings.getSetting(ScStwAppSettings.BaseStationIpSetting, ScStwAppSettings.KeyLevel)
            ipAddress: ipSetting.value
        }

        ScStwAppSettings {
            id: scStwAppSettings

            scStwClient: scStwClient
        }

        ScStwTimer {
            id: defaultTimer
        }

        ScStwRemoteRace {
            id: scStwRemoteRace

            autoRefreshTimerText: true
            settings: scStwAppSettings
            scStwClient: scStwClient

            Component.onCompleted: {
                scStwRemoteRace.addTimer(defaultTimer)
            }

            onStateChanged: {
                if(scStwRemoteRace.state !== ScStwRace.IDLE) {
                    settingsDialog.close()
                    profilesDialog.close()
                }
            }
        }

        SpeedBackend {
            id: speedBackend
            scStwClient: scStwClient
        }

        ScStwAppThemeManager {
            id: appTheme
            property ScStwSetting setting: scStwAppSettings.getSetting(ScStwAppSettings.AppThemeSetting, ScStwAppSettings.KeyLevel)
            themeName: setting.value
        }

        // ---------------
        // - Main layout -
        // ---------------

        GridLayout {
            id: mainLayout

            anchors.fill: parent

            columns: app.landscape() ? 3:1
            rows: app.landscape() ? 1:3

            TopToolBar {
                id: topToolBar

                state: app.state

                sizeMode: "large"
            }


            CenterContent {
                id: centerContent

                state: app.state

                Layout.fillWidth: true
                Layout.fillHeight: true
            }

            BottomToolBar {
                id: bottomToolBar

                sizeMode: "tiny"
            }

        }

        states: [
            State {
                name: ScStwRace.IDLE

                PropertyChanges {
                    target: topToolBar
                    sizeMode: "small"
                }

                PropertyChanges {
                    target: bottomToolBar
                    sizeMode: "small"
                }
            },

            State {
                name: ScStwRace.PREPAIRING

                PropertyChanges {
                    target: topToolBar
                    sizeMode: "medium"
                }

                PropertyChanges {
                    target: bottomToolBar
                    sizeMode: "tiny"
                }
            },

            State {
                name: ScStwRace.WAITING

                PropertyChanges {
                    target: topToolBar
                    sizeMode: "medium"
                }

                PropertyChanges {
                    target: bottomToolBar
                    sizeMode: "tiny"
                }
            },

            State {
                name: ScStwRace.STARTING

                PropertyChanges {
                    target: topToolBar
                    sizeMode: "medium"
                }

                PropertyChanges {
                    target: bottomToolBar
                    sizeMode: "tiny"
                }
            },

            State {
                name: ScStwRace.RUNNING

                PropertyChanges {
                    target: topToolBar
                    sizeMode: "medium"
                }

                PropertyChanges {
                    target: bottomToolBar
                    sizeMode: "tiny"
                }
            },

            State {
                name: ScStwRace.STOPPED

                PropertyChanges {
                    target: topToolBar
                    sizeMode: "large"
                }

                PropertyChanges {
                    target: bottomToolBar
                    sizeMode: "tiny"
                }
            },

            State {
                name: ScStwRace.INCIDENT

                PropertyChanges {
                    target: topToolBar
                    sizeMode: "small"
                }

                PropertyChanges {
                    target: bottomToolBar
                    sizeMode: "tiny"
                }
            }
        ]


        /*------
          Popups
        ------*/
        SettingsDialog{
            id: settingsDialog

            x: centerContent.x + (centerContent.width - width) * 0.5
            y: centerContent.y + (centerContent.height - height) *  0.5

            width: Math.min(centerContent.width * 0.9, centerContent.height * 0.9)
            height: width
        }

        ProfilesDialog {
            id: profilesDialog

            property int margin: app.landscape() ? app.height * 0.05:app.width * 0.05

            x: centerContent.x + margin
            y: centerContent.y + margin
            width: centerContent.width - margin * 2
            height: centerContent.height - margin * 2
        }

        function landscape(){
            return(app.height < app.width)
        }
    }
}
