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

import net.minecraft.network.chat.Component
import net.minecraft.world.entity.player.Inventory
import ru.dbotthepony.kommons.math.RGBAColor
import ru.dbotthepony.mc.otm.client.ShiftPressedCond
import ru.dbotthepony.mc.otm.client.render.TextIcon
import ru.dbotthepony.mc.otm.client.render.Widgets18
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.BooleanButtonPanel
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.TextInputPanel
import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.core.util.formatPower
import ru.dbotthepony.mc.otm.menu.tech.EnergyCounterMenu

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

		frame.height = 180f
		frame.width += 80f

		val graphs = listOf(
			menu.history5s to "otm.gui.time.short.5s",
			menu.history15s to "otm.gui.time.short.15s",
			menu.history1m to "otm.gui.time.short.1m",
			menu.history10m to "otm.gui.time.short.10m",
			menu.history1h to "otm.gui.time.short.1h",
			menu.history6h to "otm.gui.time.short.6h",
			menu.history24h to "otm.gui.time.short.24h",
		)

		kotlin.run {
			var i = 1

			for ((graph, text) in graphs) {
				val tab = frame.Tab(activeIcon = TextIcon(color = RGBAColor.BLACK, font = font, text = TranslatableComponent(text)))
				val panel = DecimalHistoryChartPanel(this, tab.canvas, graph, formatText = { it.formatPower(formatAsReadable = ShiftPressedCond) })
				panel.dock = Dock.FILL

				val current = i++
				val button = BooleanButtonPanel.Checkbox(this, tab.canvas, menu.displayChartOnBlock.map({ it == current }, { current }), TranslatableComponent("block.overdrive_that_matters.energy_counter.display_this"))
				button.dock = Dock.BOTTOM
				button.dockTop = 2f
				button.displayUncheckedIcon = false
			}
		}

		val informationTab = frame.Tab(activeIcon = Widgets18.STATISTICS_TAB)
		val limitsTab = frame.Tab(activeIcon = Widgets18.SETTINGS_TAB)

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

		labels.add(Label(this, informationTab.canvas, text = TranslatableComponent("otm.gui.power.passed")))
		labels.add(DynamicLabel(this, informationTab.canvas, textSupplier = { menu.passed.formatPower(formatAsReadable = ShiftPressedCond) }))
		labels.add(Label(this, informationTab.canvas, text = TranslatableComponent("otm.gui.power.average")))
		labels.add(DynamicLabel(this, informationTab.canvas, textSupplier = { menu.history5s.calculateAverage(20).formatPower(formatAsReadable = ShiftPressedCond) }))
		labels.add(Label(this, informationTab.canvas, text = TranslatableComponent("otm.gui.power.last_20_ticks")))
		labels.add(DynamicLabel(this, informationTab.canvas, textSupplier = { menu.history5s.calculateSum(20).formatPower(formatAsReadable = ShiftPressedCond) }))
		labels.add(Label(this, informationTab.canvas, text = TranslatableComponent("otm.gui.power.last_tick")))
		labels.add(DynamicLabel(this, informationTab.canvas, textSupplier = { menu.lastTick.formatPower(formatAsReadable = ShiftPressedCond) }))
		labels.add(DynamicLabel(this, informationTab.canvas, textSupplier = { TranslatableComponent("block.overdrive_that_matters.energy_counter.facing", menu.inputDirection) }))
		labels.add(BooleanButtonPanel.Checkbox(this, informationTab.canvas, menu.displayChartOnBlock.map({ it == 0 }, { 0 }), TranslatableComponent("block.overdrive_that_matters.energy_counter.display_this")).also { it.displayUncheckedIcon = false })

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

			label.dock = Dock.TOP
		}

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

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

		TextInputPanel(this, limitsTab.canvas).also {
			it.dock = Dock.TOP
			it.dockTop = 4f

			it.allowNumbersAndSign()
			it.onTextChange { new, old ->
				try {
					var value = Decimal(new)

					if (value < Decimal.ZERO)
						value = Decimal.MINUS_ONE

					if (value != menu.maxIOInput)
						menu.maxIOInput.accept(value)
				} catch (err: NumberFormatException) {
					// do nothing
				}
			}

			it.onIdle {
				it.text = menu.maxIO.toString()
			}
		}

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

		return frame
	}
}
