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 net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import ru.dbotthepony.mc.otm.client.ShiftPressedCond
import ru.dbotthepony.mc.otm.client.render.RenderGravity
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.DockProperty
import ru.dbotthepony.mc.otm.client.screen.panels.DockResizeMode
import ru.dbotthepony.mc.otm.client.screen.panels.DynamicLabel
import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel
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.slot.AbstractSlotPanel
import ru.dbotthepony.mc.otm.client.screen.panels.util.BackgroundPanel
import ru.dbotthepony.mc.otm.client.screen.widget.TallHorizontalProfiledPowerGaugePanel
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.util.formatPower
import ru.dbotthepony.mc.otm.menu.tech.FlywheelBatteryMenu
import java.util.function.Supplier

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

		frame.behaveAsWindow()
		frame.onClose { onClose() }

		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
		}

		Label(this, frame, TranslatableComponent("otm.gui.flywheel.current_loss_t")).also {
			it.dock = Dock.TOP
		}

		DynamicLabel(this, frame, textSupplier = Supplier { menu.currentLossPerTick.get().formatPower(decimalPlaces = 4, formatAsReadable = ShiftPressedCond) }).also {
			it.dock = Dock.TOP
		}

		Label(this, frame, TranslatableComponent("otm.gui.flywheel.current_loss_s")).also {
			it.dock = Dock.TOP
		}

		DynamicLabel(this, frame, textSupplier = Supplier { (menu.currentLossPerTick.get() * 20).formatPower(formatAsReadable = ShiftPressedCond) }).also {
			it.dock = Dock.TOP
		}

		TallHorizontalProfiledPowerGaugePanel(this, frame, menu.energy).also {
			it.dock = Dock.TOP
		}

		val coreStrip = BackgroundPanel.block(this, frame)

		coreStrip.dock = Dock.TOP
		coreStrip.dockTop = 4f

		val leftStrip = EditablePanel(this, coreStrip)
		leftStrip.dock = Dock.FILL

		object : AbstractSlotPanel<FlywheelBatteryScreen>(this@FlywheelBatteryScreen, coreStrip) {
			override val itemStack: ItemStack
				get() = menu.core.get()?.key?.asItem()?.let { ItemStack(it) } ?: ItemStack.EMPTY

			init {
				dock = Dock.RIGHT
				dockResize = DockResizeMode.NONE
			}
		}

		Label(this, leftStrip, text = TranslatableComponent("otm.gui.flywheel.core_material")).also {
			it.dock = Dock.TOP
			it.gravity = RenderGravity.CENTER_LEFT
		}

		DynamicLabel(this, leftStrip, textSupplier = Supplier { TranslatableComponent("otm.gui.flywheel.core_material_count", (menu.core.get()?.value ?: 0).toString()) }).also {
			it.dock = Dock.TOP
		}

		leftStrip.sizeToContents()
		coreStrip.sizeToContents()

		frame.sizeToContents()

		return frame
	}
}
