package ru.dbotthepony.mc.otm.capability.energy

import net.minecraft.ChatFormatting
import net.minecraft.network.chat.Component
import net.neoforged.neoforge.energy.IEnergyStorage
import ru.dbotthepony.mc.otm.capability.FlowDirection
import ru.dbotthepony.mc.otm.client.ShiftPressedCond
import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.util.formatPower

sealed interface IEnergyStorageImpl {
	val maxInput: Decimal?
	val maxOutput: Decimal?
}

internal fun batteryLevel(it: IEnergyStorage, tooltips: (Component) -> Unit) {
	tooltips(
		TranslatableComponent(
			"otm.item.power.storage",
			it.energyStored.formatPower(formatAsReadable = ShiftPressedCond),
			it.maxEnergyStored.formatPower(formatAsReadable = ShiftPressedCond)
		).withStyle(ChatFormatting.GRAY))
}

internal fun batteryLevel(it: IMatteryEnergyStorage, tooltips: (Component) -> Unit, displayMaxLevel: Boolean = true) {
	if (displayMaxLevel)
		tooltips(
			TranslatableComponent(
				"otm.item.power.storage",
				it.batteryLevel.formatPower(formatAsReadable = ShiftPressedCond),
				it.maxBatteryLevel.formatPower(formatAsReadable = ShiftPressedCond)
			).withStyle(ChatFormatting.GRAY))
	else
		tooltips(
			TranslatableComponent(
				"otm.item.power.storage0",
				it.batteryLevel.formatPower(formatAsReadable = ShiftPressedCond)
			).withStyle(ChatFormatting.GRAY))

	if (it is IEnergyStorageImpl) {
		when (it.energyFlow) {
			FlowDirection.INPUT -> {
				tooltips(TranslatableComponent("otm.item.power.throughput_mono", (it.maxInput ?: Decimal.POSITIVE_INFINITY).formatPower(formatAsReadable = ShiftPressedCond)).withStyle(ChatFormatting.GRAY))
			}

			FlowDirection.OUTPUT -> {
				tooltips(TranslatableComponent("otm.item.power.throughput_mono", (it.maxOutput ?: Decimal.POSITIVE_INFINITY).formatPower(formatAsReadable = ShiftPressedCond)).withStyle(ChatFormatting.GRAY))
			}

			FlowDirection.BI_DIRECTIONAL -> {
				tooltips(
					TranslatableComponent(
						"otm.item.power.throughput",
						(it.maxInput ?: Decimal.POSITIVE_INFINITY).formatPower(formatAsReadable = ShiftPressedCond),
						(it.maxOutput ?: Decimal.POSITIVE_INFINITY).formatPower(formatAsReadable = ShiftPressedCond)
					).withStyle(ChatFormatting.GRAY))
			}

			FlowDirection.NONE -> {}
		}
	}
}
