pragma ComponentBehavior: Bound

import QtQuick
import QtQuick.Layouts
import Quickshell
import Quickshell.Bluetooth
import Quickshell.Networking as QSNetwork
import ZShell.Components
import ZShell.Config
import qs.Components
import qs.Services
import qs.Helpers
import qs.Daemons
import qs.Modules.Settings

CustomRect {
	id: root

	readonly property bool needExtraRow: quickToggles.length > 6
	readonly property var quickToggles: {
		const seenIds = new Set();

		return Config.utilities.quickToggles.values.filter(item => {
			if (!item.enabled)
				return false;

			if (seenIds.has(item.id)) {
				return false;
			}

			if (item.id === "bluetooth" && !Bluetooth.defaultAdapter)
				return false;

			if (item.id === "wifi" && !QSNetwork.Networking.devices.values.some(n => n.type === QSNetwork.DeviceType.Wifi))
				return false;

			seenIds.add(item.id);
			return true;
		});
	}
	readonly property int splitIndex: Math.ceil(quickToggles.length / 2)
	required property PersistentProperties visibilities

	color: Colors.tPalette.m3surfaceContainer
	implicitHeight: layout.implicitHeight + Tokens.padding.extraLargeIncreased
	radius: Tokens.rounding.small

	ColumnLayout {
		id: layout

		anchors.fill: parent
		anchors.margins: Tokens.padding.large
		spacing: Tokens.spacing.medium

		CustomText {
			font.pointSize: Tokens.font.size.normal
			text: qsTr("Quick Toggles")
		}

		QuickToggleRow {
			model: root.needExtraRow ? root.quickToggles.slice(0, root.splitIndex) : root.quickToggles
		}

		QuickToggleRow {
			model: root.needExtraRow ? root.quickToggles.slice(root.splitIndex) : []
			visible: root.needExtraRow
		}
	}

	component QuickToggleRow: ButtonRow {
		property alias model: repeater.model

		Layout.fillWidth: true
		spacing: Tokens.spacing.small

		Repeater {
			id: repeater

			delegate: DelegateChooser {
				role: "id"

				DelegateChoice {
					roleValue: "wifi"

					delegate: Toggle {
						checked: Network.wifiEnabled
						icon: checked ? "wifi" : "wifi_off"

						onClicked: Network.toggleWifi()
					}
				}

				DelegateChoice {
					roleValue: "bluetooth"

					delegate: Toggle {
						checked: Bluetooth.defaultAdapter?.enabled ?? false // qmllint disable unresolved-type
						icon: "bluetooth"

						onClicked: {
							const adapter = Bluetooth.defaultAdapter; // qmllint disable unresolved-type
							if (adapter)
								adapter.enabled = !adapter.enabled;
						}
					}
				}

				DelegateChoice {
					roleValue: "mic"

					delegate: Toggle {
						checked: !Audio.sourceMuted
						icon: checked ? "mic" : "mic_off"

						onClicked: {
							const audio = Audio.source?.audio;
							if (audio)
								audio.muted = !audio.muted;
						}
					}
				}

				DelegateChoice {
					roleValue: "audio"

					delegate: Toggle {
						checked: !Audio.muted
						icon: checked ? "volume_up" : "volume_off"

						onClicked: {
							const audio = Audio.sink?.audio;
							if (audio)
								audio.muted = !audio.muted;
						}
					}
				}

				DelegateChoice {
					roleValue: "gameMode"

					delegate: Toggle {
						checked: GameMode.enabled
						icon: "gamepad"

						onClicked: GameMode.enabled = !GameMode.enabled
					}
				}

				DelegateChoice {
					roleValue: "dnd"

					delegate: Toggle {
						checked: NotifServer.dnd
						icon: "notifications_off"

						onClicked: NotifServer.dnd = !NotifServer.dnd
					}
				}

				DelegateChoice {
					roleValue: "settings"

					delegate: Toggle {
						icon: "settings"
						inactiveOnColor: Colors.palette.m3onSurfaceVariant
						isToggle: false

						onClicked: {
							root.visibilities.sidebar = false;
							root.visibilities.settings = true;
						}
					}
				}
			}
		}
	}
	component Toggle: IconButton {
		fillWidth: true
		inactiveColor: Colors.layer(Colors.palette.m3surfaceContainerHighest, 2)
		isRound: true
		isToggle: true
		shapeMorph: true
	}
}
