import QtQuick
import Quickshell
import Quickshell.Wayland
import Quickshell.Services.Mpris
import ".."

PanelWindow {
    id: overlay
    anchors {
        top: true
        left: true
        right: true
        bottom: true
    }

    WlrLayershell.layer: WlrLayer.Overlay
    WlrLayershell.focusable: false
    WlrLayershell.keyboardFocus: WlrKeyboardFocus.None
    WlrLayershell.namespace: "deltarune-quickshell-overlay-nonanimated"
    exclusionMode: ExclusionMode.Ignore

    mask: Region {}

    color: '#00ffffff'

    readonly property bool overlayEnabled: Boolean(ShellStateManager.global("shell.mprisOverlayEnabled", true))
    visible: overlayEnabled

    Repeater {
        model: Mpris.players

        Item {
            id: songItem

            property bool linger: false
            function showSongOverlay() {
                linger = true;
                hideTimer.restart();
            }

            Timer {
                id: hideTimer
                interval: 2000
                repeat: false
                onTriggered: songItem.linger = false
            }

            property string artistText: {
                if (modelData.trackArtist === undefined || modelData.trackArtist === null)
                    return "";
                const text = String(modelData.trackArtist);
                return text === "" ? "" : text;
            }
            property bool hasSong: modelData.trackTitle !== ""
            property string displayText: {
                if (!hasSong)
                    return "";
                if (artistText !== "")
                    return "♪ " + artistText + " - " + modelData.trackTitle;
                return "♪ " + modelData.trackTitle;
            }

            property bool active: hasSong && linger

            onHasSongChanged: {
                if (hasSong)
                    showSongOverlay();
            }

            onDisplayTextChanged: {
                if (hasSong)
                    showSongOverlay();
            }

            visible: opacity > 0
            y: 64
            x: 64
            opacity: 0

            states: [
                State {
                    name: "shown"
                    when: songItem.active
                    PropertyChanges {
                        target: songItem
                        x: 64
                        opacity: 1
                    }
                },
                State {
                    name: "hidden"
                    when: !songItem.active
                    PropertyChanges {
                        target: songItem
                        x: 64 + 32
                        opacity: 0
                    }
                }
            ]

            transitions: [
                Transition {
                    from: "hidden"
                    to: "shown"
                    ParallelAnimation {
                        NumberAnimation {
                            properties: "x"
                            from: 64 + 32
                            to: 64
                            duration: 500
                            easing.type: Easing.OutCubic
                        }
                        NumberAnimation {
                            properties: "opacity"
                            duration: 500
                        }
                    }
                },
                Transition {
                    from: "shown"
                    to: "hidden"
                    ParallelAnimation {
                        NumberAnimation {
                            properties: "x"
                            duration: 350
                            from: 64
                            to: 64 + 32
                            easing.type: Easing.InCubic
                        }
                        NumberAnimation {
                            properties: "opacity"
                            duration: 350
                        }
                    }
                }
            ]

            Text {
                x: 1
                y: 1
                text: songItem.displayText
                font.family: "8bitoperator JVE"
                font.pixelSize: 32
                font.letterSpacing: 1
                renderType: Text.NativeRendering
                font.hintingPreference: Font.PreferNoHinting
                smooth: false
                antialiasing: false
                color: "#04047c"
            }

            Text {
                x: 0
                y: 0
                text: songItem.displayText
                font.family: "8bitoperator JVE"
                font.pixelSize: 32
                font.letterSpacing: 1
                renderType: Text.NativeRendering
                font.hintingPreference: Font.PreferNoHinting
                smooth: false
                antialiasing: false
                color: "#ae00ff"
            }
        }
    }
}
