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

import net.minecraft.network.chat.Component
import net.minecraft.world.entity.player.Inventory
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.FramePanel
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.kommons.math.RGBAColor
import ru.dbotthepony.mc.otm.block.entity.decorative.HoloSignBlockEntity
import ru.dbotthepony.mc.otm.client.MatteryGUI.SMALL_FONT_STYLE
import ru.dbotthepony.mc.otm.client.render.TextIcon
import ru.dbotthepony.mc.otm.client.render.Widgets18
import ru.dbotthepony.mc.otm.client.screen.panels.button.BooleanButtonPanel
import ru.dbotthepony.mc.otm.client.screen.panels.button.ButtonPanel
import ru.dbotthepony.mc.otm.menu.decorative.HoloSignMenu

class HoloSignScreen(menu: HoloSignMenu, inventory: Inventory, title: Component) : MatteryScreen<HoloSignMenu>(menu, title) {
	override fun makeMainFrame(): FramePanel<MatteryScreen<*>> {
		val frame = FramePanel(this, null, 0f, 0f, minecraft!!.window.guiScaledWidth * 0.8f, minecraft!!.window.guiScaledHeight * 0.8f, getTitle())

		frame.makeCloseButton()
		frame.onClose { onClose() }
		frame.makeHelpButton().apply {
			tooltips.add(TranslatableComponent("otm.gui.lock_holo_screen.tip"))
		}

		object : NetworkedStringInputPanel<HoloSignScreen>(this@HoloSignScreen, frame, backend = menu.text) {
			init {
				dock = Dock.FILL
				isMultiLine = true
			}

			override var maxLineLength: Int
				get() = if (menu.locked.value) HoloSignBlockEntity.CREATIVE_MAX_LINE_LENGTH else HoloSignBlockEntity.DEFAULT_MAX_LINE_LENGTH
				set(value) {}
			override var maxLines: Int
				get() = if (menu.locked.value) HoloSignBlockEntity.CREATIVE_MAX_NEWLINES else HoloSignBlockEntity.DEFAULT_MAX_NEWLINES
				set(value) {}
		}

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

		controls.addButton(ButtonPanel.square18(
			this@HoloSignScreen,
			frame,
			icon = Widgets18.COLOR_PALETTE,
			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")
				)
			}
		).also {
			it.tooltips.add(TranslatableComponent("otm.gui.change_color"))
		})

		controls.addButton(BooleanButtonPanel.square18(
			this@HoloSignScreen,
			frame,
			prop = menu.textAutoScale,
			iconActive = Widgets18.TEXT_SCALE_ENABLED,
			iconInactive = Widgets18.TEXT_SCALE_DISABLED,
			tooltipActive = TranslatableComponent("otm.gui.holo_screen.resize_text"),
			tooltipInactive = TranslatableComponent("otm.gui.holo_screen.do_not_resize_text"),
		))

		controls.addButton(BooleanButtonPanel.square18(
			this@HoloSignScreen,
			frame,
			prop = menu.locked,
			iconActive = Widgets18.LOCK_LOCKED,
			iconInactive = Widgets18.LOCK_UNLOCKED,
			tooltipActive = TranslatableComponent("otm.gui.lock_holo_screen"),
			tooltipInactive = TranslatableComponent("otm.gui.lock_holo_screen.unlocked")
		))

		controls.addButton(BooleanButtonPanel.square18(
			this@HoloSignScreen,
			frame,
			prop = menu.smallerFont,
			iconActive = TextIcon(font = font, text = TranslatableComponent("otm.gui.abc").withStyle(SMALL_FONT_STYLE)),
			iconInactive = TextIcon(font = font, text = TranslatableComponent("otm.gui.abc")),
			tooltipActive = TranslatableComponent("otm.gui.use_small_font"),
			tooltipInactive = TranslatableComponent("otm.gui.use_standard_font")
		))

		return frame
	}
}
