package net.minecraft.world.level.block.entity;

import net.minecraft.SharedConstants;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.util.RandomSource;
import net.minecraft.util.Util;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.component.TypedEntityData;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.Spawner;
import net.minecraft.world.level.block.TrialSpawnerBlock;
import net.minecraft.world.level.block.entity.trialspawner.PlayerDetector;
import net.minecraft.world.level.block.entity.trialspawner.TrialSpawner;
import net.minecraft.world.level.block.entity.trialspawner.TrialSpawnerState;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;

public class TrialSpawnerBlockEntity extends BlockEntity implements TrialSpawner.StateAccessor, Spawner {
	private final TrialSpawner trialSpawner = this.createDefaultSpawner();

	public TrialSpawnerBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
		super(BlockEntityTypes.TRIAL_SPAWNER, worldPosition, blockState);
	}

	private TrialSpawner createDefaultSpawner() {
		PlayerDetector playerDetector = SharedConstants.DEBUG_TRIAL_SPAWNER_DETECTS_SHEEP_AS_PLAYERS ? PlayerDetector.SHEEP : PlayerDetector.NO_CREATIVE_PLAYERS;
		PlayerDetector.EntitySelector entitySelector = PlayerDetector.EntitySelector.SELECT_FROM_LEVEL;
		return new TrialSpawner(TrialSpawner.FullConfig.DEFAULT, this, playerDetector, entitySelector);
	}

	@Override
	protected void loadAdditional(final ValueInput input) {
		super.loadAdditional(input);
		this.trialSpawner.load(input);
		if (this.level != null) {
			this.markUpdated();
		}
	}

	@Override
	protected void saveAdditional(final ValueOutput output) {
		super.saveAdditional(output);
		this.trialSpawner.store(output);
	}

	public ClientboundBlockEntityDataPacket getUpdatePacket() {
		return ClientboundBlockEntityDataPacket.create(this);
	}

	@Override
	public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
		return this.trialSpawner.getStateData().getUpdateTag(this.getBlockState().getValue(TrialSpawnerBlock.STATE));
	}

	@Override
	public void setEntityId(final EntityType<?> type, final RandomSource random) {
		if (this.level == null) {
			Util.logAndPauseIfInIde("Expected non-null level");
		} else {
			this.trialSpawner.overrideEntityToSpawn(type, this.level);
			this.setChanged();
		}
	}

	@Override
	public void setEntityData(final TypedEntityData<EntityType<?>> entityData, final RandomSource random) {
		if (this.level == null) {
			Util.logAndPauseIfInIde("Expected non-null level");
		} else {
			this.trialSpawner.overrideEntityToSpawn(entityData, this.level);
			this.setChanged();
		}
	}

	public TrialSpawner getTrialSpawner() {
		return this.trialSpawner;
	}

	@Override
	public TrialSpawnerState getState() {
		return !this.getBlockState().hasProperty(BlockStateProperties.TRIAL_SPAWNER_STATE)
			? TrialSpawnerState.INACTIVE
			: this.getBlockState().getValue(BlockStateProperties.TRIAL_SPAWNER_STATE);
	}

	@Override
	public void setState(final Level level, final TrialSpawnerState state) {
		this.setChanged();
		level.setBlockAndUpdate(this.worldPosition, this.getBlockState().setValue(BlockStateProperties.TRIAL_SPAWNER_STATE, state));
	}

	@Override
	public void markUpdated() {
		this.setChanged();
		if (this.level != null) {
			this.level.sendBlockUpdated(this.worldPosition, this.getBlockState(), this.getBlockState(), 3);
		}
	}
}
