package ru.dbotthepony.mc.otm.datagen.recipes

import com.google.gson.JsonObject
import com.google.gson.JsonPrimitive
import com.mojang.serialization.JsonOps
import net.minecraft.data.recipes.FinishedRecipe
import net.minecraft.resources.ResourceLocation
import net.minecraft.util.valueproviders.ConstantFloat
import net.minecraft.util.valueproviders.FloatProvider
import net.minecraft.world.item.crafting.RecipeSerializer
import ru.dbotthepony.mc.otm.recipe.PlatePressRecipe
import ru.dbotthepony.mc.otm.core.set
import ru.dbotthepony.mc.otm.core.toJsonStrict

class PlatePressFinishedRecipe(private val recipe: PlatePressRecipe) : FinishedRecipe {
	override fun serializeRecipeData(it: JsonObject) {
		it["input"] = recipe.input.toJson()

		it["result"] = recipe.output.toJson().also {
			if (it is JsonObject && recipe.count != 1)
				it["count"] = JsonPrimitive(recipe.count)
		}

		it["work_time"] = JsonPrimitive(recipe.workTime)
		it["experience"] = FloatProvider.CODEC.toJsonStrict(recipe.experience)
	}

	override fun getId(): ResourceLocation {
		return recipe.id
	}

	override fun getType(): RecipeSerializer<*> {
		return PlatePressRecipe.Companion
	}

	override fun serializeAdvancement(): JsonObject? {
		return null
	}

	override fun getAdvancementId(): ResourceLocation? {
		return null
	}
}

class PlatePressShallowFinishedRecipe(
	private val id: ResourceLocation,
	private val input: ResourceLocation,
	private val output: ResourceLocation,
	private val count: Int = 1,
	private val workTime: Int = 200,
	private val experience: FloatProvider = ConstantFloat.ZERO,
) : FinishedRecipe {
	override fun serializeRecipeData(it: JsonObject) {
		it["input"] = JsonObject().also {
			it["tag"] = JsonPrimitive(input.toString())
		}

		it["result"] = JsonObject().also {
			it["tag"] = JsonPrimitive(output.toString())

			if (count != 1)
				it["count"] = JsonPrimitive(count)
		}

		it["work_time"] = JsonPrimitive(workTime)
		it["experience"] = FloatProvider.CODEC.toJsonStrict(experience)
	}

	override fun getId(): ResourceLocation {
		return id
	}

	override fun getType(): RecipeSerializer<*> {
		return PlatePressRecipe.Companion
	}

	override fun serializeAdvancement(): JsonObject? {
		return null
	}

	override fun getAdvancementId(): ResourceLocation? {
		return null
	}
}
