package ru.dbotthepony.mc.otm.registry

import net.minecraft.resources.ResourceLocation
import net.minecraft.sounds.SoundEvents
import net.minecraft.world.item.Rarity
import net.minecraft.world.level.material.Fluid
import net.minecraftforge.client.extensions.common.IClientFluidTypeExtensions
import net.minecraftforge.common.SoundActions
import net.minecraftforge.eventbus.api.IEventBus
import net.minecraftforge.fluids.FluidType
import net.minecraftforge.fluids.ForgeFlowingFluid
import net.minecraftforge.registries.DeferredRegister
import net.minecraftforge.registries.ForgeRegistries
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import java.util.function.Consumer

object MFluids {
	private val types = MDeferredRegister(ForgeRegistries.Keys.FLUID_TYPES)
	private val fluids = MDeferredRegister(ForgeRegistries.FLUIDS)

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

	private fun makeXpProps(): ForgeFlowingFluid.Properties {
		return ForgeFlowingFluid.Properties(::LIQUID_XP_TYPE, ::LIQUID_XP, ::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: ForgeFlowingFluid.Source by fluids.register("liquid_xp") { ForgeFlowingFluid.Source(xpProps) }
	val LIQUID_XP_FLOWING: ForgeFlowingFluid.Flowing by fluids.register("liquid_xp_flowing") { ForgeFlowingFluid.Flowing(xpProps) }
}
