package ru.dbotthepony.mc.otm.registry

import net.minecraft.client.renderer.BiomeColors
import net.minecraft.core.BlockPos
import net.minecraft.world.item.ItemStack
import net.minecraft.world.level.BlockAndTintGetter
import net.minecraft.world.level.block.state.BlockState
import net.minecraftforge.client.event.RegisterColorHandlersEvent
import net.minecraftforge.eventbus.api.IEventBus

object MBlockColors {
	private const val DEFAULT_WATER_TINT: Int = 0x3F76E4

	private fun registerBlockColors(event: RegisterColorHandlersEvent.Block) {
		event.register({ state: BlockState, light: BlockAndTintGetter?, pos: BlockPos?, index: Int ->
			if (index == 0) {
				if (light == null || pos == null)
					DEFAULT_WATER_TINT
				else
					BiomeColors.getAverageWaterColor(light, pos)
			} else -1
		}, MBlocks.COBBLESTONE_GENERATOR)
	}

	private fun registerItemColors(event: RegisterColorHandlersEvent.Item) {
		event.register({ stack: ItemStack, index: Int ->
			if (index == 0) DEFAULT_WATER_TINT else -1
		}, MBlocks.COBBLESTONE_GENERATOR)
	}

	internal fun register(bus: IEventBus) {
		bus.addListener(this::registerBlockColors)
		bus.addListener(this::registerItemColors)
	}
}
