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

import com.google.common.collect.ImmutableList
import net.minecraft.MethodsReturnNonnullByDefault
import javax.annotation.ParametersAreNonnullByDefault
import net.minecraft.world.level.block.EntityBlock
import net.minecraft.world.item.context.BlockPlaceContext
import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.block.state.properties.BooleanProperty
import net.minecraft.world.level.block.entity.BlockEntity
import net.minecraft.world.level.block.entity.BlockEntityType
import net.minecraft.world.level.block.entity.BlockEntityTicker
import net.minecraft.world.level.block.state.StateDefinition
import net.minecraft.core.BlockPos
import ru.dbotthepony.mc.otm.block.entity.tech.BatteryBankBlockEntity
import net.minecraft.world.level.BlockGetter
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.Block
import net.minecraft.world.phys.shapes.CollisionContext
import net.minecraft.world.phys.shapes.VoxelShape
import ru.dbotthepony.mc.otm.block.RotatableMatteryBlock
import ru.dbotthepony.mc.otm.block.getShapeForEachState
import ru.dbotthepony.mc.otm.core.get
import ru.dbotthepony.mc.otm.oncePre
import ru.dbotthepony.mc.otm.registry.MBlockEntities
import ru.dbotthepony.mc.otm.shapes.BlockShapes

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
class BatteryBankBlock : RotatableMatteryBlock(), EntityBlock {
	override fun <T : BlockEntity> getTicker(
		level: Level,
		p_153213_: BlockState,
		type: BlockEntityType<T>
	): BlockEntityTicker<T>? {
		if (level.isClientSide || type !== MBlockEntities.BATTERY_BANK)
			return null

		return BlockEntityTicker { _, _, _, tile -> if (tile is BatteryBankBlockEntity) tile.tick() }
	}

	override fun newBlockEntity(blockPos: BlockPos, blockState: BlockState): BlockEntity {
		return BatteryBankBlockEntity(blockPos, blockState)
	}

	private val shapes = getShapeForEachState(rotationProperty) { BlockShapes.BATTERY_BANK.rotateFromNorth(it[rotationProperty]).computeShape() }

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

	override fun faceToPlayer(context: BlockPlaceContext): Boolean {
		return false
	}
}
