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

CustomRect {
	id: root

	required property Toast modelData

	anchors.left: parent.left
	anchors.right: parent.right
	border.color: {
		let colour = Colors.palette.m3outlineVariant;
		if (root.modelData.type === Toast.Success)
			colour = Colors.palette.m3success;
		if (root.modelData.type === Toast.Warning)
			colour = Colors.palette.m3secondaryContainer;
		if (root.modelData.type === Toast.Error)
			colour = Colors.palette.m3error;
		return Qt.alpha(colour, 0.3);
	}
	border.width: 1
	color: {
		if (root.modelData.type === Toast.Success)
			return Colors.palette.m3successContainer;
		if (root.modelData.type === Toast.Warning)
			return Colors.palette.m3secondary;
		if (root.modelData.type === Toast.Error)
			return Colors.palette.m3errorContainer;
		return Colors.palette.m3surface;
	}
	implicitHeight: layout.implicitHeight + Tokens.padding.small * 2
	radius: Tokens.rounding.medium

	Behavior on border.color {
		CAnim {
		}
	}

	Elevation {
		anchors.fill: parent
		level: 3
		opacity: parent.opacity
		radius: parent.radius
		z: -1
	}

	RowLayout {
		id: layout

		anchors.fill: parent
		anchors.leftMargin: Tokens.padding.small
		anchors.margins: Tokens.padding.small
		anchors.rightMargin: Tokens.padding.small
		spacing: Tokens.spacing.medium

		CustomRect {
			color: {
				if (root.modelData.type === Toast.Success)
					return Colors.palette.m3success;
				if (root.modelData.type === Toast.Warning)
					return Colors.palette.m3secondaryContainer;
				if (root.modelData.type === Toast.Error)
					return Colors.palette.m3error;
				return Colors.palette.m3surfaceContainerHigh;
			}
			implicitHeight: icon.implicitHeight + Tokens.padding.small * 2
			implicitWidth: implicitHeight
			radius: Tokens.rounding.medium

			MaterialIcon {
				id: icon

				anchors.centerIn: parent
				color: {
					if (root.modelData.type === Toast.Success)
						return Colors.palette.m3onSuccess;
					if (root.modelData.type === Toast.Warning)
						return Colors.palette.m3onSecondaryContainer;
					if (root.modelData.type === Toast.Error)
						return Colors.palette.m3onError;
					return Colors.palette.m3onSurfaceVariant;
				}
				font.pointSize: Math.round(Tokens.font.size.large * 1.2)
				text: root.modelData.icon
			}
		}

		ColumnLayout {
			Layout.fillWidth: true
			spacing: 0

			CustomText {
				id: title

				Layout.fillWidth: true
				color: {
					if (root.modelData.type === Toast.Success)
						return Colors.palette.m3onSuccessContainer;
					if (root.modelData.type === Toast.Warning)
						return Colors.palette.m3onSecondary;
					if (root.modelData.type === Toast.Error)
						return Colors.palette.m3onErrorContainer;
					return Colors.palette.m3onSurface;
				}
				elide: Text.ElideRight
				font.pointSize: Tokens.font.size.normal
				text: root.modelData.title
			}

			CustomText {
				Layout.fillWidth: true
				color: {
					if (root.modelData.type === Toast.Success)
						return Colors.palette.m3onSuccessContainer;
					if (root.modelData.type === Toast.Warning)
						return Colors.palette.m3onSecondary;
					if (root.modelData.type === Toast.Error)
						return Colors.palette.m3onErrorContainer;
					return Colors.palette.m3onSurface;
				}
				elide: Text.ElideRight
				opacity: 0.8
				text: root.modelData.message
				textFormat: Text.StyledText
			}
		}
	}
}
