package ru.dbotthepony.mc.otm.registry.objects

import net.minecraft.world.item.BlockItem
import net.minecraft.world.item.DyeColor
import net.minecraft.world.item.Item
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.state.BlockBehaviour
import net.minecraftforge.registries.DeferredRegister
import net.minecraftforge.registries.RegistryObject
import ru.dbotthepony.mc.otm.core.collect.SupplierMap
import ru.dbotthepony.mc.otm.core.util.WriteOnce

/**
 * Base + Colored
 */
class DecorativeBlock(
	baseName: String,
	private val provider: (DyeColor?) -> Block,
) : ColoredDecorativeBlock(baseName, provider) {
	private var _block: RegistryObject<Block> by WriteOnce()
	private var _item: RegistryObject<Item> by WriteOnce()

	val block: Block get() = _block.get()
	val item: Item get() = _item.get()

	val allBlocks: Map<DyeColor?, Block> by lazy {
		check(registeredBlocks) { "Didn't register items yet" }
		SupplierMap(blockMap.map { it.key to it.value::get }.toMutableList().also { it.add(null to _block::get) })
	}

	val allItems: Map<DyeColor?, Item> by lazy {
		check(registeredItems) { "Didn't register items yet" }
		SupplierMap(itemMap.map { it.key to it.value::get }.toMutableList().also { it.add(null to _item::get) })
	}

	override fun registerBlocks(registry: DeferredRegister<Block>) {
		_block = registry.register(baseName) { provider.invoke(null) }
		super.registerBlocks(registry)
	}

	override fun registerItems(registry: DeferredRegister<Item>) {
		_item = registry.register(baseName) { BlockItem(_block.get(), Item.Properties().stacksTo(64)) }
		super.registerItems(registry)
	}

	companion object {
		fun simple(baseName: String, provider: (DyeColor?) -> BlockBehaviour.Properties): DecorativeBlock {
			return DecorativeBlock(baseName) {
				Block(provider.invoke(it))
			}
		}
	}
}
