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

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.core.TranslatableComponent
import ru.dbotthepony.mc.otm.client.screen.panels.*
import ru.dbotthepony.mc.otm.client.screen.panels.button.makeDeviceControls
import ru.dbotthepony.mc.otm.client.screen.panels.slot.AbstractSlotPanel
import ru.dbotthepony.mc.otm.client.screen.panels.slot.BatterySlotPanel
import ru.dbotthepony.mc.otm.client.screen.widget.HorizontalPowerGaugePanel
import ru.dbotthepony.mc.otm.core.util.formatPower

import ru.dbotthepony.mc.otm.menu.storage.StoragePowerSupplierMenu

class StoragePowerSupplierScreen(menu: StoragePowerSupplierMenu, inventory: Inventory, title: Component) :
	MatteryScreen<StoragePowerSupplierMenu>(menu, inventory, title) {
	override fun makeMainFrame(): FramePanel<MatteryScreen<*>> {
		val frame = FramePanel(this, width = 200f, height = 60f, title)

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

		HorizontalPowerGaugePanel(this, frame, menu.energyWidget).also {
			it.dock = Dock.BOTTOM
			it.dockResize = DockResizeMode.NONE
		}

		val topStrip = EditablePanel(this, frame, height = AbstractSlotPanel.SIZE)

		topStrip.dock = Dock.TOP
		topStrip.dockTop = 3f

		BatterySlotPanel(this, topStrip, menu.batterySlot).also {
			it.dock = Dock.LEFT
			it.dockRight = 3f
		}

		val labels = EditablePanel(this, topStrip)

		labels.dock = Dock.FILL

		object : Label<StoragePowerSupplierScreen>(this@StoragePowerSupplierScreen, labels) {
			init {
				dock = Dock.TOP
			}

			override fun tickInner() {
				super.tickInner()

				text = TranslatableComponent(
					"otm.item.power.passed",
					menu.totalTransferred.formatPower()
				)
			}
		}

		object : Label<StoragePowerSupplierScreen>(this@StoragePowerSupplierScreen, labels) {
			init {
				dock = Dock.TOP
			}

			override fun tickInner() {
				super.tickInner()

				text = TranslatableComponent(
					"otm.gui.power_supplier.active_nodes",
					menu.activeNodes
				)
			}
		}

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

		return frame
	}
}
