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

import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.screens.Screen
import ru.dbotthepony.mc.otm.client.render.AbstractMatterySprite
import ru.dbotthepony.mc.otm.client.render.WidgetLocation
import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel
import ru.dbotthepony.mc.otm.core.GetterSetter

open class CheckBoxPanel<out S : Screen>(
	screen: S,
	parent: EditablePanel<*>?,
	x: Float = 0f,
	y: Float = 0f,
	width: Float = REGULAR_DIMENSIONS,
	height: Float = REGULAR_DIMENSIONS,
	open val isChecked: GetterSetter<Boolean> = GetterSetter.box(false)
) : AbstractButtonPanel<S>(screen, parent, x, y, width, height) {
	open val IDLE_UNCHECKED: AbstractMatterySprite = Companion.IDLE_UNCHECKED
	open val IDLE_CHECKED: AbstractMatterySprite = Companion.IDLE_CHECKED
	open val HOVERED_UNCHECKED: AbstractMatterySprite = Companion.HOVERED_UNCHECKED
	open val HOVERED_CHECKED: AbstractMatterySprite = Companion.HOVERED_CHECKED
	open val PRESSED_UNCHECKED: AbstractMatterySprite = Companion.PRESSED_UNCHECKED
	open val PRESSED_CHECKED: AbstractMatterySprite = Companion.PRESSED_CHECKED
	open val DISABLED_UNCHECKED: AbstractMatterySprite = Companion.DISABLED_UNCHECKED
	open val DISABLED_CHECKED: AbstractMatterySprite = Companion.DISABLED_CHECKED

	protected fun renderCheckboxBackground(graphics: GuiGraphics, mouseX: Float, mouseY: Float, partialTick: Float) {
		if (isDisabled) {
			if (isChecked.get()) {
				DISABLED_CHECKED.render(graphics, width = width, height = height)
			} else {
				DISABLED_UNCHECKED.render(graphics, width = width, height = height)
			}
		} else {
			if (isPressed) {
				if (isChecked.get()) {
					PRESSED_CHECKED.render(graphics, width = width, height = height)
				} else {
					PRESSED_UNCHECKED.render(graphics, width = width, height = height)
				}
			} else if (isHovered) {
				if (isChecked.get()) {
					HOVERED_CHECKED.render(graphics, width = width, height = height)
				} else {
					HOVERED_UNCHECKED.render(graphics, width = width, height = height)
				}
			} else {
				if (isChecked.get()) {
					IDLE_CHECKED.render(graphics, width = width, height = height)
				} else {
					IDLE_UNCHECKED.render(graphics, width = width, height = height)
				}
			}
		}
	}

	override fun innerRender(graphics: GuiGraphics, mouseX: Float, mouseY: Float, partialTick: Float) {
		renderCheckboxBackground(graphics, mouseX, mouseY, partialTick)
	}

	override fun onClick(mouseButton: Int) {
		isChecked.accept(!isChecked.get())
	}

	companion object {
		const val REGULAR_DIMENSIONS = 15f

		val IDLE_UNCHECKED: AbstractMatterySprite
		val IDLE_CHECKED: AbstractMatterySprite
		val HOVERED_UNCHECKED: AbstractMatterySprite
		val HOVERED_CHECKED: AbstractMatterySprite
		val PRESSED_UNCHECKED: AbstractMatterySprite
		val PRESSED_CHECKED: AbstractMatterySprite
		val DISABLED_UNCHECKED: AbstractMatterySprite
		val DISABLED_CHECKED: AbstractMatterySprite

		init {
			val grid = WidgetLocation.CHECKBOX.grid(columns = 2, rows = 4)

			IDLE_CHECKED = grid.next()
			IDLE_UNCHECKED = grid.next()
			HOVERED_CHECKED = grid.next()
			HOVERED_UNCHECKED = grid.next()
			PRESSED_CHECKED = grid.next()
			PRESSED_UNCHECKED = grid.next()
			DISABLED_CHECKED = grid.next()
			DISABLED_UNCHECKED = grid.next()
		}
	}
}

