/*
 * Copyright (C) 2020-2026 Savoir-faire Linux Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import net.jami.Adapters 1.1
import net.jami.Constants 1.1
import net.jami.Enums 1.1
import net.jami.Models 1.1

// Import qml component files.
import "components"
import "../"
import "../wizardview"
import "../settingsview"
import "../settingsview/components"
import "../commoncomponents"

import "js/keyboardshortcuttablecreation.js" as KeyboardShortcutTableCreation

Item {
    id: mainView
    objectName: "mainView"

    property var appContext: null
    readonly property var viewCoordinator: appContext ? appContext.viewCoordinator : null

    property string currentConvId: CurrentConversation.id
    onCurrentConvIdChanged: {
        if (!viewCoordinator) {
            return;
        } else if (currentConvId !== '') {
            viewCoordinator.present("ConversationView");
        } else {
            viewCoordinator.present("WelcomePage");
        }
    }
    // These color properties are kept here so that BackgroundGradient (and other
    // components) can read them via JamiQmlUtils.mainViewRectObj. The actual
    // background visuals are rendered by MainApplicationWindow directly.
    property color currentConversationColor: CurrentConversation.color
    property color tintColor: Qt.rgba(currentConversationColor.r, currentConversationColor.g, currentConversationColor.b, JamiTheme.chatViewBackgroundTintOpacity)
    property real tintOpacity: CurrentConversation.id === "" ? 0.0 : 1.0
    property color baseColor: Qt.tint(JamiTheme.globalBackgroundColor, tintColor)

    Behavior on baseColor {
        ColorAnimation {
            duration: JamiTheme.chatViewFadeDuration
            easing.type: Easing.InOutQuad
        }
    }

    Behavior on tintOpacity {
        NumberAnimation {
            duration: JamiTheme.longFadeDuration
            easing.type: Easing.InOutQuad
        }
    }
    onWidthChanged: Qt.callLater(JamiQmlUtils.updateMessageBarButtonsPoints)
    onHeightChanged: Qt.callLater(JamiQmlUtils.updateMessageBarButtonsPoints)

    Component.onCompleted: {
        JamiQmlUtils.mainViewRectObj = mainView;
    }

    WheelHandler {
        onWheel: wheel => {
            if (wheel.modifiers & Qt.ControlModifier) {
                var delta = wheel.angleDelta.y / 120;
                UtilsAdapter.setAppValue(Settings.BaseZoom, parseFloat(UtilsAdapter.getAppValue(Settings.BaseZoom)) + delta * 0.1);
            }
        }
    }

    // Bind to requests for a settings page to be selected via shortcut.
    Connections {
        target: JamiQmlUtils
        function onSettingsPageRequested(index) {
            if (!viewCoordinator)
                return;
            viewCoordinator.present("SettingsView");
            const settingsView = viewCoordinator.getView("SettingsView");
            if (!settingsView)
                return;
            settingsView.leftPaneItem.select(index);
        }
    }

    Shortcut {
        sequence: "Ctrl+M"
        context: Qt.ApplicationShortcut
        onActivated: JamiQmlUtils.requestSettingsPage(12)
    }

    Shortcut {
        sequence: "Ctrl++"
        context: Qt.ApplicationShortcut
        onActivated: {
            UtilsAdapter.setAppValue(Settings.BaseZoom, parseFloat(UtilsAdapter.getAppValue(Settings.BaseZoom)) + 0.1);
        }
    }

    Shortcut {
        sequence: "Ctrl+="
        context: Qt.ApplicationShortcut
        onActivated: {
            UtilsAdapter.setAppValue(Settings.BaseZoom, parseFloat(UtilsAdapter.getAppValue(Settings.BaseZoom)) + 0.1);
        }
    }

    Shortcut {
        sequence: "Ctrl+-"
        context: Qt.ApplicationShortcut
        onActivated: {
            UtilsAdapter.setAppValue(Settings.BaseZoom, parseFloat(UtilsAdapter.getAppValue(Settings.BaseZoom)) - 0.1);
        }
    }

    Shortcut {
        sequence: "Ctrl+_"
        context: Qt.ApplicationShortcut
        onActivated: {
            UtilsAdapter.setAppValue(Settings.BaseZoom, parseFloat(UtilsAdapter.getAppValue(Settings.BaseZoom)) - 0.1);
        }
    }

    Shortcut {
        sequence: "Ctrl+0"
        context: Qt.ApplicationShortcut
        onActivated: UtilsAdapter.setAppValue(Settings.BaseZoom, 1.0)
    }

    Shortcut {
        sequence: "Ctrl+G"
        context: Qt.ApplicationShortcut
        onActivated: JamiQmlUtils.requestSettingsPage(5)
    }

    Shortcut {
        sequence: "Ctrl+Alt+I"
        context: Qt.ApplicationShortcut
        onActivated: JamiQmlUtils.requestSettingsPage(0)
    }

    Shortcut {
        sequence: "Ctrl+E"
        context: Qt.ApplicationShortcut
        onActivated: JamiQmlUtils.requestSettingsPage(15)
    }

    Shortcut {
        sequence: "F10"
        context: Qt.ApplicationShortcut
        onActivated: {
            KeyboardShortcutTableCreation.createKeyboardShortcutTableWindowObject(appWindow);
            KeyboardShortcutTableCreation.showKeyboardShortcutTableWindow();
        }
    }

    Shortcut {
        sequence: "Ctrl+D"
        context: Qt.ApplicationShortcut
        onActivated: CallAdapter.endCall()
        onActivatedAmbiguously: CallAdapter.endCall()
    }

    Shortcut {
        sequence: "Ctrl+Shift+A"
        context: Qt.ApplicationShortcut
        onActivated: LRCInstance.makeConversationPermanent()
    }

    Shortcut {
        sequence: "Ctrl+Shift+N"
        context: Qt.ApplicationShortcut
        onActivated: viewCoordinator.present("WizardView")
    }

    Shortcut {
        sequences: [StandardKey.Quit]
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }
}
