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

import net.minecraft.core.BlockPos
import net.minecraft.network.chat.Component
import net.minecraft.world.InteractionHand
import net.minecraft.world.InteractionResult
import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.TooltipFlag
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.BlockHitResult
import net.minecraft.world.phys.shapes.CollisionContext
import net.minecraft.world.phys.shapes.VoxelShape
import ru.dbotthepony.mc.otm.block.MatteryBlock
import ru.dbotthepony.mc.otm.block.entity.tech.AndroidStationBlockEntity
import ru.dbotthepony.mc.otm.block.entity.MatteryPoweredBlockEntity
import ru.dbotthepony.mc.otm.block.entity.WorkerState
import ru.dbotthepony.mc.otm.capability.MatteryCapability
import ru.dbotthepony.mc.otm.capability.energy.WorkerEnergyStorage
import ru.dbotthepony.mc.otm.core.orNull
import ru.dbotthepony.mc.otm.registry.MBlockEntities
import ru.dbotthepony.mc.otm.shapes.BlockShapes

class AndroidStationBlock : MatteryBlock(), EntityBlock {
	override fun use(
		blockState: BlockState,
		level: Level,
		blockPos: BlockPos,
		ply: Player,
		hand: InteractionHand,
		blockHitResult: BlockHitResult
	): InteractionResult {
		val cap = ply.getCapability(MatteryCapability.MATTERY_PLAYER).orNull() ?: return InteractionResult.FAIL

		if (!cap.isAndroid)
			return InteractionResult.FAIL

		return super.use(blockState, level, blockPos, ply, hand, blockHitResult)
	}

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

	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 getShape(
		p_151964_: BlockState,
		p_151965_: BlockGetter,
		p_151966_: BlockPos,
		p_151967_: CollisionContext
	): VoxelShape {
		return SHAPE
	}

	override fun newBlockEntity(pos: BlockPos, state: BlockState): BlockEntity {
		return AndroidStationBlockEntity(pos, state)
	}

	override fun <T : BlockEntity?> getTicker(
		level: Level,
		state: BlockState,
		type: BlockEntityType<T>
	): BlockEntityTicker<T>? {
		if (level.isClientSide || type !== MBlockEntities.ANDROID_STATION)
			return null

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

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