import QtQuick
import QtQuick.Layouts
import Quickshell
import ZShell.Components
import qs.Components
import ZShell.Config
import qs.Helpers
import qs.Services

CustomClippingRect {
	id: root

	required property var lock

	color: Colors.tPalette.m3surfaceContainer
	implicitHeight: layout.implicitHeight + layout.anchors.margins * 2
	radius: Tokens.rounding.small

	FadeImage {
		id: image

		anchors.fill: parent
		asynchronous: true
		fillMode: Image.PreserveAspectCrop
		layer.enabled: true
		opacity: status === Image.Ready ? 1 : 0
		source: Players.getArtUrl(Players.active)
		sourceSize: {
			const dpr = (QsWindow.window as QsWindow)?.devicePixelRatio ?? 1;
			return Qt.size(width * dpr, height * dpr);
		}

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

		CustomRect {
			anchors.fill: parent
			color: Colors.palette.m3surface
			opacity: 0.7
		}
	}

	ColumnLayout {
		id: layout

		anchors.left: parent.left
		anchors.margins: Tokens.padding.extraLarge
		anchors.right: parent.right
		anchors.verticalCenter: parent.verticalCenter
		spacing: Tokens.spacing.extraSmall

		CustomText {
			Layout.fillWidth: true
			animate: true
			color: Colors.palette.m3primary
			elide: Text.ElideRight
			font.pointSize: Tokens.font.size.normal
			horizontalAlignment: Text.AlignHCenter
			text: (Players.active?.trackTitle ?? qsTr("Nothing playing")) || qsTr("Unknown track")
		}

		CustomText {
			Layout.fillWidth: true
			animate: true
			color: Colors.palette.m3onSurfaceVariant
			elide: Text.ElideRight
			font.pointSize: Tokens.font.size.small
			horizontalAlignment: Text.AlignHCenter
			text: (Players.active?.trackArtist ?? qsTr("Try playing some music!")) || qsTr("Unknown artist")
		}

		ButtonRow {
			Layout.alignment: Qt.AlignHCenter
			Layout.topMargin: Tokens.spacing.small
			spacing: Tokens.spacing.extraSmall

			IconButton {
				enabled: Players.active?.canGoPrevious
				icon: "skip_previous"
				isRound: true
				shapeMorph: true
				type: IconButton.Tonal

				onClicked: Players.active?.previous()
			}

			IconButton {
				checked: Players.active?.isPlaying ?? false
				enabled: Players.active?.canTogglePlaying
				icon: Players.active?.isPlaying ? "pause" : "play_arrow"
				implicitWidth: implicitHeight + Tokens.padding.largeIncreased * 2
				isRound: true
				shapeMorph: true

				onClicked: Players.active?.togglePlaying()
			}

			IconButton {
				enabled: Players.active?.canGoNext
				icon: "skip_next"
				isRound: true
				shapeMorph: true
				type: IconButton.Tonal

				onClicked: Players.active?.next()
			}
		}
	}
}
