pragma ComponentBehavior: Bound

import Quickshell
import QtQuick
import qs.Components
import qs.Helpers
import ZShell.Config
import qs.Daemons

Item {
	id: root

	property real brightness
	property bool hovered
	readonly property Brightness.Monitor monitor: Brightness.getMonitorForScreen(root.screen)
	property bool muted
	property real offsetScale: shouldBeActive ? 0 : 1
	required property ShellScreen screen
	readonly property bool shouldBeActive: visibilities.osd && Config.osd.enabled && !(visibilities.utilities && Config.utilities.enabled)
	property real sidebarOffset: sidebarOrSessionVisible ? 12 : 0
	required property bool sidebarOrSessionVisible
	property bool sourceMuted
	property real sourceVolume
	required property var visibilities
	property real volume

	function show(): void {
		visibilities.osd = true;
		timer.restart();
	}

	anchors.rightMargin: (-implicitWidth - 5 - sidebarOffset) * offsetScale
	implicitHeight: content.implicitHeight
	implicitWidth: content.implicitWidth
	opacity: 1 - offsetScale
	visible: offsetScale < 1

	Behavior on offsetScale {
		Anim {
			duration: Tokens.anim.durations.expressiveDefaultSpatial
			easing: Tokens.anim.expressiveDefaultSpatial
		}
	}

	Component.onCompleted: {
		volume = Audio.volume;
		muted = Audio.muted;
		sourceVolume = Audio.sourceVolume;
		sourceMuted = Audio.sourceMuted;
		brightness = root.monitor?.brightness ?? 0;
	}

	Connections {
		function onMutedChanged(): void {
			root.show();
			root.muted = Audio.muted;
		}

		function onSourceMutedChanged(): void {
			root.show();
			root.sourceMuted = Audio.sourceMuted;
		}

		function onSourceVolumeChanged(): void {
			root.show();
			root.sourceVolume = Audio.sourceVolume;
		}

		function onVolumeChanged(): void {
			root.show();
			root.volume = Audio.volume;
		}

		target: Audio
	}

	Connections {
		function onBrightnessChanged(): void {
			root.show();
			root.brightness = root.monitor?.brightness ?? 0;
		}

		target: root.monitor
	}

	Timer {
		id: timer

		interval: Config.osd.hideDelay

		onTriggered: {
			if (!root.hovered)
				root.visibilities.osd = false;
		}
	}

	Loader {
		id: content

		active: root.shouldBeActive || root.visible
		anchors.left: parent.left
		anchors.verticalCenter: parent.verticalCenter

		sourceComponent: Content {
			brightness: root.brightness
			monitor: root.monitor
			muted: root.muted
			sourceMuted: root.sourceMuted
			sourceVolume: root.sourceVolume
			visibilities: root.visibilities
			volume: root.volume
		}
	}
}
