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.ShiftPressedCond
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.ProgressGaugePanel
import ru.dbotthepony.mc.otm.client.screen.widget.TallHorizontalPowerGaugePanel
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 = 68f, title)

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

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

		strip.dock = Dock.BOTTOM
		strip.dockTop = 3f

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

		SpritePanel(this, strip, ProgressGaugePanel.GAUGE_BACKGROUND).also {
			it.dock = Dock.LEFT
			it.dockRight = 3f
			it.dockResize = DockResizeMode.NONE
		}

		TallHorizontalPowerGaugePanel(this, strip, menu.energyWidget).also {
			it.dock = Dock.LEFT
			it.dockResize = DockResizeMode.NONE
		}

		val labels = EditablePanel(this, frame)
		labels.dock = Dock.FILL
		labels.dockTop = 4f

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

			override fun tickInner() {
				super.tickInner()

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

		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
	}
}
