package net.mcreator.gts.procedures;

import net.minecraft.world.phys.Vec3;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.Level;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.sounds.SoundSource;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.commands.arguments.EntityAnchorArgument;

import net.mcreator.gts.GtsMod;

public class CandyTamingProcedure {
	public static void execute(LevelAccessor world, double x, double y, double z, Entity entity, Entity sourceentity) {
		if (entity == null || sourceentity == null)
			return;
		sourceentity.getPersistentData().putBoolean("AttackCheck", true);
		sourceentity.setDeltaMovement(new Vec3(0, 0, 0));
		if (sourceentity instanceof Mob _mobSetNoAi) {
			_mobSetNoAi.setNoAi(true);
		}
		sourceentity.lookAt(EntityAnchorArgument.Anchor.EYES, new Vec3((entity.getX()), (entity.getY()), (entity.getZ())));
		if (sourceentity.getXRot() < -35) {
			{
				Entity _ent = sourceentity;
				_ent.setYRot(sourceentity.getYRot());
				_ent.setXRot(-35);
				_ent.setYBodyRot(_ent.getYRot());
				_ent.setYHeadRot(_ent.getYRot());
				_ent.yRotO = _ent.getYRot();
				_ent.xRotO = _ent.getXRot();
				if (_ent instanceof LivingEntity _entity) {
					_entity.yBodyRotO = _entity.getYRot();
					_entity.yHeadRotO = _entity.getYRot();
				}
			}
		} else if (sourceentity.getXRot() > 35) {
			{
				Entity _ent = sourceentity;
				_ent.setYRot(sourceentity.getYRot());
				_ent.setXRot(35);
				_ent.setYBodyRot(_ent.getYRot());
				_ent.setYHeadRot(_ent.getYRot());
				_ent.yRotO = _ent.getYRot();
				_ent.xRotO = _ent.getXRot();
				if (_ent instanceof LivingEntity _entity) {
					_entity.yBodyRotO = _entity.getYRot();
					_entity.yHeadRotO = _entity.getYRot();
				}
			}
		}
		GtsMod.queueServerWork(10, () -> {
			if (sourceentity.isAlive()) {
				{
					Entity _ent = sourceentity;
					_ent.setYRot(sourceentity.getYRot());
					_ent.setXRot(0);
					_ent.setYBodyRot(_ent.getYRot());
					_ent.setYHeadRot(_ent.getYRot());
					_ent.yRotO = _ent.getYRot();
					_ent.xRotO = _ent.getXRot();
					if (_ent instanceof LivingEntity _entity) {
						_entity.yBodyRotO = _entity.getYRot();
						_entity.yHeadRotO = _entity.getYRot();
					}
				}
				if (entity instanceof Player _player) {
					ItemStack _stktoremove = new ItemStack(Items.CAKE);
					_player.getInventory().clearOrCountMatchingItems(p -> _stktoremove.getItem() == p.getItem(), 1, _player.inventoryMenu.getCraftSlots());
				}
			}
		});
		GtsMod.queueServerWork(20, () -> {
			if (sourceentity.isAlive()) {
				if (world instanceof Level _level) {
					if (!_level.isClientSide()) {
						_level.playSound(null, BlockPos.containing(x, y, z), BuiltInRegistries.SOUND_EVENT.get(ResourceLocation.parse("gts:candy.swallow")), SoundSource.HOSTILE,
								(float) (sourceentity instanceof LivingEntity _livingEntity19 && _livingEntity19.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity19.getAttribute(Attributes.SCALE).getBaseValue() : 0), 1);
					} else {
						_level.playLocalSound(x, y, z, BuiltInRegistries.SOUND_EVENT.get(ResourceLocation.parse("gts:candy.swallow")), SoundSource.HOSTILE,
								(float) (sourceentity instanceof LivingEntity _livingEntity19 && _livingEntity19.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity19.getAttribute(Attributes.SCALE).getBaseValue() : 0), 1, false);
					}
				}
				if (world instanceof ServerLevel _level)
					_level.sendParticles(ParticleTypes.HEART, (sourceentity.getX()),
							(sourceentity.getY() + (sourceentity instanceof LivingEntity _livingEntity23 && _livingEntity23.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity23.getAttribute(Attributes.SCALE).getBaseValue() : 0) * 2),
							(sourceentity.getZ()),
							(int) ((sourceentity instanceof LivingEntity _livingEntity28 && _livingEntity28.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity28.getAttribute(Attributes.SCALE).getBaseValue() : 0) * 10),
							((sourceentity instanceof LivingEntity _livingEntity25 && _livingEntity25.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity25.getAttribute(Attributes.SCALE).getBaseValue() : 0) / 2),
							((sourceentity instanceof LivingEntity _livingEntity26 && _livingEntity26.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity26.getAttribute(Attributes.SCALE).getBaseValue() : 0) / 2),
							((sourceentity instanceof LivingEntity _livingEntity27 && _livingEntity27.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity27.getAttribute(Attributes.SCALE).getBaseValue() : 0) / 2), 0);
			}
		});
		GtsMod.queueServerWork(45, () -> {
			if (sourceentity.isAlive()) {
				TamedCandySpawnProcedure.execute(world, sourceentity.getX(), sourceentity.getY(), sourceentity.getZ(), entity, sourceentity instanceof LivingEntity _livEnt ? _livEnt.getHealth() : -1,
						sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMaxHealth() : -1,
						sourceentity instanceof LivingEntity _livingEntity34 && _livingEntity34.getAttributes().hasAttribute(Attributes.MOVEMENT_SPEED) ? _livingEntity34.getAttribute(Attributes.MOVEMENT_SPEED).getBaseValue() : 0,
						sourceentity.getXRot(),
						sourceentity instanceof LivingEntity _livingEntity37 && _livingEntity37.getAttributes().hasAttribute(Attributes.SAFE_FALL_DISTANCE) ? _livingEntity37.getAttribute(Attributes.SAFE_FALL_DISTANCE).getBaseValue() : 0,
						sourceentity instanceof LivingEntity _livingEntity38 && _livingEntity38.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity38.getAttribute(Attributes.SCALE).getBaseValue() : 0, sourceentity.getYRot());
				if (!sourceentity.level().isClientSide())
					sourceentity.discard();
			}
		});
	}
}
