package ru.dbotthepony.mc.otm.registry.game

import net.minecraft.core.registries.BuiltInRegistries
import net.minecraft.resources.ResourceLocation
import net.minecraft.sounds.SoundEvents
import net.minecraft.world.item.Rarity
import net.neoforged.bus.api.IEventBus
import net.neoforged.neoforge.client.extensions.common.IClientFluidTypeExtensions
import net.neoforged.neoforge.common.SoundActions
import net.neoforged.neoforge.fluids.BaseFlowingFluid
import net.neoforged.neoforge.fluids.FluidType
import net.neoforged.neoforge.registries.NeoForgeRegistries
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.core.ResourceLocation
import ru.dbotthepony.mc.otm.registry.MDeferredRegister
import java.util.function.Consumer

object MFluids {
	private val types = MDeferredRegister(NeoForgeRegistries.FLUID_TYPES.key())
	private val fluids = MDeferredRegister(BuiltInRegistries.FLUID)

	fun register(bus: IEventBus) {
		types.register(bus)
		fluids.register(bus)
	}

	private fun makeXpProps(): BaseFlowingFluid.Properties {
		return BaseFlowingFluid.Properties(
			MFluids::LIQUID_XP_TYPE,
			MFluids::LIQUID_XP,
			MFluids::LIQUID_XP_FLOWING
		).bucket(MItems::LIQUID_XP_BUCKET).block(MBlocks::LIQUID_XP)
	}

	private val xpProps = makeXpProps()

	val LIQUID_XP_TYPE: FluidType by types.register("liquid_xp") {
		object : FluidType(
			Properties.create()
				.rarity(Rarity.UNCOMMON)
				.canDrown(true)
				.canExtinguish(false)
				.canSwim(true)
				.canHydrate(false)
				.canPushEntity(true)
				.canConvertToSource(false)
				.fallDistanceModifier(0f)
				.sound(SoundActions.BUCKET_FILL, SoundEvents.BUCKET_FILL)
				.sound(SoundActions.BUCKET_EMPTY, SoundEvents.BUCKET_EMPTY)
				.sound(SoundActions.FLUID_VAPORIZE, SoundEvents.FIRE_EXTINGUISH)
				.descriptionId("block.overdrive_that_matters.liquid_xp")
		) {
			override fun initializeClient(consumer: Consumer<IClientFluidTypeExtensions>) {
				val still = ResourceLocation(OverdriveThatMatters.MOD_ID, "block/liquid_xp_still")
				val flowing = ResourceLocation(OverdriveThatMatters.MOD_ID, "block/liquid_xp_flowing")

				consumer.accept(object : IClientFluidTypeExtensions {
					override fun getStillTexture(): ResourceLocation {
						return still
					}

					override fun getFlowingTexture(): ResourceLocation {
						return flowing
					}
				})
			}
		}
	}

	val LIQUID_XP: BaseFlowingFluid.Source by fluids.register("liquid_xp") { BaseFlowingFluid.Source(xpProps) }
	val LIQUID_XP_FLOWING: BaseFlowingFluid.Flowing by fluids.register("liquid_xp_flowing") { BaseFlowingFluid.Flowing(
		xpProps
	) }
}
