pragma ComponentBehavior: Bound

import Quickshell
import QtQuick
import qs.Paths
import qs.Modules
import qs.Components
import qs.Helpers
import qs.Config

Item {
	id: root

	required property var content
	readonly property Item currentList: showWallpapers ? wallpaperList.item : appList.item
	required property real maxHeight
	required property int padding
	required property var panels
	required property int rounding
	required property CustomTextField search
	readonly property bool showWallpapers: search.text.startsWith(`${Config.launcher.actionPrefix}wallpaper `)
	required property PersistentProperties visibilities

	anchors.bottom: parent.bottom
	anchors.horizontalCenter: parent.horizontalCenter
	clip: true
	state: showWallpapers ? "wallpapers" : "apps"

	Behavior on implicitHeight {
		enabled: root.visibilities.launcher

		Anim {
			duration: Appearance.anim.durations.small
			easing.bezierCurve: Appearance.anim.curves.expressiveEffects
		}
	}
	Behavior on implicitWidth {
		enabled: root.visibilities.launcher

		Anim {
			duration: Appearance.anim.durations.small
			easing.bezierCurve: Appearance.anim.curves.expressiveEffects
		}
	}
	Behavior on state {
		SequentialAnimation {
			Anim {
				duration: Appearance.anim.durations.small
				from: 1
				property: "opacity"
				target: root
				to: 0
			}

			PropertyAction {
			}

			Anim {
				duration: Appearance.anim.durations.small
				from: 0
				property: "opacity"
				target: root
				to: 1
			}
		}
	}
	states: [
		State {
			name: "apps"

			PropertyChanges {
				appList.active: true
				root.implicitHeight: Math.min(root.maxHeight, appList.implicitHeight > 0 ? appList.implicitHeight : empty.implicitHeight)
				root.implicitWidth: Config.launcher.sizes.itemWidth
			}

			AnchorChanges {
				anchors.left: root.parent.left
				anchors.right: root.parent.right
			}
		},
		State {
			name: "wallpapers"

			PropertyChanges {
				root.implicitHeight: Config.launcher.sizes.wallpaperHeight
				root.implicitWidth: Math.max(Config.launcher.sizes.itemWidth * 1.2, wallpaperList.implicitWidth)
				wallpaperList.active: true
			}
		}
	]

	Loader {
		id: appList

		active: false
		anchors.fill: parent

		sourceComponent: AppList {
			search: root.search
			visibilities: root.visibilities
		}
	}

	Loader {
		id: wallpaperList

		active: false
		anchors.bottom: parent.bottom
		anchors.horizontalCenter: parent.horizontalCenter
		anchors.top: parent.top

		sourceComponent: WallpaperList {
			content: root.content
			panels: root.panels
			search: root.search
			visibilities: root.visibilities
		}
	}

	Row {
		id: empty

		anchors.horizontalCenter: parent.horizontalCenter
		anchors.verticalCenter: parent.verticalCenter
		opacity: root.currentList?.count === 0 ? 1 : 0
		padding: Appearance.padding.large
		scale: root.currentList?.count === 0 ? 1 : 0.5
		spacing: Appearance.spacing.normal

		Behavior on opacity {
			Anim {
			}
		}
		Behavior on scale {
			Anim {
			}
		}

		MaterialIcon {
			anchors.verticalCenter: parent.verticalCenter
			color: DynamicColors.palette.m3onSurfaceVariant
			font.pointSize: Appearance.font.size.extraLarge
			text: root.state === "wallpapers" ? "wallpaper_slideshow" : "manage_search"
		}

		Column {
			anchors.verticalCenter: parent.verticalCenter

			CustomText {
				color: DynamicColors.palette.m3onSurfaceVariant
				font.pointSize: Appearance.font.size.larger
				font.weight: 500
				text: root.state === "wallpapers" ? qsTr("No wallpapers found") : qsTr("No results")
			}

			CustomText {
				color: DynamicColors.palette.m3onSurfaceVariant
				font.pointSize: Appearance.font.size.normal
				text: root.state === "wallpapers" && Wallpapers.list.length === 0 ? qsTr("Try putting some wallpapers in %1").arg(Paths.shortenHome(Paths.wallsdir)) : qsTr("Try searching for something else")
			}
		}
	}
}
