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

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.ButtonPanel
import ru.dbotthepony.mc.otm.client.screen.panels.button.makeDeviceControls
import ru.dbotthepony.mc.otm.client.screen.panels.input.NetworkNumberInputPanel
import ru.dbotthepony.mc.otm.core.util.formatPower
import ru.dbotthepony.mc.otm.menu.tech.EnergyCounterMenu
import java.util.function.Supplier

class EnergyCounterScreen(menu: EnergyCounterMenu, inventory: Inventory, title: Component) : MatteryScreen<EnergyCounterMenu>(menu, inventory, title) {
	override fun makeMainFrame(): FramePanel<MatteryScreen<*>> {
		val frame = super.makeMainFrame()!!

		frame.height = 160f
		frame.width += 40f

		val labels = ArrayList<EditablePanel<*>>()

		labels.add(Label(this, frame, text = TranslatableComponent("otm.gui.power.passed")))
		labels.add(DynamicLabel(this, frame, textSupplier = { menu.passed.formatPower(formatAsReadable = ShiftPressedCond) }))
		labels.add(Label(this, frame, text = TranslatableComponent("otm.gui.power.average")))
		labels.add(DynamicLabel(this, frame, textSupplier = { menu.average.formatPower(formatAsReadable = ShiftPressedCond) }))
		labels.add(Label(this, frame, text = TranslatableComponent("otm.gui.power.last_20_ticks")))
		labels.add(DynamicLabel(this, frame, textSupplier = { menu.last20Ticks.formatPower(formatAsReadable = ShiftPressedCond) }))
		labels.add(Label(this, frame, text = TranslatableComponent("otm.gui.power.last_tick")))
		labels.add(DynamicLabel(this, frame, textSupplier = { menu.lastTick.formatPower(formatAsReadable = ShiftPressedCond) }))
		labels.add(DynamicLabel(this, frame, textSupplier = { TranslatableComponent("block.overdrive_that_matters.energy_counter.facing", menu.inputDirection) }))

		for ((i, label) in labels.withIndex()) {
			if (i == 0) {
				label.dockTop = 5f
			} else {
				label.dockTop = 2f
			}

			label.dock = Dock.TOP
		}

		if (!menu.ply.isSpectator) {
			val button = ButtonPanel(this, frame, 0f, 0f, 0f, 20f, TranslatableComponent("block.overdrive_that_matters.energy_counter.switch"), onPress = Runnable { menu.switchDirection.input(null) })
			button.dock = Dock.TOP
			button.setDockMargin(4f, 5f, 4f, 0f)
		}

		val infoPanels = frame.fetchChildren()

		Label(this, frame, TranslatableComponent("block.overdrive_that_matters.energy_counter.limit")).also {
			it.dock = Dock.TOP
			it.dockTop = 10f
		}

		NetworkNumberInputPanel(this, frame, widget = menu.maxIOInput, networkValue = menu::maxIO).also {
			it.dock = Dock.TOP
			it.dockTop = 4f
		}

		val limitsPanels = frame.fetchChildren().filter { !infoPanels.contains(it) }

		val informationTab = frame.Tab()
		val limitsTab = frame.Tab()

		informationTab.showHidePanels(infoPanels)
		limitsTab.showHidePanels(limitsPanels)

		limitsTab.onClose!!.run()

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

		return frame
	}
}
