package ru.dbotthepony.mc.otm.client.screen.panels

import net.minecraft.world.inventory.Slot
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.client.screen.MatteryScreen
import ru.dbotthepony.mc.otm.client.screen.panels.slot.AbstractSlotPanel
import ru.dbotthepony.mc.otm.client.screen.panels.slot.FoldableSlotPanel
import ru.dbotthepony.mc.otm.client.screen.panels.slot.InventorySlotPanel
import ru.dbotthepony.mc.otm.client.screen.panels.slot.SlotPanel
import ru.dbotthepony.mc.otm.client.screen.panels.util.BackgroundPanel
import ru.dbotthepony.mc.otm.compat.cos.CosmeticToggleButton
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import ru.dbotthepony.mc.otm.menu.PlayerSlot

fun <S : MatteryScreen<*>> makeCuriosPanel(screen: S, parent: FramePanel<S>, slots: List<PlayerSlot<Slot, Slot>>, autoAlign: Boolean = false): EditablePanel<S>? {
	if (slots.isEmpty()) {
		return null
	}

	val curiosWidth = if (slots.any { it.cosmetic != null }) AbstractSlotPanel.SIZE * 2f else AbstractSlotPanel.SIZE
	val curiosHeight = slots.size.coerceAtMost(4) * AbstractSlotPanel.SIZE

	val curiosRect =
			if (slots.size > 4)
				ScrollbarBackgroundPanel.padded(screen, parent, 0f,
												width = curiosWidth,
												height = curiosHeight, alwaysShowScrollbar = true)
			else
				BackgroundPanel.padded(screen, parent, 0f,
									   width = curiosWidth,
									   height = curiosHeight)

	if (autoAlign) {
		curiosRect.x = -4f - curiosRect.width
	}

	for ((slot, cosmetic) in slots) {
		val row = EditablePanel(screen, if (curiosRect is ScrollbarBackgroundPanel) curiosRect.canvas else curiosRect, height = AbstractSlotPanel.SIZE)
		row.dock = Dock.TOP

		SlotPanel(screen, row, slot).dock = Dock.RIGHT

		if (cosmetic != null) {
			SlotPanel(screen, row, cosmetic).dock = Dock.LEFT
		}
	}

	return curiosRect
}

@Suppress("LeakingThis")
open class PlayerEquipmentPanel<S : MatteryScreen<*>>(
	screen: S,
	parent: EditablePanel<*>?,
	x: Float = 0f,
	y: Float = 0f,
	val armorSlots: List<PlayerSlot<MatteryMenu.EquipmentSlot, Slot>>
) : EditablePanel<S>(screen, parent, x, y, height = HEIGHT, width = WIDTH) {
	val armorSlotsStrip = EditablePanel(screen, this, width = AbstractSlotPanel.SIZE)
	val entityPanel = EntityRendererPanel(screen, this, minecraft.player!!)

	init {
		entityPanel.dock = Dock.LEFT
		armorSlotsStrip.dock = Dock.LEFT

		for ((slot, cosmeticSlot) in armorSlots) {
			if (cosmeticSlot == null) {
				InventorySlotPanel(screen, armorSlotsStrip, slot).dock = Dock.TOP
			} else {
				FoldableSlotPanel(screen, armorSlotsStrip,
								  InventorySlotPanel(screen, null, slot).also { CosmeticToggleButton(screen, it, slot.type) },
								  listOf(SlotPanel(screen, null, cosmeticSlot))).dock = Dock.TOP

			}
		}
	}

	var leftSided: Boolean
		get() = armorSlotsStrip.dock == Dock.LEFT
		set(value) {
			if (value) {
				armorSlotsStrip.dock = Dock.LEFT
			} else {
				armorSlotsStrip.dock = Dock.RIGHT
			}
		}

	companion object {
		val HEIGHT = EntityRendererPanel.ENTITY_RECTANGLE.height
		val WIDTH = AbstractSlotPanel.SIZE + EntityRendererPanel.ENTITY_RECTANGLE.width
	}
}
