package net.minecraft.world.entity.animal.squid;

import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.util.RandomSource;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.AgeableMob;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.ServerLevelAccessor;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
import org.jspecify.annotations.Nullable;

public class GlowSquid extends Squid {
	private static final EntityDataAccessor<Integer> DATA_DARK_TICKS_REMAINING = SynchedEntityData.defineId(GlowSquid.class, EntityDataSerializers.INT);
	private static final int DEFAULT_DARK_TICKS_REMAINING = 0;

	public GlowSquid(final EntityType<? extends GlowSquid> type, final Level level) {
		super(type, level);
	}

	@Override
	protected ParticleOptions getInkParticle() {
		return ParticleTypes.GLOW_SQUID_INK;
	}

	@Override
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
		super.defineSynchedData(entityData);
		entityData.define(DATA_DARK_TICKS_REMAINING, 0);
	}

	@Override
	public @Nullable AgeableMob getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
		return EntityTypes.GLOW_SQUID.create(level, EntitySpawnReason.BREEDING);
	}

	@Override
	protected SoundEvent getSquirtSound() {
		return SoundEvents.GLOW_SQUID_SQUIRT;
	}

	@Override
	protected SoundEvent getAmbientSound() {
		return SoundEvents.GLOW_SQUID_AMBIENT;
	}

	@Override
	protected SoundEvent getHurtSound(final DamageSource source) {
		return SoundEvents.GLOW_SQUID_HURT;
	}

	@Override
	protected SoundEvent getDeathSound() {
		return SoundEvents.GLOW_SQUID_DEATH;
	}

	@Override
	protected void addAdditionalSaveData(final ValueOutput output) {
		super.addAdditionalSaveData(output);
		output.putInt("DarkTicksRemaining", this.getDarkTicksRemaining());
	}

	@Override
	protected void readAdditionalSaveData(final ValueInput input) {
		super.readAdditionalSaveData(input);
		this.setDarkTicks(input.getIntOr("DarkTicksRemaining", 0));
	}

	@Override
	public void aiStep() {
		super.aiStep();
		int darkTicks = this.getDarkTicksRemaining();
		if (darkTicks > 0) {
			this.setDarkTicks(darkTicks - 1);
		}

		this.level().addParticle(ParticleTypes.GLOW, this.getRandomX(0.6), this.getRandomY(), this.getRandomZ(0.6), 0.0, 0.0, 0.0);
	}

	@Override
	public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
		boolean hurt = super.hurtServer(level, source, damage);
		if (hurt) {
			this.setDarkTicks(100);
		}

		return hurt;
	}

	private void setDarkTicks(final int ticks) {
		this.entityData.set(DATA_DARK_TICKS_REMAINING, ticks);
	}

	public int getDarkTicksRemaining() {
		return this.entityData.get(DATA_DARK_TICKS_REMAINING);
	}

	public static boolean checkGlowSquidSpawnRules(
		final EntityType<? extends LivingEntity> type,
		final ServerLevelAccessor level,
		final EntitySpawnReason spawnReason,
		final BlockPos pos,
		final RandomSource random
	) {
		return pos.getY() <= level.getSeaLevel() - 33 && level.getRawBrightness(pos, 0) == 0 && level.getBlockState(pos).is(Blocks.WATER);
	}
}
