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

import "Healthbar"

PanelWindow {
    id: healthbarWindow

    anchors {
        left: true
        right: true
        bottom: true
    }

    WlrLayershell.layer: WlrLayer.Top
    WlrLayershell.focusable: false
    WlrLayershell.keyboardFocus: WlrKeyboardFocus.None
    WlrLayershell.namespace: "deltarune-quickshell-bottom"

    visible: ShellStateManager.shellOpen
    exclusionMode: ExclusionMode.Ignore
    aboveWindows: true
    focusable: false

    mask: Region {}

    implicitHeight: 137
    color: "#000000"

    property string currentSongText: "♪ NO SONG"

    function formatSong(player) {
        if (!player)
            return "";

        const title = String(player.trackTitle || "");
        if (title.length === 0)
            return "";

        const artist = String(player.trackArtist || "");
        const album = String(player.trackAlbum || "");
        const core = artist.length > 0 ? (artist + " - " + title) : title;
        return album.length > 0 ? (core + " (" + album + ")") : core;
    }

    function updateSongText() {
        let playingSong = "";
        let fallbackSong = "";

        for (let i = 0; i < mprisRepeater.count; i++) {
            const item = mprisRepeater.itemAt(i);
            if (!item || !item.player)
                continue;

            const candidate = formatSong(item.player);
            if (candidate.length === 0)
                continue;

            if (fallbackSong.length === 0)
                fallbackSong = candidate;

            if (item.player.isPlaying) {
                playingSong = candidate;
                break;
            }
        }

        const chosen = playingSong.length > 0 ? playingSong : fallbackSong;
        currentSongText = chosen.length > 0 ? ("♪ " + chosen) : "♪ NO SONG";
    }

    Repeater {
        id: mprisRepeater
        model: Mpris.players

        delegate: Item {
            required property var modelData
            property var player: modelData
            visible: false

            Connections {
                target: player
                function onTrackChanged() {
                    healthbarWindow.updateSongText();
                }
                function onPostTrackChanged() {
                    healthbarWindow.updateSongText();
                }
                function onPlaybackStateChanged() {
                    healthbarWindow.updateSongText();
                }
            }
        }
    }

    Connections {
        target: Mpris.players
        ignoreUnknownSignals: true
        function onCountChanged() {
            healthbarWindow.updateSongText();
        }
        function onModelReset() {
            healthbarWindow.updateSongText();
        }
        function onRowsInserted() {
            healthbarWindow.updateSongText();
        }
        function onRowsRemoved() {
            healthbarWindow.updateSongText();
        }
    }

    Healthbar {}

    Text {
        x: parent.width - width - 24 + 1
        y: parent.height - height - 8 + 1
        width: Math.floor(parent.width * 0.45)
        elide: Text.ElideRight
        color: "#04047c"
        text: healthbarWindow.currentSongText
        font.family: "8bitoperator JVE"
        font.pixelSize: 28
        font.letterSpacing: 1
        renderType: Text.NativeRendering
        font.hintingPreference: Font.PreferNoHinting
        smooth: false
        antialiasing: false
        horizontalAlignment: Text.AlignRight
    }

    Text {
        x: parent.width - width - 24
        y: parent.height - height - 8
        width: Math.floor(parent.width * 0.45)
        elide: Text.ElideRight
        color: "#ffffff"
        text: healthbarWindow.currentSongText
        font.family: "8bitoperator JVE"
        font.pixelSize: 28
        font.letterSpacing: 1
        renderType: Text.NativeRendering
        font.hintingPreference: Font.PreferNoHinting
        smooth: false
        antialiasing: false
        horizontalAlignment: Text.AlignRight
    }

    Component.onCompleted: updateSongText()
}
