import QtQuick
import Quickshell
import Quickshell.Widgets
import ZShell.Config
import qs.Components
import qs.Helpers
import qs.Modules.Launcher.Services
import qs.Services

Item {
	id: root

	required property var list
	required property var modelData

	anchors.left: parent?.left
	anchors.right: parent?.right
	implicitHeight: Config.launcher.sizes.itemHeight

	function onClicked(): void {
		root.list.visibilities.launcher = false;
		if (root.modelData?.path)
			Files.launch(["xdg-open", root.modelData.path]);
	}

	StateLayer {
		id: sLayer

		radius: Tokens.rounding.medium
		manualHoverOverride: openLocation.hovered

		onClicked: {
			root.onClicked();
		}
	}

	IconButton {
		id: openLocation

		anchors.right: parent.right
		anchors.rightMargin: Tokens.padding.medium
		anchors.verticalCenter: parent.verticalCenter
		icon: "open_in_new"
		isRound: true

		scale: sLayer.containsMouse || sLayer.manualHoverOverride ? 1 : 0.4
		opacity: sLayer.containsMouse || sLayer.manualHoverOverride ? 1 : 0
		visible: opacity > 0

		Behavior on opacity {
			Anim {
				type: Anim.DefaultEffects
			}
		}

		Behavior on scale {
			Anim {
				type: Anim.DefaultEffects
			}
		}

		onClicked: {
			root.list.visibilities.launcher = false;
			if (root.modelData?.path)
				Files.showInFolder(root.modelData.path);
		}
	}

	Item {
		anchors.fill: parent
		anchors.leftMargin: Tokens.padding.small
		anchors.margins: Tokens.padding.small
		anchors.rightMargin: Tokens.padding.small

		IconImage {
			id: icon

			anchors.verticalCenter: parent.verticalCenter
			implicitSize: Tokens.font.size.extraLarge
			source: Quickshell.iconPath(root.modelData?.icon) ?? ""
			visible: !root.modelData?.isImage
		}

		Loader {
			active: opacity > 0
			anchors.verticalCenter: parent.verticalCenter
			asynchronous: true
			opacity: root.modelData?.isImage && image.status === Image.Loading ? 1 : 0

			Behavior on opacity {
				Anim {
					type: Anim.DefaultEffects
				}
			}
			sourceComponent: LoadingIndicator {
				color: Colors.palette.m3primaryContainer
				implicitSize: Tokens.font.size.extraLarge
			}
		}

		FadeImage {
			id: image

			property int implicitSize: Tokens.font.size.extraLarge

			width: implicitSize
			fillMode: Image.PreserveAspectFit
			anchors.verticalCenter: parent.verticalCenter
			source: root.modelData?.isImage ? root.modelData?.path ?? "" : ""
			visible: root.modelData?.isImage
		}

		Item {
			anchors.left: icon.right
			anchors.leftMargin: Tokens.spacing.medium
			anchors.verticalCenter: icon.verticalCenter
			implicitHeight: name.implicitHeight + desc.implicitHeight
			implicitWidth: parent.width - icon.width

			CustomText {
				id: name

				font.pointSize: Tokens.font.size.normal
				text: root.modelData?.name ?? ""
				elide: Text.ElideRight
				width: root.width - icon.width - openLocation.implicitWidth - openLocation.anchors.rightMargin - Tokens.spacing.medium - Tokens.rounding.medium * 2
			}

			CustomText {
				id: desc

				anchors.top: name.bottom
				color: Colors.palette.m3outline
				elide: Text.ElideRight
				font.pointSize: Tokens.font.size.small
				text: root.modelData?.path ?? ""
				width: root.width - icon.width - openLocation.implicitWidth - openLocation.anchors.rightMargin - Tokens.spacing.medium - Tokens.rounding.medium * 2
			}
		}
	}
}
