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

import net.minecraft.ChatFormatting
import net.minecraft.network.chat.Component
import net.minecraft.world.entity.player.Inventory
import ru.dbotthepony.mc.otm.block.entity.blackhole.BlackHoleGeneratorBlockEntity
import ru.dbotthepony.mc.otm.client.screen.MatteryScreen
import ru.dbotthepony.mc.otm.client.screen.panels.Dock
import ru.dbotthepony.mc.otm.client.screen.panels.DynamicLabel
import ru.dbotthepony.mc.otm.client.screen.panels.FramePanel
import ru.dbotthepony.mc.otm.client.screen.panels.Label
import ru.dbotthepony.mc.otm.client.screen.panels.button.BooleanButtonPanel
import ru.dbotthepony.mc.otm.client.screen.panels.button.DeviceControls
import ru.dbotthepony.mc.otm.client.screen.panels.input.DecimalInputPanel
import ru.dbotthepony.mc.otm.client.screen.panels.util.makeButtonLabel
import ru.dbotthepony.mc.otm.client.screen.widget.ProfiledMatterGaugePanel
import ru.dbotthepony.mc.otm.client.screen.widget.ProfiledPowerGaugePanel
import ru.dbotthepony.mc.otm.core.TextComponent
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.menu.tech.BlackHoleGeneratorMenu
import java.util.function.Supplier

class BlackHoleGeneratorScreen(menu: BlackHoleGeneratorMenu, inventory: Inventory, title: Component) : MatteryScreen<BlackHoleGeneratorMenu>(menu, inventory, title) {
	override fun makeMainFrame(): FramePanel<MatteryScreen<*>> {
		val frame = FramePanel.padded(this, 240f, 150f, title)

		frame.behaveAsWindow()
		frame.onClose { onClose() }
		frame.makeHelpButton().also {
			it.tooltips.add(TranslatableComponent("otm.gui.black_hole_generator.help0"))
			it.tooltips.add(TranslatableComponent("otm.gui.black_hole_generator.help1"))
			it.tooltips.add(TranslatableComponent("otm.gui.black_hole_generator.help2"))
			it.tooltips.add(TextComponent(""))
			it.tooltips.add(TranslatableComponent("otm.gui.black_hole_generator.help3"))
		}

		DynamicLabel(this, frame, textSupplier = Supplier {
			TranslatableComponent(
				"otm.gui.multiblock.formed",
				if (menu.formed.get())
					TranslatableComponent("otm.misc.yes").withStyle(ChatFormatting.DARK_GREEN)
				else
					TranslatableComponent("otm.misc.no")
			)
		}).also {
			it.dock = Dock.TOP
			it.dockTop = 4f
			it.dockBottom = 4f
		}

		val energy = ProfiledPowerGaugePanel(this, frame, menu.energy)
		val matter = ProfiledMatterGaugePanel(this, frame, menu.matter)

		matter.dockLeft = 2f
		energy.dock = Dock.LEFT
		matter.dock = Dock.LEFT

		matter.dockRight = 5f

		val drawGuide = BooleanButtonPanel.Checkbox(this, frame, menu.drawBuildingGuide, TranslatableComponent("otm.gui.draw_multiblock_guide"))
		drawGuide.dock = Dock.TOP

		Label(this, frame, text = TranslatableComponent("otm.gui.black_hole_generator.injection_rate.mode")).also {
			it.dock = Dock.TOP
			it.dockTop = 2f
		}

		val injectionRate = DecimalInputPanel(this, frame, menu.injectionRate, min = Decimal.ONE_TENTH, step = Decimal.ONE_TENTH)
		injectionRate.dock = Dock.TOP
		injectionRate.dockTop = 1f

		Label(this, frame, text = TranslatableComponent("otm.gui.black_hole_generator.target_mass.mode")).also {
			it.dock = Dock.TOP
			it.dockTop = 2f
		}

		val targetMass = DecimalInputPanel(this, frame, menu.targetMass, min = Decimal.ONE, step = Decimal(1000))
		targetMass.dock = Dock.TOP
		targetMass.dockTop = 1f
		targetMass.dockBottom = 4f

		for (mode in BlackHoleGeneratorBlockEntity.Mode.entries) {
			val (radio, label, canvas) = makeButtonLabel(
				this,
				frame,
				BooleanButtonPanel.Radio(this, prop = menu.mode.map({ it == mode }, { if (it) mode else BlackHoleGeneratorBlockEntity.Mode.TARGET_MASS })),
				Label(this, text = mode.label)
			)

			label.tooltips.add(mode.tooltip)
			radio.tooltips.add(mode.tooltip)
			canvas.tooltips.add(mode.tooltip)

			canvas.dock = Dock.TOP
			canvas.dockTop = 2f
		}

		DeviceControls(this, frame, redstoneConfig = menu.redstoneConfig)

		return frame
	}
}
