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

import QtQuick
import QtQuick.Shapes
import org.kde.kwin.decoration
import org.kde.plasma.private.mobileshell as MobileShell
import org.kde.plasma.private.mobileshell.shellsettingsplugin as ShellSettings

Decoration {
    id: root
    alpha: true

    // ── Palette ─────────────────────────────────────────────────────────────
    readonly property color activeBar:   "#1a1d2e"
    readonly property color inactiveBar: "#141620"
    readonly property color activeText:  "#f0f0f8"
    readonly property color inactiveText: "#505570"

    readonly property int barHeight:     30
    readonly property int btnSize:       16
    readonly property int btnSpacing:     8
    readonly property int btnSideMargin: 12
    readonly property int normalCornerRadius: 8
    readonly property int cornerRadius:  decoration.client.maximized ? 0 : normalCornerRadius
    readonly property int frameThickness: decoration.client.maximized ? 0 : normalCornerRadius
    readonly property int shortAnimationDuration: MobileShell.Motion.duration(MobileShell.Motion.EffectsFast)
    readonly property bool windowMenuAllowed: !ShellSettings.Settings.convergenceModeEnabled
                                           || ShellSettings.Settings.gamingModeEnabled
                                           || !ShellSettings.Settings.dynamicTilingEnabled

    Component.onCompleted: {
        borders.top    = barHeight;
        borders.left   = normalCornerRadius;
        borders.right  = normalCornerRadius;
        borders.bottom = normalCornerRadius;

        // Keep titlebar controls available for maximized windows in desktop
        // convergence mode. Mobile mode uses noBorder=true and bypasses this.
        maximizedBorders.top    = barHeight;
        maximizedBorders.left   = 0;
        maximizedBorders.right  = 0;
        maximizedBorders.bottom = 0;
    }

    DecorationOptions {
        id: options
        deco: decoration
    }

    readonly property color frameColor: decoration.client.active ? root.activeBar : root.inactiveBar

    Shape {
        anchors.fill: parent
        visible: !decoration.client.maximized

        ShapePath {
            fillColor: root.frameColor
            fillRule: ShapePath.OddEvenFill
            strokeWidth: 0

            startX: root.cornerRadius
            startY: 0
            PathLine { x: root.width - root.cornerRadius; y: 0 }
            PathArc { x: root.width; y: root.cornerRadius; radiusX: root.cornerRadius; radiusY: root.cornerRadius }
            PathLine { x: root.width; y: root.height - root.cornerRadius }
            PathArc { x: root.width - root.cornerRadius; y: root.height; radiusX: root.cornerRadius; radiusY: root.cornerRadius }
            PathLine { x: root.cornerRadius; y: root.height }
            PathArc { x: 0; y: root.height - root.cornerRadius; radiusX: root.cornerRadius; radiusY: root.cornerRadius }
            PathLine { x: 0; y: root.cornerRadius }
            PathArc { x: root.cornerRadius; y: 0; radiusX: root.cornerRadius; radiusY: root.cornerRadius }

            PathMove { x: root.frameThickness; y: root.barHeight }
            PathLine { x: root.width - root.frameThickness; y: root.barHeight }
            PathLine { x: root.width - root.frameThickness; y: root.height - root.frameThickness }
            PathLine { x: root.frameThickness; y: root.height - root.frameThickness }
            PathLine { x: root.frameThickness; y: root.barHeight }
        }
    }

    // ── Faint window outline ─────────────────────────────────────────────────
    Rectangle {
        anchors.fill: parent
        color: "transparent"
        radius: root.cornerRadius
        border.width: decoration.client.maximized ? 0 : 1
        border.color: Qt.rgba(1, 1, 1, 0.08)
    }

    // ── Title bar ────────────────────────────────────────────────────────────
    Rectangle {
        id: bar
        anchors { left: parent.left; right: parent.right; top: parent.top }
        height: root.barHeight
        radius: root.cornerRadius
        color: decoration.client.active ? root.activeBar : root.inactiveBar
        Behavior on color { MobileShell.MotionColorAnimation { type: MobileShell.Motion.EffectsFast; duration: root.shortAnimationDuration } }

        // Square off bottom half — only top corners are rounded
        Rectangle {
            anchors { left: parent.left; right: parent.right; bottom: parent.bottom }
            height: root.cornerRadius
            color: parent.color
            visible: !decoration.client.maximized
        }

        // ── Title row ────────────────────────────────────────────────────────
        Item {
            id: titleRow
            anchors.fill: parent

            Row {
                id: leftRow
                spacing: root.btnSpacing
                anchors {
                    left: parent.left
                    leftMargin: root.btnSideMargin
                    verticalCenter: parent.verticalCenter
                }
                Repeater {
                    model: options.titleButtonsLeft
                    delegate: ShiftButton { btnType: modelData }
                }
            }

            Text {
                anchors {
                    left: leftRow.right; leftMargin: 6
                    right: rightRow.left; rightMargin: 6
                    verticalCenter: parent.verticalCenter
                }
                text: decoration.client.caption
                color: decoration.client.active ? root.activeText : root.inactiveText
                font: options.titleFont
                elide: Text.ElideMiddle
                horizontalAlignment: Text.AlignHCenter
                renderType: Text.NativeRendering
                Behavior on color { MobileShell.MotionColorAnimation { type: MobileShell.Motion.EffectsFast; duration: root.shortAnimationDuration } }
            }

            Row {
                id: rightRow
                spacing: root.btnSpacing
                anchors {
                    right: parent.right
                    rightMargin: root.btnSideMargin
                    verticalCenter: parent.verticalCenter
                }
                Repeater {
                    model: options.titleButtonsRight
                    delegate: ShiftButton { btnType: modelData }
                }
            }

            MouseArea {
                anchors.fill: parent
                z: 1
                enabled: !root.windowMenuAllowed
                acceptedButtons: Qt.RightButton
                onPressed: (mouse) => { mouse.accepted = true }
            }

            Component.onCompleted: decoration.installTitleItem(titleRow)
        }
    }

    // ── Button component ─────────────────────────────────────────────────────
    component ShiftButton: DecorationButton {
        property int btnType: DecorationOptions.DecorationButtonNone
        readonly property bool isSpacer: btnType === DecorationOptions.DecorationButtonExplicitSpacer
        readonly property bool supported: {
            switch (btnType) {
            case DecorationOptions.DecorationButtonExplicitSpacer:
            case DecorationOptions.DecorationButtonClose:
            case DecorationOptions.DecorationButtonMinimize:
            case DecorationOptions.DecorationButtonMaximizeRestore:
                return true;
            case DecorationOptions.DecorationButtonMenu:
            case DecorationOptions.DecorationButtonApplicationMenu:
                return root.windowMenuAllowed;
            default:
                return false;
            }
        }
        buttonType: btnType
        width: isSpacer ? root.btnSpacing * 2 : (supported ? root.btnSize : 0)
        height: isSpacer ? 1 : (supported ? root.btnSize : 0)
        visible: supported

        readonly property color normalColor: {
            switch (btnType) {
            case DecorationOptions.DecorationButtonClose:           return "#C4455D";
            case DecorationOptions.DecorationButtonMenu:
            case DecorationOptions.DecorationButtonApplicationMenu:
            case DecorationOptions.DecorationButtonMinimize:
            case DecorationOptions.DecorationButtonMaximizeRestore: return "#2b3246";
            default:                                                return "#2b3246";
            }
        }
        readonly property color hoverColor: {
            switch (btnType) {
            case DecorationOptions.DecorationButtonClose:           return "#E05D76";
            case DecorationOptions.DecorationButtonMinimize:
            case DecorationOptions.DecorationButtonMaximizeRestore:
            case DecorationOptions.DecorationButtonMenu:
            case DecorationOptions.DecorationButtonApplicationMenu: return "#3b435c";
            default:                                                return "#3b435c";
            }
        }
        readonly property color symbolColor: {
            switch (btnType) {
            case DecorationOptions.DecorationButtonClose:           return "#ffffff";
            case DecorationOptions.DecorationButtonMenu:
            case DecorationOptions.DecorationButtonApplicationMenu:
            case DecorationOptions.DecorationButtonMinimize:
            case DecorationOptions.DecorationButtonMaximizeRestore: return "#eaf2ff";
            default:                                                return "#eaf2ff";
            }
        }
        readonly property string symbol: {
            switch (btnType) {
            case DecorationOptions.DecorationButtonClose:           return "\u00d7";
            case DecorationOptions.DecorationButtonMinimize:        return "\u2212";
            case DecorationOptions.DecorationButtonMaximizeRestore:
                return decoration.client.maximized ? "\u25a3" : "\u25a1";
            case DecorationOptions.DecorationButtonMenu:
            case DecorationOptions.DecorationButtonApplicationMenu: return "\u2261";
            default: return "";
            }
        }

        // Snap-layout hover activation lives in the SHIFT Snap Assist effect.
        // The decoration only renders the maximize button; the effect decides
        // when maximize-hover is eligible and shows the popup.

        Rectangle {
            visible: !isSpacer
            anchors.fill: parent
            radius: width / 2
            antialiasing: true
            border.width: 1
            border.color: Qt.rgba(1, 1, 1, 0.18)
            color: parent.pressed  ? Qt.darker(parent.hoverColor, 1.3)
                 : parent.hovered  ? parent.hoverColor
                                   : parent.normalColor
                            Behavior on color { MobileShell.MotionColorAnimation { type: MobileShell.Motion.EffectsFast; duration: root.shortAnimationDuration } }

            Text {
                anchors.centerIn: parent
                text: parent.parent.symbol
                color: parent.parent.symbolColor
                font.pixelSize: Math.round(parent.width * 0.66)
                font.weight: Font.Bold
                opacity: 1.0
                Behavior on opacity { MobileShell.MotionNumberAnimation { type: MobileShell.Motion.EffectsFast; duration: root.shortAnimationDuration } }
            }
        }
    }
}
