package ru.dbotthepony.mc.otm.compat.jade.providers

import net.minecraft.nbt.CompoundTag
import net.minecraft.resources.ResourceLocation
import ru.dbotthepony.mc.otm.block.entity.WorkerState
import ru.dbotthepony.mc.otm.block.entity.matter.MatterBottlerBlockEntity
import ru.dbotthepony.mc.otm.compat.jade.JadeTagKeys
import ru.dbotthepony.mc.otm.compat.jade.JadeUids
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.math.RGBAColor
import snownee.jade.api.BlockAccessor
import snownee.jade.api.IBlockComponentProvider
import snownee.jade.api.IServerDataProvider
import snownee.jade.api.ITooltip
import snownee.jade.api.config.IPluginConfig
import snownee.jade.api.ui.BoxStyle

object MatterBottlerProvider : IBlockComponentProvider, IServerDataProvider<BlockAccessor> {
	override fun getUid(): ResourceLocation = JadeUids.MATTER_BOTTLER

	override fun appendServerData(data: CompoundTag, accessor: BlockAccessor) {
		if (accessor.blockEntity is MatterBottlerBlockEntity) {
			val bottler = accessor.blockEntity as MatterBottlerBlockEntity

			val bottlerData = CompoundTag()
			bottlerData.putBoolean("isBottling", bottler.isBottling)
			bottlerData.putBoolean("isIdling", accessor.blockState.getValue(WorkerState.SEMI_WORKER_STATE) !== WorkerState.WORKING)
			bottlerData.putFloat("workProgress", bottler.getWorkProgress())
			data.put(JadeTagKeys.MATTER_BOTTLER_DATA, bottlerData)
		}
	}

	override fun appendTooltip(tooltip: ITooltip, accessor: BlockAccessor, config: IPluginConfig) {
		val data = accessor.serverData

		if (!data.contains(JadeTagKeys.MATTER_BOTTLER_DATA)) return
		val bottlerData = data.getCompound(JadeTagKeys.MATTER_BOTTLER_DATA)

		val elementHelper = tooltip.elementHelper
		tooltip.add(
			elementHelper.text(
				TranslatableComponent(
					"otm.jade.mode",
					if (bottlerData.getBoolean("isBottling"))
						TranslatableComponent("otm.jade.matter_bottler.mode.fill")
					else
						TranslatableComponent("otm.jade.matter_bottler.mode.drain")
				)
			)
		)

		if (!data.getBoolean("isIdling")) {
			val workProgress = data.getFloat("workProgress")
			val style = elementHelper.progressStyle().color(RGBAColor.WHITE.toARGB())

			tooltip.add(
				elementHelper.progress(
					workProgress,
					null,
					style,
					BoxStyle.DEFAULT,
					true
				)
			)
		}
	}
}
