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: DeferredRegister<FluidType> = DeferredRegister.create(ForgeRegistries.Keys.FLUID_TYPES, OverdriveThatMatters.MOD_ID)
	private val fluids: DeferredRegister<Fluid> = DeferredRegister.create(ForgeRegistries.FLUIDS, OverdriveThatMatters.MOD_ID)

	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 path = ResourceLocation(OverdriveThatMatters.MOD_ID, "block/ph_kitty")

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

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

	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) }
}
