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

import net.minecraft.nbt.CompoundTag
import net.minecraft.resources.ResourceLocation
import net.minecraft.server.level.ServerPlayer
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.entity.BlockEntity
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
import snownee.jade.api.ui.IElementHelper

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

	override fun appendServerData(data: CompoundTag, player: ServerPlayer, level: Level, bottler: BlockEntity?, state: Boolean) {
		if (bottler is MatterBottlerBlockEntity) {
			val bottlerData = CompoundTag()
			bottlerData.putBoolean("isBottling", bottler.isBottling)
			bottlerData.putBoolean("isIdling", bottler.blockState.getValue(WorkerState.SEMI_WORKER_STATE) !== WorkerState.WORKING)
			bottlerData.putFloat("workProgress", bottler.workProgress)
			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 = IElementHelper.get()
		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")) {
			tooltip.add(
				elementHelper.progress(
					data.getFloat("workProgress"),
					null,
					elementHelper.progressStyle().color(RGBAColor.WHITE.toARGB()),
					BoxStyle.DEFAULT,
					true
				)
			)
		}
	}
}
