pragma Singleton

import ZShell.Config
import Quickshell
import Quickshell.Services.Notifications
import QtQuick

Singleton {
	id: root

	readonly property var categoryIcons: ({
			WebBrowser: "web",
			Printing: "print",
			Security: "security",
			Network: "chat",
			Archiving: "archive",
			Compression: "archive",
			Development: "code",
			IDE: "code",
			TextEditor: "edit_note",
			Audio: "music_note",
			Music: "music_note",
			Player: "music_note",
			Recorder: "mic",
			Game: "sports_esports",
			FileTools: "files",
			FileManager: "files",
			Filesystem: "files",
			FileTransfer: "files",
			Settings: "settings",
			DesktopSettings: "settings",
			HardwareSettings: "settings",
			TerminalEmulator: "terminal",
			ConsoleOnly: "terminal",
			Utility: "build",
			Monitor: "monitor_heart",
			Midi: "graphic_eq",
			Mixer: "graphic_eq",
			AudioVideoEditing: "video_settings",
			AudioVideo: "music_video",
			Video: "videocam",
			Building: "construction",
			Graphics: "photo_library",
			"2DGraphics": "photo_library",
			RasterGraphics: "photo_library",
			TV: "tv",
			System: "host",
			Office: "content_paste"
		})
	readonly property var weatherIcons: ({
			"0": "clear_day",
			"1": "clear_day",
			"2": "partly_cloudy_day",
			"3": "cloud",
			"45": "foggy",
			"48": "foggy",
			"51": "rainy",
			"53": "rainy",
			"55": "rainy",
			"56": "rainy",
			"57": "rainy",
			"61": "rainy",
			"63": "rainy",
			"65": "rainy",
			"66": "rainy",
			"67": "rainy",
			"71": "cloudy_snowing",
			"73": "cloudy_snowing",
			"75": "snowing_heavy",
			"77": "cloudy_snowing",
			"80": "rainy",
			"81": "rainy",
			"82": "rainy",
			"85": "cloudy_snowing",
			"86": "snowing_heavy",
			"95": "thunderstorm",
			"96": "thunderstorm",
			"99": "thunderstorm"
		})

	function getAppCategoryIcon(name: string, fallback: string): string {
		const categories = DesktopEntries.heuristicLookup(name)?.categories;

		if (categories)
			for (const [key, value] of Object.entries(categoryIcons))
				if (categories.includes(key))
					return value;
		return fallback;
	}

	function getAppIcon(name: string, fallback: string): string {
		const icon = DesktopEntries.heuristicLookup(name)?.icon;
		if (fallback !== "undefined")
			return Quickshell.iconPath(icon, fallback);
		return Quickshell.iconPath(icon);
	}

	function getBluetoothIcon(icon: string): string {
		if (icon.includes("headset") || icon.includes("headphones"))
			return "headphones";
		if (icon.includes("audio"))
			return "speaker";
		if (icon.includes("phone"))
			return "smartphone";
		if (icon.includes("mouse"))
			return "mouse";
		if (icon.includes("keyboard"))
			return "keyboard";
		return "bluetooth";
	}

	function getMicVolumeIcon(volume: real, isMuted: bool): string {
		if (!isMuted && volume > 0)
			return "mic";
		return "mic_off";
	}

	function getNetworkIcon(strength: int, isSecure = false): string {
		if (isSecure) {
			if (strength >= 80)
				return "network_wifi_locked";
			if (strength >= 60)
				return "network_wifi_3_bar_locked";
			if (strength >= 40)
				return "network_wifi_2_bar_locked";
			if (strength >= 20)
				return "network_wifi_1_bar_locked";
			return "signal_wifi_0_bar";
		} else {
			if (strength >= 80)
				return "network_wifi";
			if (strength >= 60)
				return "network_wifi_3_bar";
			if (strength >= 40)
				return "network_wifi_2_bar";
			if (strength >= 20)
				return "network_wifi_1_bar";
			return "signal_wifi_0_bar";
		}
	}

	function getNotifIcon(summary: string, urgency: int): string {
		summary = summary.toLowerCase();
		if (summary.includes("reboot"))
			return "restart_alt";
		if (summary.includes("recording"))
			return "screen_record";
		if (summary.includes("battery"))
			return "power";
		if (summary.includes("screenshot"))
			return "screenshot_monitor";
		if (summary.includes("welcome"))
			return "waving_hand";
		if (summary.includes("time") || summary.includes("a break"))
			return "schedule";
		if (summary.includes("installed"))
			return "download";
		if (summary.includes("update"))
			return "update";
		if (summary.includes("unable to"))
			return "deployed_code_alert";
		if (summary.includes("profile"))
			return "person";
		if (summary.includes("file"))
			return "folder_copy";
		if (urgency === NotificationUrgency.Critical)
			return "release_alert";
		return "chat";
	}

	function getVolumeIcon(volume: real, isMuted: bool): string {
		if (isMuted)
			return "no_sound";
		if (volume >= 0.5)
			return "volume_up";
		if (volume > 0)
			return "volume_down";
		return "volume_mute";
	}

	function getWeatherIcon(code: string): string {
		if (weatherIcons.hasOwnProperty(code))
			return weatherIcons[code];
		return "air";
	}
}
