package ru.dbotthepony.mc.otm.block.storage

import net.minecraft.core.BlockPos
import net.minecraft.network.chat.Component
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.TooltipFlag
import net.minecraft.world.item.context.BlockPlaceContext
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.BooleanOp
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.CableBlock
import ru.dbotthepony.mc.otm.block.RotatableMatteryBlock
import ru.dbotthepony.mc.otm.block.StorageCableBlock
import ru.dbotthepony.mc.otm.block.entity.MatteryPoweredBlockEntity
import ru.dbotthepony.mc.otm.block.entity.storage.StorageBusBlockEntity
import ru.dbotthepony.mc.otm.capability.energy.WorkerEnergyStorage
import ru.dbotthepony.mc.otm.core.get
import ru.dbotthepony.mc.otm.core.math.BlockRotation
import ru.dbotthepony.mc.otm.core.math.BlockRotationFreedom
import ru.dbotthepony.mc.otm.core.math.facingTwo
import ru.dbotthepony.mc.otm.core.math.rotationTwo
import ru.dbotthepony.mc.otm.registry.MBlockEntities
import ru.dbotthepony.mc.otm.shapes.BlockShapes
import ru.dbotthepony.mc.otm.core.math.unaryMinus
import ru.dbotthepony.mc.otm.oncePre

class StorageBusBlock : RotatableMatteryBlock(), EntityBlock {
	override fun rotationFreedom(): BlockRotationFreedom {
		return BlockRotationFreedom.TWO
	}

	init {
		registerDefaultState(defaultBlockState()
			.setValue(CableBlock.CONNECTION_SOUTH, false)
			.setValue(CableBlock.CONNECTION_WEST, false)
			.setValue(CableBlock.CONNECTION_EAST, false)
			.setValue(CableBlock.CONNECTION_NORTH, false)
			.setValue(CableBlock.CONNECTION_UP, false)
			.setValue(CableBlock.CONNECTION_DOWN, false))
	}

	override fun newBlockEntity(p_153215_: BlockPos, p_153216_: BlockState): BlockEntity {
		return StorageBusBlockEntity(p_153215_, p_153216_)
	}

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

	override fun appendHoverText(
		p_49816_: ItemStack,
		p_49817_: BlockGetter?,
		p_49818_: MutableList<Component>,
		p_49819_: TooltipFlag
	) {
		super.appendHoverText(p_49816_, p_49817_, p_49818_, p_49819_)
		WorkerEnergyStorage.appendHoverText(p_49816_, p_49817_, p_49818_, p_49819_)
		MatteryPoweredBlockEntity.appendHoverText(p_49816_, p_49817_, p_49818_, p_49819_)
	}

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

		builder.add(
			CableBlock.CONNECTION_SOUTH,
			CableBlock.CONNECTION_WEST,
			CableBlock.CONNECTION_EAST,
			CableBlock.CONNECTION_NORTH,
			CableBlock.CONNECTION_UP,
			CableBlock.CONNECTION_DOWN
		)
	}

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

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

	private val shapes = getShapeForEachState {
		val shapes = StorageCableBlock.getShapeFor(it)
		var finalShape = shapes[0]

		for (i in 1 until shapes.size) {
			finalShape = Shapes.joinUnoptimized(finalShape, shapes[i], BooleanOp.OR)
		}

		finalShape = Shapes.joinUnoptimized(finalShape, BlockShapes.STORAGE_BUS.rotateFromNorth(it[rotationProperty]).computeShape(), BooleanOp.OR)

		return@getShapeForEachState finalShape
	}

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

	@Suppress("OVERRIDE_DEPRECATION")
	override fun neighborChanged(
		state: BlockState,
		level: Level,
		pos: BlockPos,
		neighbour: Block,
		neighbourPos: BlockPos,
		movedByPiston: Boolean
	) {
		super.neighborChanged(state, level, pos, neighbour, neighbourPos, movedByPiston)

		if (!level.isClientSide) {
			val tile = level.getBlockEntity(pos)

			if (tile is StorageBusBlockEntity) {
				level.oncePre {
					tile.checkSurroundings()
				}
			}
		}
	}
}
