pragma ComponentBehavior: Bound

import QtQuick
import QtQuick.Layouts
import ZShell.Models
import qs.Paths
import qs.Helpers
import qs.Components
import ZShell.Config
import qs.Modules.Settings.Common
import qs.Services

PageBase {
	id: root

	isSubPage: true
	title: qsTr("Wallpapers")

	ColumnLayout {
		anchors.left: parent.left
		anchors.right: parent.right
		anchors.top: parent.top
		spacing: Tokens.spacing.small
		width: root.cappedWidth

		CustomText {
			Layout.topMargin: Tokens.spacing.largeIncreased
			font.pointSize: Tokens.font.size.large
			text: qsTr("Wallpapers")
		}

		GridLayout {
			Layout.fillWidth: true
			columnSpacing: Tokens.spacing.extraSmall
			columns: 4
			rowSpacing: Tokens.spacing.extraSmall
			visible: localWalls.count > 0

			Repeater {
				id: localWalls

				model: {
					const walls = [...Wallpapers.list];
					var baseDir = Paths.wallsdir;
					walls.sort((a, b) => ((a.parentDir === baseDir) - (b.parentDir === baseDir)) || a.name.localeCompare(b.name));
					while (walls.length < 4)
						walls.push(null);

					return walls;
				}

				WallItem {
					required property FileSystemEntry modelData

					enabled: modelData
					opacity: modelData ? 1 : 0
					source: String(modelData?.path ?? "")

					onClicked: {
						Wallpapers.setWallpaper(modelData.path);
						root.sState.closeSubPage();
					}
				}
			}
		}

		Loader {
			Layout.fillWidth: true
			active: localWalls.count === 0
			asynchronous: true
			visible: active

			sourceComponent: CustomRect {
				color: Colors.tPalette.m3surfaceContainer
				implicitHeight: noWallsLayout.implicitHeight + Tokens.padding.extraLarge * 3
				radius: Tokens.rounding.largeIncreased

				ColumnLayout {
					id: noWallsLayout

					anchors.centerIn: parent
					spacing: Tokens.spacing.extraSmall

					MaterialIcon {
						Layout.alignment: Qt.AlignHCenter
						color: Colors.palette.m3outline
						text: "hide_image"
					}

					CustomText {
						Layout.alignment: Qt.AlignHCenter
						color: Colors.palette.m3outline
						text: qsTr("No local wallpapers found")
					}
				}
			}
		}
	}
}
