package ru.dbotthepony.mc.otm.menu.tech

import net.minecraft.core.Direction
import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.level.block.Block
import ru.dbotthepony.kommons.util.getValue
import ru.dbotthepony.kommons.util.setValue
import ru.dbotthepony.mc.otm.block.entity.RedstoneSetting
import ru.dbotthepony.mc.otm.block.tech.EnergyCounterBlock
import ru.dbotthepony.mc.otm.block.entity.tech.EnergyCounterBlockEntity
import ru.dbotthepony.mc.otm.core.chart.DecimalHistoryChart
import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.core.math.toDecimal
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import ru.dbotthepony.mc.otm.menu.input.EnumInputWithFeedback
import ru.dbotthepony.mc.otm.menu.input.IntInputWithFeedback
import ru.dbotthepony.mc.otm.registry.game.MMenus
import java.math.BigDecimal

class EnergyCounterMenu(
	p_38852_: Int,
	inventory: Inventory,
	tile: EnergyCounterBlockEntity? = null
) : MatteryMenu(MMenus.ENERGY_COUNTER, p_38852_, inventory, tile) {
	var passed by mSynchronizer.decimal()
	var lastTick by mSynchronizer.decimal()
	var maxIO by mSynchronizer.decimal()

	val history5s = tile?.history5s ?: DecimalHistoryChart(1, 100)
	val history15s = tile?.history15s ?: DecimalHistoryChart(3, 100)
	val history1m = tile?.history1m ?: DecimalHistoryChart(12, 100)
	val history10m = tile?.history10m ?: DecimalHistoryChart(120, 100)
	val history1h = tile?.history1h ?: DecimalHistoryChart(720, 100)
	val history6h = tile?.history6h ?: DecimalHistoryChart(720 * 6, 100)
	val history24h = tile?.history24h ?: DecimalHistoryChart(720 * 24, 100)

	init {
		mSynchronizer.add(history5s)
		mSynchronizer.add(history15s)
		mSynchronizer.add(history1m)
		mSynchronizer.add(history10m)
		mSynchronizer.add(history1h)
		mSynchronizer.add(history6h)
		mSynchronizer.add(history24h)
	}

	val switchDirection = oneWayInput {
		if (tile is EnergyCounterBlockEntity)
			tile.level?.setBlock(tile.blockPos, tile.blockState.setValue(EnergyCounterBlock.INPUT_DIRECTION, tile.blockState.getValue(EnergyCounterBlock.INPUT_DIRECTION).opposite), Block.UPDATE_ALL)
	}

	val displayChartOnBlock = IntInputWithFeedback(this)
	val redstone = EnumInputWithFeedback(this, RedstoneSetting::class.java)

	init {
		if (tile != null) {
			redstone.with(tile.redstoneControl::redstoneSetting)
			displayChartOnBlock.with(tile::displayChartOnBlock)
		}
	}

	var inputDirection: Direction by mSynchronizer.enum(Direction.UP)

	val maxIOInput = decimalInput {
		if (tile is EnergyCounterBlockEntity) {
			if (it.signum() < 0) {
				tile.ioLimit = null
			} else {
				tile.ioLimit = it
			}
		}
	}

	override fun beforeBroadcast() {
		super.beforeBroadcast()

		if (tile is EnergyCounterBlockEntity) {
			passed = tile.passed
			lastTick = tile.lastTick
			inputDirection = tile.blockState.getValue(EnergyCounterBlock.INPUT_DIRECTION)

			maxIO = tile.ioLimit?.toDecimal() ?: -Decimal.ONE
		}
	}

	companion object {
		private val MINUS_ONE = -BigDecimal.ONE
	}
}
