import QtQuick.Layouts
import ZShell.Config
import qs.Modules.Settings.Common

PageBase {
	id: root

	readonly property var builtinIcons: ({
			audio: qsTr("Speakers"),
			microphone: qsTr("Microphone"),
			power: qsTr("Battery")
		})

	isSubPage: true
	title: qsTr("System icons")

	ColumnLayout {
		anchors.horizontalCenter: parent.horizontalCenter
		anchors.top: parent.top
		spacing: Tokens.spacing.extraSmall / 2
		width: root.cappedWidth

		// Visible icons
		SectionHeader {
			first: true
			text: qsTr("Visible icons")
		}

		ListEditor {
			function labelFor(item: var): string {
				const prettyName = root.builtinIcons[item.id];
				if (prettyName)
					return prettyName;
				const label = item.id.replace(/([A-Z])/g, " $1");
				return label.charAt(0).toUpperCase() + label.slice(1).toLowerCase();
			}

			function toggledFor(item: var): bool {
				return item.enabled;
			}

			first: true
			values: Config.bar.tray.statusIcons.values
			z: 1

			onItemMoved: (from, to) => Config.bar.tray.statusIcons.move(from, to)
			onItemRemoved: index => Config.bar.tray.statusIcons.remove(index)
			onItemToggled: (index, checked) => Config.bar.tray.statusIcons.at(index).enabled = checked
		}

		DialogSelectButton {
			id: addItemContainer

			acceptLabel: qsTr("Add")
			enabled: Object.keys(model).length > 0
			header: qsTr("Add new entry")
			icon: "add"
			settingAnchor: "panels-bar-status-icons-add-item"
			onOpenChanged: {
				root.sState.dimmed = open;
			}
			last: true
			label: qsTr("Add entry")
			model: {
				const present = new Set(Config.bar.tray.statusIcons.values.map(item => item.id));
				return Object.keys(root.builtinIcons).filter(id => !present.has(id)).map(id => ({
							id: id,
							label: root.builtinIcons[id]
						}));
			}
			rootParent: root.flickable

			onAccepted: {
				if (!selectedItem)
					return;

				Config.bar.tray.statusIcons.insert({
					id: selectedItem,
					enabled: true
				});
			}
		}

		// Behavior
		SectionHeader {
			text: qsTr("Behavior")
		}

		ToggleRow {
			checked: Config.bar.popouts.statusIcons
			first: true
			last: true
			settingAnchor: "bar-status-popout-on-hover"
			subtext: qsTr("Show a details popout when hovering the status icons")
			text: qsTr("Popout on hover")

			onToggled: Config.bar.popouts.statusIcons = checked
		}
	}
}
