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

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.ButtonPanel
import ru.dbotthepony.mc.otm.client.screen.panels.button.makeDeviceControls
import ru.dbotthepony.mc.otm.client.screen.panels.slot.BatterySlotPanel
import ru.dbotthepony.mc.otm.client.screen.panels.slot.MatterCapacitorSlotPanel
import ru.dbotthepony.mc.otm.client.screen.widget.MatterGaugePanel
import ru.dbotthepony.mc.otm.client.screen.widget.PowerGaugePanel
import ru.dbotthepony.mc.otm.client.screen.widget.ProfiledMatterGaugePanel
import ru.dbotthepony.mc.otm.client.screen.widget.ProfiledPowerGaugePanel
import ru.dbotthepony.mc.otm.client.screen.widget.ProgressGaugePanel
import ru.dbotthepony.mc.otm.menu.matter.MatterBottlerMenu

class MatterBottlerScreen(menu: MatterBottlerMenu, inventory: Inventory, title: Component) :
	MatteryScreen<MatterBottlerMenu>(menu, inventory, title) {

	private var progress: ProgressGaugePanel<MatterBottlerScreen>? = null

	override fun makeMainFrame(): FramePanel<MatteryScreen<*>> {
		val frame = super.makeMainFrame()!!

		val p = ProfiledPowerGaugePanel(this, frame, menu.profiledEnergy, LEFT_MARGIN, GAUGE_TOP_WITH_SLOT)
		ProfiledMatterGaugePanel(this, frame, menu.matterWidget, LEFT_MARGIN + p.width, GAUGE_TOP_WITH_SLOT)

		BatterySlotPanel(this, frame, menu.batterySlot, LEFT_MARGIN, SLOT_TOP_UNDER_GAUGE)

		for (i in 0 .. 2) {
			MatterCapacitorSlotPanel(this, frame, menu.storageSlots[i], 31f + i * 18, PROGRESS_SLOT_TOP)
		}

		for (i in 3 .. 5) {
			MatterCapacitorSlotPanel(this, frame, menu.storageSlots[i], 116f + (i - 3) * 18, PROGRESS_SLOT_TOP)
		}

		progress = ProgressGaugePanel(this, frame, menu.progressWidget, 90f, PROGRESS_ARROW_TOP)

		if (minecraft?.player?.isSpectator != true) {
			ButtonPanel(this, frame, 46f, 69f, 100f, 20f, TranslatableComponent("otm.matter_bottler.switch_mode"), onPress = menu.workFlow::switchValue)
		}

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

		return frame
	}

	override fun containerTick() {
		super.containerTick()

		progress?.flop = !menu.workFlow.value
	}
}
