package ru.dbotthepony.mc.otm.core.multiblock

import com.google.common.collect.ImmutableList
import com.google.common.collect.ImmutableSet
import net.minecraft.core.BlockPos
import net.minecraft.world.level.block.Rotation
import net.minecraft.world.level.block.state.BlockState
import ru.dbotthepony.mc.otm.core.collect.collect
import ru.dbotthepony.mc.otm.core.collect.map
import java.util.function.Predicate

class ShapedMultiblockFactory(val north: ImmutableSet<Part>, val customChecks: ImmutableList<Predicate<IMultiblockAccess>>) {
	data class Part(
		val pos: BlockPos,
		val strategy: Strategy,
		val predicate: ImmutableList<BlockPredicate>,
		val children: ImmutableList<Part>,
		val blockStateTags: ImmutableSet<Any>,
		val blockTags: ImmutableSet<Any>,
		val blockEntityTags: ImmutableSet<BlockEntityTag<*>>,
		val displayBlocks: ImmutableSet<BlockState>,
	)

	/**
	 * Bakes multiblock configuration with specified [pos] as multiblock's root position
	 */
	fun create(pos: BlockPos): ShapedMultiblock {
		return ShapedMultiblock(pos, this)
	}

	val south: ImmutableSet<Part> = north.iterator().map { it.copy(pos = it.pos.rotate(Rotation.CLOCKWISE_180)) }.collect(ImmutableSet.toImmutableSet())
	val west: ImmutableSet<Part> = north.iterator().map { it.copy(pos = it.pos.rotate(Rotation.COUNTERCLOCKWISE_90)) }.collect(ImmutableSet.toImmutableSet())
	val east: ImmutableSet<Part> = north.iterator().map { it.copy(pos = it.pos.rotate(Rotation.CLOCKWISE_90)) }.collect(ImmutableSet.toImmutableSet())
}
