package net.mcreator.gts.procedures;

import net.neoforged.neoforge.event.entity.living.LivingIncomingDamageEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.bus.api.ICancellableEvent;
import net.neoforged.bus.api.Event;

import net.minecraft.world.phys.Vec3;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.animal.IronGolem;
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.util.RandomSource;
import net.minecraft.util.Mth;
import net.minecraft.tags.TagKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.registries.Registries;
import net.minecraft.commands.arguments.EntityAnchorArgument;

import net.mcreator.gts.network.GtsModVariables;
import net.mcreator.gts.init.GtsModMobEffects;
import net.mcreator.gts.entity.TamedTokiEntity;
import net.mcreator.gts.entity.GiantessSpitEntity;

import javax.annotation.Nullable;

@EventBusSubscriber
public class TamedTokiMeleeAttackProcedure {
	@SubscribeEvent
	public static void onEntityAttacked(LivingIncomingDamageEvent event) {
		if (event.getEntity() != null) {
			execute(event, event.getEntity().level(), event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ(), event.getEntity(), event.getSource().getDirectEntity(), event.getSource().getEntity());
		}
	}

	public static void execute(LevelAccessor world, double x, double y, double z, Entity entity, Entity immediatesourceentity, Entity sourceentity) {
		execute(null, world, x, y, z, entity, immediatesourceentity, sourceentity);
	}

	private static void execute(@Nullable Event event, LevelAccessor world, double x, double y, double z, Entity entity, Entity immediatesourceentity, Entity sourceentity) {
		if (entity == null || immediatesourceentity == null || sourceentity == null)
			return;
		double MeleeAttackChoice = 0;
		double VoreChance = 0;
		double TargetHeight = 0;
		if (sourceentity instanceof TamedTokiEntity) {
			if (immediatesourceentity instanceof GiantessSpitEntity || entity.getType().is(TagKey.create(Registries.ENTITY_TYPE, ResourceLocation.parse("gts:giantess")))) {
				if (event instanceof ICancellableEvent _cancellable) {
					_cancellable.setCanceled(true);
				}
			} else if (!sourceentity.getPersistentData().getBoolean("AttackCheck")) {
				if (event instanceof ICancellableEvent _cancellable) {
					_cancellable.setCanceled(true);
				}
				sourceentity.getPersistentData().putDouble("RangeAttack", 0);
				sourceentity.getPersistentData().putDouble("BreachAttack", 0);
				entity.getPersistentData().putDouble("TargetRangeAttack", 0);
				TargetHeight = entity.getY() - sourceentity.getY();
				sourceentity.lookAt(EntityAnchorArgument.Anchor.EYES, new Vec3((entity.getX()), (entity.getY()), (entity.getZ())));
				if (!(sourceentity instanceof LivingEntity _livEnt13 && _livEnt13.hasEffect(GtsModMobEffects.GTS_TIMER)) && !entity.getPersistentData().getBoolean("BeingGrabbed") && !GtsModVariables.MapVariables.get(world).DisableVore
						&& (sourceentity instanceof LivingEntity _livingEntity15 && _livingEntity15.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity15.getAttribute(Attributes.SCALE).getBaseValue() : 0) > 2
						&& ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getHealth() : -1) / (sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMaxHealth() : -1) <= 0.7
								&& (entity instanceof LivingEntity _livEnt ? _livEnt.getMaxHealth() : -1) <= 50 || GtsModVariables.MapVariables.get(world).DisableEatDependsOnHealth)
						&& !(entity instanceof Player)
						&& (Mth.nextInt(RandomSource.create(), 0, (int) Math.round(((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getHealth() : -1) / (sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMaxHealth() : -1)) * 10)) == 0
								|| (entity instanceof Mob _mobEnt ? (Entity) _mobEnt.getTarget() : null) == null)
						&& true) {
					if ((sourceentity instanceof LivingEntity _livingEntity25 && _livingEntity25.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity25.getAttribute(Attributes.SCALE).getBaseValue() : 0) > TargetHeight) {
						if (sourceentity instanceof TamedTokiEntity _datEntSetS)
							_datEntSetS.getEntityData().set(TamedTokiEntity.DATA_AnimationDecision, "Toki.LowerVoreAttack");
					} else if ((sourceentity instanceof LivingEntity _livingEntity27 && _livingEntity27.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity27.getAttribute(Attributes.SCALE).getBaseValue() : 0) <= TargetHeight
							&& (sourceentity instanceof LivingEntity _livingEntity28 && _livingEntity28.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity28.getAttribute(Attributes.SCALE).getBaseValue() : 0) * 2 >= TargetHeight) {
						if (sourceentity instanceof TamedTokiEntity _datEntSetS)
							_datEntSetS.getEntityData().set(TamedTokiEntity.DATA_AnimationDecision, "Toki.MiddleVoreAttack");
					} else if ((sourceentity instanceof LivingEntity _livingEntity30 && _livingEntity30.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity30.getAttribute(Attributes.SCALE).getBaseValue() : 0) * 2 < TargetHeight) {
						if (sourceentity instanceof TamedTokiEntity _datEntSetS)
							_datEntSetS.getEntityData().set(TamedTokiEntity.DATA_AnimationDecision, "Toki.UpperVoreAttack");
					}
					TamedTokiVoreAttackProcedure.execute(world, x, y, z, entity, sourceentity);
				} else {
					MeleeAttackChoice = Mth.nextInt(RandomSource.create(), 1, 6);
					if ((sourceentity instanceof LivingEntity _livingEntity33 && _livingEntity33.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity33.getAttribute(Attributes.SCALE).getBaseValue() : 0) > TargetHeight) {
						if (MeleeAttackChoice < 3 && !GtsModVariables.MapVariables.get(world).DisableStep) {
							TamedTokiSteppingProcedure.execute(world, sourceentity);
						} else if (MeleeAttackChoice == 3 && !(entity instanceof IronGolem) && !GtsModVariables.MapVariables.get(world).DisableSit) {
							TamedTokiSittingProcedure.execute(world, sourceentity);
						} else if (MeleeAttackChoice >= 5 && !GtsModVariables.MapVariables.get(world).DisableKick) {
							TamedTokiKickProcedure.execute(world, sourceentity);
						} else if (!GtsModVariables.MapVariables.get(world).DisableStep) {
							TamedTokiSteppingProcedure.execute(world, sourceentity);
						} else if (!GtsModVariables.MapVariables.get(world).DisableKick) {
							TamedTokiKickProcedure.execute(world, sourceentity);
						} else if (!GtsModVariables.MapVariables.get(world).DisableSit) {
							TamedTokiSittingProcedure.execute(world, sourceentity);
						}
					} else if ((sourceentity instanceof LivingEntity _livingEntity35 && _livingEntity35.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity35.getAttribute(Attributes.SCALE).getBaseValue() : 0) <= TargetHeight
							&& (sourceentity instanceof LivingEntity _livingEntity36 && _livingEntity36.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity36.getAttribute(Attributes.SCALE).getBaseValue() : 0) * 2 >= TargetHeight) {
						if (MeleeAttackChoice <= 2 && !GtsModVariables.MapVariables.get(world).DisableGunSlap) {
							TamedTokiGunMeleeProcedure.execute(world, x, y, z, sourceentity);
						} else if (MeleeAttackChoice > 2 && MeleeAttackChoice <= 4
								&& (sourceentity instanceof LivingEntity _livingEntity37 && _livingEntity37.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity37.getAttribute(Attributes.SCALE).getBaseValue() : 0) >= 2
								&& !entity.getPersistentData().getBoolean("BeingGrabbed") && !(sourceentity instanceof LivingEntity _livEnt39 && _livEnt39.hasEffect(GtsModMobEffects.GTS_TIMER))
								&& !GtsModVariables.MapVariables.get(world).DisableThrow) {
							TamedTokiThrowProcedure.execute(world, entity, sourceentity);
						} else if (MeleeAttackChoice >= 5 && !GtsModVariables.MapVariables.get(world).DisableGunSmash) {
							TamedTokiGunSmashProcedure.execute(world, x, y, z, sourceentity);
						} else if (!GtsModVariables.MapVariables.get(world).DisableGunSlap) {
							TamedTokiGunMeleeProcedure.execute(world, x, y, z, sourceentity);
						} else if (!GtsModVariables.MapVariables.get(world).DisableGunSmash) {
							TamedTokiGunSmashProcedure.execute(world, x, y, z, sourceentity);
						} else if ((sourceentity instanceof LivingEntity _livingEntity40 && _livingEntity40.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity40.getAttribute(Attributes.SCALE).getBaseValue() : 0) >= 2
								&& !entity.getPersistentData().getBoolean("BeingGrabbed") && !(sourceentity instanceof LivingEntity _livEnt42 && _livEnt42.hasEffect(GtsModMobEffects.GTS_TIMER))
								&& !GtsModVariables.MapVariables.get(world).DisableThrow) {
							TamedTokiThrowProcedure.execute(world, entity, sourceentity);
						}
					} else if ((sourceentity instanceof LivingEntity _livingEntity43 && _livingEntity43.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity43.getAttribute(Attributes.SCALE).getBaseValue() : 0) * 2 < TargetHeight) {
						if (!GtsModVariables.MapVariables.get(world).DisableGunSmash) {
							TamedTokiGunSmashProcedure.execute(world, x, y, z, sourceentity);
						}
					}
				}
			}
		}
	}
}
