package ru.dbotthepony.mc.otm.block.matter

import com.google.common.collect.ImmutableMap
import net.minecraft.world.level.block.EntityBlock
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.block.entity.BlockEntity
import ru.dbotthepony.mc.otm.block.entity.matter.MatterPanelBlockEntity
import net.minecraft.world.phys.shapes.VoxelShape
import net.minecraft.world.phys.shapes.Shapes
import net.minecraft.world.level.BlockGetter
import net.minecraft.world.phys.shapes.CollisionContext
import net.minecraft.world.item.context.BlockPlaceContext
import ru.dbotthepony.mc.otm.block.RotatableMatteryBlock
import ru.dbotthepony.mc.otm.core.math.BlockRotation
import ru.dbotthepony.mc.otm.core.math.BlockRotationFreedom
import ru.dbotthepony.mc.otm.core.math.rotationTwo

class MatterPanelBlock : RotatableMatteryBlock(), EntityBlock {
	override fun newBlockEntity(blockPos: BlockPos, blockState: BlockState): BlockEntity {
		return MatterPanelBlockEntity(blockPos, blockState)
	}

	private val shapes: ImmutableMap<BlockState, VoxelShape>

	init {
		registerDefaultState(getStateDefinition().any().setValue(BlockRotationFreedom.TWO.property, BlockRotation.SOUTH))

		shapes = getShapeForEachState {
			when (it.rotationTwo.front) {
				Direction.NORTH -> Shapes.box(
					0.0,
					0.0,
					0.625,
					1.0,
					1.0,
					1.0
				)
				Direction.EAST -> Shapes.box(
					0.0,
					0.0,
					0.0,
					0.375,
					1.0,
					1.0
				)
				Direction.WEST -> Shapes.box(
					0.625,
					0.0,
					0.0,
					1.0,
					1.0,
					1.0
				)
				Direction.DOWN -> Shapes.box(
					0.0,
					0.625,
					0.0,
					1.0,
					1.0,
					1.0
				)
				Direction.UP -> Shapes.box(
					0.0,
					0.0,
					0.0,
					1.0,
					0.375,
					1.0
				)
				else -> Shapes.box(
					0.0,
					0.0,
					0.0,
					1.0,
					1.0,
					0.375
				)
			}
		}
	}

	override fun getShape(
		p_60555_: BlockState,
		p_60556_: BlockGetter,
		p_60557_: BlockPos,
		p_60558_: CollisionContext
	): VoxelShape {
		return shapes[p_60555_]!!
	}

	override fun rotationFreedom(): BlockRotationFreedom {
		return BlockRotationFreedom.TWO
	}

	override fun getStateForPlacement(context: BlockPlaceContext): BlockState? {
		return defaultBlockState().setValue(BlockRotationFreedom.TWO.property, BlockRotation.of(context.clickedFace))
	}
}
