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

import net.minecraft.network.chat.Component
import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.item.ItemStack
import ru.dbotthepony.mc.otm.client.render.ItemStackIcon
import ru.dbotthepony.mc.otm.client.screen.MatteryScreen
import ru.dbotthepony.mc.otm.client.screen.panels.ColorPickerPanel
import ru.dbotthepony.mc.otm.client.screen.panels.Dock
import ru.dbotthepony.mc.otm.client.screen.panels.DockProperty
import ru.dbotthepony.mc.otm.client.screen.panels.FramePanel
import ru.dbotthepony.mc.otm.client.screen.panels.button.CheckBoxLabelInputPanel
import ru.dbotthepony.mc.otm.client.screen.panels.button.LargeRectangleButtonPanel
import ru.dbotthepony.mc.otm.client.screen.panels.button.makeDeviceControls
import ru.dbotthepony.mc.otm.client.screen.panels.input.NetworkedStringInputPanel
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.math.RGBAColor
import ru.dbotthepony.mc.otm.menu.decorative.HoloSignMenu
import ru.dbotthepony.mc.otm.registry.MItems

class HoloSignScreen(menu: HoloSignMenu, inventory: Inventory, title: Component) : MatteryScreen<HoloSignMenu>(menu, title) {
	override fun makeMainFrame(): FramePanel<MatteryScreen<*>> {
		val frame = FramePanel(this, null, 0f, 0f, 200f, 200f, getTitle())

		frame.makeCloseButton()
		frame.onClose { onClose() }

		val input = NetworkedStringInputPanel(this, frame, backend = menu.text)
		input.dock = Dock.FILL
		input.isMultiLine = true

		val lock = CheckBoxLabelInputPanel(this, frame, menu.locked, TranslatableComponent("otm.gui.lock_holo_screen"))
		lock.dock = Dock.BOTTOM
		lock.dockMargin = DockProperty(2f, 2f, 2f, 2f)
		lock.tooltips.add(TranslatableComponent("otm.gui.lock_holo_screen.tip"))

		val controls = makeDeviceControls(this, frame, redstoneConfig = menu.redstone)

		controls.addButton(object : LargeRectangleButtonPanel<HoloSignScreen>(this@HoloSignScreen, frame, onPress = {
			frame.blockingWindow = ColorPickerPanel.frame(
				this@HoloSignScreen,
				callback = {
					menu.textRed.accept(it.red)
					menu.textGreen.accept(it.green)
					menu.textBlue.accept(it.blue)
					menu.textAlpha.accept(it.alpha)
				},
				color = RGBAColor(menu.textRed.value, menu.textGreen.value, menu.textBlue.value, menu.textAlpha.value),
				isDisabled = { !menu.textRed.and(menu.textGreen).and(menu.textBlue).and(menu.textAlpha).test(minecraft?.player) },
				title = TranslatableComponent("otm.gui.change_color"))
		}) {
			init {
				tooltips.add(TranslatableComponent("otm.gui.change_color"))
				icon = ItemStackIcon(ItemStack(MItems.PAINTER)).fixed()
			}
		})

		return frame
	}
}
