package ru.dbotthepony.mc.otm.block

import net.minecraft.core.BlockPos
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.SoundType
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.material.MapColor
import net.minecraft.world.level.material.PushReaction
import net.minecraft.world.phys.shapes.CollisionContext
import net.minecraft.world.phys.shapes.VoxelShape
import ru.dbotthepony.mc.otm.block.entity.blackhole.BlackHoleBlockEntity
import ru.dbotthepony.mc.otm.registry.MBlockEntities
import ru.dbotthepony.mc.otm.shapes.BlockShapes

class BlackHoleBlock :
	Block(Properties.of().mapColor(MapColor.COLOR_BLACK).noCollission().pushReaction(PushReaction.BLOCK).sound(SoundType.STONE).strength(-1f, 7200000.0f)), EntityBlock {
	override fun getShape(
		p_60555_: BlockState,
		p_60556_: BlockGetter,
		p_60557_: BlockPos,
		p_60558_: CollisionContext
	): VoxelShape {
		return SHAPE
	}

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

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

		if (p_153212_.isClientSide) {
			return BlockEntityTicker { _, _, _, t -> if (t is BlackHoleBlockEntity) t.clientTick() }
		}

		return BlockEntityTicker { _, _, _, t -> if (t is BlackHoleBlockEntity) t.tick() }
	}

	companion object {
		private val SHAPE = BlockShapes.BLACK_HOLE.computeShape()
	}
}
