package ru.dbotthepony.mc.otm.block.entity

import net.minecraft.core.BlockPos
import net.minecraft.tags.BlockTags
import net.minecraft.world.level.block.Blocks
import net.minecraft.world.level.block.entity.FurnaceBlockEntity
import net.minecraft.world.level.block.entity.HopperBlockEntity
import net.minecraft.world.level.block.state.BlockState
import ru.dbotthepony.mc.otm.core.multiblock.MultiblockStatus
import ru.dbotthepony.mc.otm.core.multiblock.multiblockEntity
import ru.dbotthepony.mc.otm.core.multiblock.shapedMultiblock
import ru.dbotthepony.mc.otm.registry.game.MBlockEntities
import ru.dbotthepony.mc.otm.registry.game.MBlocks

class MultiblockTestBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryBlockEntity(MBlockEntities.MULTIBLOCK_TEST, blockPos, blockState) {
	val config = CONFIG.create(blockPos)

	override fun tick() {
		super.tick()

		if (config.update(level!!) == MultiblockStatus.VALID) {
			println("It matches!")
			println("hopper block entities: ${config.blockEntities(HOPPERS)}")
		}
	}

	companion object {
		private val HOPPERS = multiblockEntity<HopperBlockEntity>()
		private val FURNACES = multiblockEntity<FurnaceBlockEntity>()

		val CONFIG = shapedMultiblock {
			block(MBlocks.MULTIBLOCK_TEST)

			and {
				top {
					or {
						block(Blocks.HOPPER)
						block(Blocks.CHEST)
						block(BlockTags.PLANKS)
					}

					tag(HOPPERS)
				}

				left(Blocks.FURNACE) {
					tag(FURNACES)

					top {
						or {
							block(Blocks.HOPPER)
							block(Blocks.CHEST)
							block(BlockTags.PLANKS)
						}

						tag(HOPPERS)
					}
				}

				right(Blocks.FURNACE) {
					tag(FURNACES)

					top {
						or {
							block(Blocks.HOPPER)
							block(Blocks.CHEST)
							block(BlockTags.PLANKS)
						}

						tag(HOPPERS)
					}
				}
			}
		}
	}
}
