import Quickshell
import QtQuick
import qs.Components
import qs.Modules as Modules
import qs.Modules.Notifications as Notifications
import qs.Modules.Notifications.Sidebar as Sidebar
import qs.Modules.Notifications.Sidebar.Utils as Utils
import qs.Modules.Dashboard as Dashboard
import qs.Modules.Osd as Osd
import qs.Components.Toast as Toasts
import qs.Modules.Launcher as Launcher
import qs.Modules.Resources as Resources
import qs.Modules.Settings as Settings
import qs.Modules.Drawing as Drawing
import qs.Modules.Dock as Dock
import qs.Modules.Clipboard as Clipboard
import qs.Config

Item {
	id: root

	required property Item bar
	required property real borderThickness
	readonly property alias clipboard: clipboard
	readonly property alias dashboard: dashboard
	readonly property alias dashboardWrapper: dashboardWrapper
	readonly property alias dock: dock
	readonly property alias drawing: drawing
	required property var drawingItem
	readonly property alias launcher: launcher
	readonly property alias notifications: notifications
	readonly property alias osd: osd
	readonly property alias osdWrapper: osdWrapper
	readonly property alias popouts: popouts.content
	readonly property alias popoutsWrapper: popouts
	readonly property alias resources: resources
	readonly property alias resourcesWrapper: resourcesWrapper
	required property ShellScreen screen
	readonly property alias settings: settings
	readonly property alias settingsWrapper: settingsWrapper
	readonly property alias sidebar: sidebar
	readonly property alias toasts: toasts
	readonly property alias utilities: utilities
	required property PersistentProperties visibilities

	anchors.fill: parent
	anchors.margins: borderThickness
	anchors.topMargin: bar.implicitHeight

	Item {
		id: resourcesWrapper

		anchors.left: parent.left
		anchors.top: parent.top
		clip: true
		implicitHeight: resources.implicitHeight * (1 - resources.offsetScale)
		implicitWidth: resources.implicitWidth

		Resources.Wrapper {
			id: resources

			anchors.left: parent.left
			anchors.top: parent.top
			visibilities: root.visibilities
		}
	}

	Item {
		id: osdWrapper

		anchors.right: parent.right
		anchors.rightMargin: sidebar.width * (1 - sidebar.offsetScale)
		anchors.verticalCenter: parent.verticalCenter
		clip: sidebar.visible
		implicitHeight: osd.implicitHeight
		implicitWidth: osd.implicitWidth * (1 - osd.offsetScale)

		Osd.Wrapper {
			id: osd

			anchors.right: parent.right
			anchors.verticalCenter: parent.verticalCenter
			screen: root.screen
			sidebarOrSessionVisible: sidebar.visible
			visibilities: root.visibilities
		}
	}

	Drawing.Wrapper {
		id: drawing

		anchors.left: parent.left
		anchors.verticalCenter: parent.verticalCenter
		drawing: root.drawingItem
		screen: root.screen
		visibilities: root.visibilities
	}

	Modules.ClipWrapper {
		id: popouts

		anchors.top: parent.top
		borderThickness: root.borderThickness
		screen: root.screen
	}

	Toasts.Toasts {
		id: toasts

		anchors.bottom: sidebar.visible ? parent.bottom : utilities.top
		anchors.margins: Appearance.padding.normal
		anchors.right: sidebar.left
	}

	Notifications.Wrapper {
		id: notifications

		anchors.right: parent.right
		anchors.top: parent.top
		panels: root
		sidebarPanel: sidebar
		visibilities: root.visibilities
	}

	Launcher.Wrapper {
		id: launcher

		anchors.bottom: parent.bottom
		anchors.horizontalCenter: parent.horizontalCenter
		panels: root
		screen: root.screen
		visibilities: root.visibilities
	}

	Utils.Wrapper {
		id: utilities

		anchors.bottom: parent.bottom
		anchors.right: parent.right
		popouts: popouts
		sidebar: sidebar
		visibilities: root.visibilities
	}

	Item {
		id: dashboardWrapper

		property real offsetScale: dashboard.shouldBeActive ? 0 : 1

		anchors.right: parent.right
		anchors.top: parent.top
		clip: true
		implicitHeight: dashboard.implicitHeight * (1 - offsetScale)
		implicitWidth: dashboard.implicitWidth

		Behavior on offsetScale {
			Anim {
				duration: Appearance.anim.durations.expressiveDefaultSpatial
				easing.bezierCurve: Appearance.anim.curves.expressiveDefaultSpatial
			}
		}

		Dashboard.Wrapper {
			id: dashboard

			anchors.right: parent.right
			anchors.top: parent.top
			anchors.topMargin: (-implicitHeight - 5) * offsetScale
			offsetScale: dashboardWrapper.offsetScale
			visibilities: root.visibilities
		}
	}

	Sidebar.Wrapper {
		id: sidebar

		anchors.bottom: utilities.top
		anchors.right: parent.right
		anchors.top: notifications.bottom
		panels: root
		visibilities: root.visibilities
	}

	Item {
		id: settingsWrapper

		clip: true
		implicitHeight: settings.implicitHeight
		implicitWidth: settings.implicitWidth
		x: (root.width - settings.implicitWidth) / 2
		y: (settings.implicitHeight + (root.height - root.bar.implicitHeight - settings.implicitHeight) / 2) * (1 - settings.offsetScale) - settings.implicitHeight - 5

		Settings.Wrapper {
			id: settings

			anchors.horizontalCenter: parent.horizontalCenter
			anchors.top: parent.top
			// anchors.centerIn: parent
			panels: root
			screen: root.screen
			visibilities: root.visibilities
		}
	}

	Dock.Wrapper {
		id: dock

		anchors.bottom: parent.bottom
		anchors.horizontalCenter: parent.horizontalCenter
		panels: root
		screen: root.screen
		visibilities: root.visibilities
	}

	Clipboard.Wrapper {
		id: clipboard

		anchors.bottom: parent.bottom
		anchors.left: parent.left
		screen: root.screen
		visibilities: root.visibilities
	}
}
