// The message tray inside the date menu: everything that has arrived, newest
// first, grouped by app, scrolling once it outgrows `maxHeight`.

import QtQuick
import qs.config
import qs.services
import qs.modules.quicksettings

Item {
    id: root

    property int maxHeight: 420

    implicitHeight: Notifs.hasNotifications ? list.implicitHeight : empty.implicitHeight

    // Which app groups are collapsed, keyed by app name. Kept here rather than
    // on the group itself because Notifs.groups is rebuilt from scratch on
    // every change to history, which destroys and recreates the delegates.
    property var collapsedApps: ({})

    function toggleApp(app: string): void {
        // Reassigned rather than mutated: a property holding a JS object only
        // re-evaluates bindings when the reference itself changes.
        const next = Object.assign({}, root.collapsedApps);
        if (next[app])
            delete next[app];
        else
            next[app] = true;
        root.collapsedApps = next;
    }

    // Called when the panel closes, so it always reopens fully expanded.
    function reset(): void {
        root.collapsedApps = ({});
    }

    Item {
        id: empty
        anchors.fill: parent
        implicitHeight: 132
        visible: !Notifs.hasNotifications

        Text {
            anchors.centerIn: parent
            text: Notifs.doNotDisturb ? "Do Not Disturb is on" : "No notifications"
            color: Theme.fgMuted
            font.family: Theme.fontFamily
            font.pixelSize: Theme.fontSize
        }
    }

    ScrollColumn {
        id: list
        anchors.fill: parent
        maxHeight: root.maxHeight
        spacing: Theme.itemSpacing
        visible: Notifs.hasNotifications

        Repeater {
            model: Notifs.groups

            NotificationGroup {
                required property var modelData

                width: list.width
                group: modelData
                expanded: !root.collapsedApps[modelData.app]
                onToggled: root.toggleApp(modelData.app)
                onCleared: Notifs.dismissApp(modelData.app)
            }
        }
    }
}
