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.screen.MatteryScreen
import ru.dbotthepony.mc.otm.client.screen.panels.Dock
import ru.dbotthepony.mc.otm.client.screen.panels.DockResizeMode
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.ScrollbarBackgroundPanel
import ru.dbotthepony.mc.otm.client.screen.panels.button.DeviceControls
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.panels.slot.SlotPanel
import ru.dbotthepony.mc.otm.client.screen.widget.ProgressGaugePanel
import ru.dbotthepony.mc.otm.client.screen.widget.WideProfiledPowerGaugePanel
import ru.dbotthepony.mc.otm.compat.jei.isJeiLoaded
import ru.dbotthepony.mc.otm.menu.tech.AbstractProcessingMachineMenu
import kotlin.math.roundToInt

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

		WideProfiledPowerGaugePanel(this, frame, menu.profiledEnergy, LEFT_MARGIN, GAUGE_TOP_WITH_SLOT)
		BatterySlotPanel(this, frame, menu.batterySlot, LEFT_MARGIN, SLOT_TOP_UNDER_GAUGE)

		val height = menu.processingTuples.size * (AbstractSlotPanel.SIZE + 2f) - 2f

		val progressPanels = ArrayList<ProgressGaugePanel<*>>()

		val parentTarget = if (height >= frame.effectiveHeight - 10f) {
			val parent = ScrollbarBackgroundPanel(this, frame)
			parent.removeBackground()
			parent.dock = Dock.FILL
			parent.dockLeft = 50f
			parent.dockRight = 4f
			parent.canvas
		} else {
			val parent = EditablePanel(this, frame)
			parent.dock = Dock.FILL
			frame.performLayout()
			parent.dockPaddingTop = ((parent.effectiveHeight - height) / 2f).roundToInt().toFloat()
			parent.dockLeft = 50f
			parent
		}

		for ((input, output, progress) in menu.processingTuples) {
			val row = EditablePanel(this, parentTarget, height = AbstractSlotPanel.SIZE)
			row.dock = Dock.TOP
			row.dockBottom = 2f

			SlotPanel(this, row, input).also {
				it.dock = Dock.LEFT
			}

			progressPanels.add(ProgressGaugePanel(this, row, progress).also {
				it.dock = Dock.LEFT
				it.dockLeft = 4f
				it.dockRight = 4f
				it.dockResize = DockResizeMode.NONE
			})

			SlotPanel(this, row, output).also {
				it.dock = Dock.LEFT
			}
		}

		DeviceControls(this, frame, redstoneConfig = menu.redstoneConfig, energyConfig = menu.energyConfig, itemConfig = menu.itemConfig, balanceInputs = menu.balanceInputs, upgrades = menu.upgrades, experience = menu.experience)

		if (isJeiLoaded) {
			val recipeTypes = menu.recipeTypes

			if (recipeTypes != null) {
				progressPanels.forEach {
					it.setRecipeType(recipeTypes)
				}
			}
		}

		return frame
	}
}
