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

import net.minecraft.client.gui.screens.Screen
import ru.dbotthepony.mc.otm.client.render.MGUIGraphics
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.client.render.IGUIRenderable
import ru.dbotthepony.mc.otm.client.render.UVWindingOrder
import ru.dbotthepony.mc.otm.client.render.Widgets18
import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel
import ru.dbotthepony.mc.otm.core.math.RGBAColor
import ru.dbotthepony.mc.otm.menu.MatteryMenu

open class LargeRectangleButtonPanel<out S : Screen>(
	screen: S,
	parent: EditablePanel<*>?,
	x: Float = 0f,
	y: Float = 0f,
	width: Float = SIZE,
	height: Float = SIZE,
	onPress: ((clickButton: Int) -> Unit)? = null,
	var icon: IGUIRenderable? = null,
	var iconWinding: UVWindingOrder? = null,
) : RectangleButtonPanel<S>(screen, parent, x, y, width, height, onPress) {
	final override val IDLE = Widgets18.BUTTON_IDLE
	final override val HOVERED = Widgets18.BUTTON_HOVERED
	final override val PRESSED = Widgets18.BUTTON_PRESSED
	final override val DISABLED = Widgets18.BUTTON_DISABLED

	override fun innerRender(graphics: MGUIGraphics, mouseX: Float, mouseY: Float, partialTick: Float) {
		super.innerRender(graphics, mouseX, mouseY, partialTick)

		val color = if (isDisabled || isPressed) RGBAColor.DARK_GRAY else RGBAColor.WHITE
		if (iconWinding != null) {
			icon?.render(graphics, width = width, height = height, winding = iconWinding!!, color = color)
		} else {
			icon?.render(graphics, width = width, height = height, color = color)
		}
	}

	companion object {
		const val SIZE = 18f

		fun <S : Screen> input(
			screen: S,
			parent: EditablePanel<*>?,
			input: MatteryMenu.PlayerInput<Nothing?>,
			x: Float = 0f,
			y: Float = 0f,
			width: Float = SIZE,
			height: Float = SIZE,
			icon: IGUIRenderable? = null,
			iconWinding: UVWindingOrder? = null,
		): LargeRectangleButtonPanel<S> {
			return object : LargeRectangleButtonPanel<S>(screen, parent, x, y, width, height, null, icon, iconWinding) {
				override fun onClick(mouseButton: Int) {
					input.accept(null)
				}

				override var isDisabled: Boolean
					get() = !input.test(minecraft.player)
					set(value) {}
			}
		}
	}
}
