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

import com.mojang.blaze3d.platform.InputConstants
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.world.item.Item
import ru.dbotthepony.mc.otm.client.isAltDown
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.client.playGuiClickSound
import ru.dbotthepony.mc.otm.client.render.MatterySprite
import ru.dbotthepony.mc.otm.client.render.Widgets18
import ru.dbotthepony.mc.otm.client.screen.MatteryScreen
import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel
import ru.dbotthepony.mc.otm.menu.MatteryMenu

open class InventorySlotPanel<out S : MatteryScreen<*>, out T : MatteryMenu.InventorySlot>(
	screen: S,
	parent: EditablePanel<*>?,
	slot: T,
	x: Float = 0f,
	y: Float = 0f,
) : UserFilteredSlotPanel<S, T>(screen, parent, slot, x, y, SIZE, SIZE) {
	override var slotFilter: Item?
		get() = slot.filter?.get()
		set(value) { slot.filter?.accept(value) }

	override fun renderBackgroundBeforeFilter(graphics: GuiGraphics, mouseX: Float, mouseY: Float, partialTick: Float) {
		if (slot.chargeFlag?.get() == true) {
			Widgets18.CHARGE_SLOT_BACKGROUND.render(graphics, 0f, 0f, width, height)
		}
	}

	override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean {
		if (button == InputConstants.MOUSE_BUTTON_LEFT && minecraft.window.isAltDown) {
			val chargeFlag = slot.chargeFlag

			if (chargeFlag == null) {
				return super.mouseClickedInner(x, y, button)
			} else {
				chargeFlag.accept(!chargeFlag.get())
				playGuiClickSound()
				return true
			}
		}

		return super.mouseClickedInner(x, y, button)
	}
}
