package ru.dbotthepony.mc.otm.block.tech

import net.minecraft.core.BlockPos
import net.minecraft.world.item.DyeColor
import net.minecraft.world.level.BlockGetter
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.EntityBlock
import net.minecraft.world.level.block.entity.BlockEntity
import net.minecraft.world.level.block.entity.BlockEntityTicker
import net.minecraft.world.level.block.entity.BlockEntityType
import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.block.state.StateDefinition
import net.minecraft.world.phys.shapes.CollisionContext
import net.minecraft.world.phys.shapes.Shapes
import net.minecraft.world.phys.shapes.VoxelShape
import ru.dbotthepony.mc.otm.block.RotatableMatteryBlock
import ru.dbotthepony.mc.otm.block.addSimpleDescription
import ru.dbotthepony.mc.otm.block.entity.WorkerState
import ru.dbotthepony.mc.otm.block.entity.tech.AbstractPoweredFurnaceBlockEntity
import ru.dbotthepony.mc.otm.block.entity.tech.PoweredBlastFurnaceBlockEntity
import ru.dbotthepony.mc.otm.block.entity.tech.PoweredFurnaceBlockEntity
import ru.dbotthepony.mc.otm.block.entity.tech.PoweredSmokerBlockEntity
import ru.dbotthepony.mc.otm.block.getShapeForEachState
import ru.dbotthepony.mc.otm.core.get
import ru.dbotthepony.mc.otm.shapes.BlockShape
import ru.dbotthepony.mc.otm.shapes.BlockShapes

sealed class AbstractPoweredFurnaceBlock<T : AbstractPoweredFurnaceBlockEntity<*, *>>(
	val color: DyeColor?,
	val factory: (BlockPos, BlockState) -> T,
	shape: BlockShape?
) : RotatableMatteryBlock(DEFAULT_MACHINE_PROPERTIES), EntityBlock {
	init {
		tooltips.painted(color)
		addSimpleDescription()
		tooltips.doubleProcessing()
		tooltips.blockEntityEnergy()
	}

	override fun newBlockEntity(p_153215_: BlockPos, p_153216_: BlockState): T {
		return factory(p_153215_, p_153216_)
	}

	override fun createBlockStateDefinition(builder: StateDefinition.Builder<Block, BlockState>) {
		super.createBlockStateDefinition(builder)
		builder.add(WorkerState.WORKER_STATE)
	}

	private val shapes = getShapeForEachState(rotationProperty) {
		shape?.rotateFromNorth(it[rotationProperty])?.computeShape() ?: Shapes.block()
	}

	@Suppress("override_deprecation")
	override fun getShape(
		state: BlockState,
		blockGetter: BlockGetter,
		pos: BlockPos,
		context: CollisionContext
	): VoxelShape {
		return shapes[state]!!
	}

	override fun <T : BlockEntity?> getTicker(p_153212_: Level, p_153213_: BlockState, p_153214_: BlockEntityType<T>): BlockEntityTicker<T>? {
		if (p_153212_.isClientSide)
			return null

		return BlockEntityTicker { _, _, _, tile -> if (tile is AbstractPoweredFurnaceBlockEntity<*, *>) tile.tick() }
	}
}

class PoweredFurnaceBlock(color: DyeColor?) : AbstractPoweredFurnaceBlock<PoweredFurnaceBlockEntity>(color, ::PoweredFurnaceBlockEntity, BlockShapes.POWERED_FURNACE)
class PoweredBlastFurnaceBlock(color: DyeColor?) : AbstractPoweredFurnaceBlock<PoweredBlastFurnaceBlockEntity>(color, ::PoweredBlastFurnaceBlockEntity, BlockShapes.POWERED_BLAST_FURNACE)
class PoweredSmokerBlock(color: DyeColor?) : AbstractPoweredFurnaceBlock<PoweredSmokerBlockEntity>(color, ::PoweredSmokerBlockEntity, BlockShapes.POWERED_SMOKER)
