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

import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.components.Renderable
import net.minecraft.client.gui.components.events.GuiEventListener
import net.minecraft.client.gui.screens.Screen

// before 1.19.3 Renderable was Widget
class Panel2Widget<out S: Screen, out P : EditablePanel<S>>(
	val panel: P
) : GuiEventListener, Renderable {
	init {
		require(panel.parent == null) { "Widget wrapped panels can't have a parent ($panel has parent ${panel.parent})" }
	}

	override fun render(graphics: GuiGraphics, mouseX: Int, mouseY: Int, partialTick: Float) {
		panel.tick()

		val xFloat = mouseX.toFloat()
		val yFloat = mouseY.toFloat()

		panel.tickHover(xFloat, yFloat)
		panel.render(graphics, false, xFloat, yFloat, partialTick)
		panel.render(graphics, true, xFloat, yFloat, partialTick)
		panel.renderTooltips(graphics, xFloat, yFloat, partialTick)
	}

	override fun mouseMoved(mouseX: Double, mouseY: Double) {
		panel.mouseMoved(mouseX, mouseY)
	}

	override fun mouseClicked(p_94737_: Double, p_94738_: Double, p_94739_: Int): Boolean {
		return panel.mouseClickedChecked(p_94737_, p_94738_, p_94739_)
	}

	override fun mouseReleased(p_94753_: Double, p_94754_: Double, p_94755_: Int): Boolean {
		return panel.mouseReleasedChecked(p_94753_, p_94754_, p_94755_)
	}

	override fun mouseDragged(
		p_94740_: Double,
		p_94741_: Double,
		p_94742_: Int,
		p_94743_: Double,
		p_94744_: Double
	): Boolean {
		return panel.mouseDraggedChecked(p_94740_, p_94741_, p_94742_, p_94743_, p_94744_)
	}

	override fun mouseScrolled(p_94734_: Double, p_94735_: Double, p_94736_: Double): Boolean {
		return panel.mouseScrolledChecked(p_94734_, p_94735_, p_94736_)
	}

	override fun keyPressed(p_94745_: Int, p_94746_: Int, p_94747_: Int): Boolean {
		return panel.keyPressed(p_94745_, p_94746_, p_94747_)
	}

	override fun keyReleased(p_94750_: Int, p_94751_: Int, p_94752_: Int): Boolean {
		return panel.keyReleased(p_94750_, p_94751_, p_94752_)
	}

	override fun charTyped(p_94732_: Char, p_94733_: Int): Boolean {
		return panel.charTyped(p_94732_, p_94733_)
	}

	override fun isMouseOver(p_94748_: Double, p_94749_: Double): Boolean {
		return panel.isMouseOver(p_94748_, p_94749_)
	}

	override fun setFocused(p_265728_: Boolean) {
		// no op
	}

	override fun isFocused(): Boolean {
		// ага, щас
		return false
	}
}
