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

import net.minecraft.ChatFormatting
import net.minecraft.network.chat.Component
import net.minecraftforge.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: MutableList<Component>) {
	tooltips.add(
		TranslatableComponent(
			"otm.item.power.storage",
			it.energyStored.formatPower(formatAsReadable = ShiftPressedCond),
			it.maxEnergyStored.formatPower(formatAsReadable = ShiftPressedCond)
		).withStyle(ChatFormatting.GRAY))
}

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

	if (it is IEnergyStorageImpl) {
		when (it.energyFlow) {
			FlowDirection.INPUT -> {
				if (it.maxInput != null) {
					tooltips.add(
						TranslatableComponent("otm.item.power.throughput_mono", it.maxInput!!.formatPower(formatAsReadable = ShiftPressedCond)).withStyle(
							ChatFormatting.GRAY
						))
				} else {
					tooltips.add(
						TranslatableComponent(
							"otm.item.power.throughput_mono",
							TranslatableComponent("otm.item.power.infinity").withStyle(ChatFormatting.GRAY)
						).withStyle(ChatFormatting.GRAY))
				}
			}

			FlowDirection.OUTPUT -> {
				if (it.maxOutput != null) {
					tooltips.add(
						TranslatableComponent("otm.item.power.throughput_mono", it.maxOutput!!.formatPower(formatAsReadable = ShiftPressedCond)).withStyle(
							ChatFormatting.GRAY
						))
				} else {
					tooltips.add(
						TranslatableComponent(
							"otm.item.power.throughput_mono",
							TranslatableComponent("otm.item.power.infinity").withStyle(ChatFormatting.GRAY)
						).withStyle(ChatFormatting.GRAY))
				}
			}

			FlowDirection.BI_DIRECTIONAL -> {
				val maxInput = it.maxInput
				val maxOutput = it.maxOutput

				if (maxInput != null && maxOutput != null) {
					tooltips.add(
						TranslatableComponent(
							"otm.item.power.throughput",
							maxInput.formatPower(formatAsReadable = ShiftPressedCond),
							maxOutput.formatPower(formatAsReadable = ShiftPressedCond)
						).withStyle(ChatFormatting.GRAY))
				} else if (maxInput != null) {
					tooltips.add(
						TranslatableComponent(
							"otm.item.power.throughput",
							maxInput.formatPower(formatAsReadable = ShiftPressedCond),
							TranslatableComponent("otm.item.power.infinity").withStyle(ChatFormatting.GRAY)
						).withStyle(ChatFormatting.GRAY))
				} else if (maxOutput != null) {
					tooltips.add(
						TranslatableComponent(
							"otm.item.power.throughput",
							TranslatableComponent("otm.item.power.infinity").withStyle(ChatFormatting.GRAY),
							maxOutput.formatPower(formatAsReadable = ShiftPressedCond),
						).withStyle(ChatFormatting.GRAY))
				} else {
					tooltips.add(
						TranslatableComponent(
							"otm.item.power.throughput",
							TranslatableComponent("otm.item.power.infinity").withStyle(ChatFormatting.GRAY),
							TranslatableComponent("otm.item.power.infinity").withStyle(ChatFormatting.GRAY),
						).withStyle(ChatFormatting.GRAY))
				}
			}

			FlowDirection.NONE -> {}
		}
	}
}
