// SPDX-FileCopyrightText: 2026 Marco Allegretti
// SPDX-License-Identifier: EUPL-1.2

import QtQuick
import QtQuick.Controls as QQC2

import org.kde.kirigami as Kirigami
import org.kde.plasma.private.mobileshell as MobileShell

/**
 * Popup hosting a Plasma applet's fullRepresentation for convergence mode.
 */
QQC2.Popup {
    id: popup

    modal: true
    dim: true
    closePolicy: QQC2.Popup.CloseOnEscape | QQC2.Popup.CloseOnPressOutside

    x: parent ? Math.round((parent.width - width) / 2) : 0
    y: parent ? Math.round((parent.height - height) / 2) : 0

    width: Math.min(Kirigami.Units.gridUnit * 18,
                    parent ? parent.width - Kirigami.Units.gridUnit * 4 : 360)
    height: Math.min(Kirigami.Units.gridUnit * 22,
                     parent ? parent.height - Kirigami.Units.gridUnit * 4 : 440)

    padding: Kirigami.Units.smallSpacing

    readonly property int popupAnimationDuration: MobileShell.Motion.duration(MobileShell.Motion.EffectsFast)
    property string currentPluginId: ""
    property Item __currentItem: null

    function show(pluginId) {
        if (!pluginId) return;

        if (__currentItem && pluginId !== currentPluginId) {
            __currentItem.parent = null;
            __currentItem.visible = false;
            __currentItem = null;
        }

        currentPluginId = pluginId;

        var item = MobileShell.AppletHost.fullRepresentationFor(pluginId);
        if (!item) {
            console.warn("DetailPopup: no fullRepresentation for", pluginId);
            return;
        }

        __currentItem = item;
        item.parent = content;
        item.anchors.fill = content;
        item.visible = true;

        popup.open();
    }

    Connections {
        target: MobileShell.AppletHost

        function onAppletReady(pluginId) {
            if (pluginId === popup.currentPluginId && !popup.opened) {
                popup.show(pluginId);
            }
        }
    }

    onClosed: {
        if (__currentItem) {
            __currentItem.visible = false;
        }
    }

    enter: Transition {
        MobileShell.MotionNumberAnimation { property: "opacity"; from: 0; to: 1; type: MobileShell.Motion.EffectsFast }
        MobileShell.MotionNumberAnimation { property: "scale"; from: 0.9; to: 1; type: MobileShell.Motion.EffectsFast }
    }
    exit: Transition {
        MobileShell.MotionNumberAnimation { property: "opacity"; from: 1; to: 0; type: MobileShell.Motion.StandardAccel; duration: popup.popupAnimationDuration }
    }

    background: Kirigami.ShadowedRectangle {
        color: Kirigami.Theme.backgroundColor
        radius: Kirigami.Units.cornerRadius

        border.color: Kirigami.ColorUtils.linearInterpolation(
            Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, 0.2)
        border.width: 1

        shadow.size: Kirigami.Units.gridUnit
        shadow.color: Qt.rgba(0, 0, 0, 0.45)
        shadow.yOffset: 2
    }

    contentItem: Item {
        id: content
    }

    QQC2.Overlay.modal: Rectangle {
        color: Qt.rgba(0, 0, 0, 0.5)
    }
}
