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.level.LevelAccessor;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.ai.attributes.Attributes;
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.server.level.ServerLevel;
import net.minecraft.core.BlockPos;

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

import javax.annotation.Nullable;

@EventBusSubscriber
public class AraMeleeAttackProcedure {
	@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 TargetHeight = 0;
		if (sourceentity instanceof AraEntity) {
			if (immediatesourceentity instanceof GiantessSpitEntity) {
				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();
				if (((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getHealth() : -1) / (sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMaxHealth() : -1) <= 0.5
						&& (entity instanceof LivingEntity _livEnt ? _livEnt.getHealth() : -1) <= 4
								* (sourceentity instanceof LivingEntity _livingEntity11 && _livingEntity11.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity11.getAttribute(Attributes.SCALE).getBaseValue() : 0)
						|| GtsModVariables.MapVariables.get(world).DisableEatDependsOnHealth) && !entity.isPassenger() && !GtsModVariables.MapVariables.get(world).DisableAraEat
						&& (sourceentity instanceof LivingEntity _livingEntity13 && _livingEntity13.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity13.getAttribute(Attributes.SCALE).getBaseValue() : 0) > 2
						&& Mth.nextInt(RandomSource.create(), 0, (int) Math.round(((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getHealth() : -1) / (sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMaxHealth() : -1)) * 10)) == 0
						&& true && !(sourceentity instanceof LivingEntity _livEnt17 && _livEnt17.hasEffect(GtsModMobEffects.GTS_TIMER)) && !entity.getPersistentData().getBoolean("BeingGrabbed")
						&& (sourceentity instanceof LivingEntity _livingEntity19 && _livingEntity19.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity19.getAttribute(Attributes.SCALE).getBaseValue() : 0) * 2 >= TargetHeight) {
					if ((sourceentity instanceof LivingEntity _livingEntity20 && _livingEntity20.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity20.getAttribute(Attributes.SCALE).getBaseValue() : 0) > TargetHeight) {
						if (Mth.nextInt(RandomSource.create(), 1, 2) == 1) {
							AraLowerVore1Procedure.execute(world, x, y, z, entity, sourceentity);
						} else {
							AraLowerVore2Procedure.execute(world, x, y, z, entity, sourceentity);
						}
					} else {
						AraWaistVoreProcedure.execute(world, x, y, z, entity, sourceentity);
					}
				} else if (entity.isShiftKeyDown() && (sourceentity instanceof LivingEntity _livEnt ? _livEnt.getHealth() : -1) / (sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMaxHealth() : -1) > 0.5
						&& !(world instanceof ServerLevel _level25 && _level25.isRaided(BlockPos.containing(x, y, z))) && !GtsModVariables.MapVariables.get(world).DisableSubmission
						&& (sourceentity instanceof LivingEntity _livingEntity26 && _livingEntity26.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity26.getAttribute(Attributes.SCALE).getBaseValue() : 0) > 2
						&& !(sourceentity instanceof LivingEntity _livEnt27 && _livEnt27.hasEffect(GtsModMobEffects.GTS_TIMER)) && !entity.getPersistentData().getBoolean("BeingGrabbed")
						&& ((entity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getItem() == Items.AMETHYST_SHARD
								|| (entity instanceof LivingEntity _livEnt ? _livEnt.getOffhandItem() : ItemStack.EMPTY).getItem() == Items.AMETHYST_SHARD)
						&& sourceentity.getPersistentData().getBoolean("CanBeFriend") && Mth.nextInt(RandomSource.create(), 1, 3) == 1) {
					GtsMod.LOGGER.info("");
				} else {
					MeleeAttackChoice = Mth.nextInt(RandomSource.create(), 1, 6);
					if ((sourceentity instanceof LivingEntity _livingEntity36 && _livingEntity36.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity36.getAttribute(Attributes.SCALE).getBaseValue() : 0) > TargetHeight) {
						if (MeleeAttackChoice < 3 && !GtsModVariables.MapVariables.get(world).DisableAraStep) {
							AraSteppingProcedure.execute(world, entity, sourceentity);
						} else if ((MeleeAttackChoice == 3 || MeleeAttackChoice == 4) && !GtsModVariables.MapVariables.get(world).DisableAraTailSwipe) {
							AraKnockbackProcedure.execute(world, x, y, z, entity, sourceentity);
						} else if (!entity.getPersistentData().getBoolean("BeingGrabbed") && !(sourceentity instanceof LivingEntity _livEnt38 && _livEnt38.hasEffect(GtsModMobEffects.GTS_TIMER))
								&& (sourceentity instanceof LivingEntity _livingEntity39 && _livingEntity39.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity39.getAttribute(Attributes.SCALE).getBaseValue() : 0) >= 2
								&& MeleeAttackChoice == 5 && !GtsModVariables.MapVariables.get(world).DisableAraTailSqueeze) {
							AraGrab1Procedure.execute(world, entity, sourceentity);
						} else if (!entity.getPersistentData().getBoolean("BeingGrabbed") && !(sourceentity instanceof LivingEntity _livEnt41 && _livEnt41.hasEffect(GtsModMobEffects.GTS_TIMER))
								&& (sourceentity instanceof LivingEntity _livingEntity42 && _livingEntity42.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity42.getAttribute(Attributes.SCALE).getBaseValue() : 0) >= 2
								&& MeleeAttackChoice == 6 && !GtsModVariables.MapVariables.get(world).DisableAraLowGrab) {
							AraGrab2Procedure.execute(world, entity, sourceentity);
						} else if (!GtsModVariables.MapVariables.get(world).DisableAraStep) {
							AraSteppingProcedure.execute(world, entity, sourceentity);
						} else if (!GtsModVariables.MapVariables.get(world).DisableAraTailSwipe) {
							AraKnockbackProcedure.execute(world, x, y, z, entity, sourceentity);
						} else if (!entity.getPersistentData().getBoolean("BeingGrabbed") && !(sourceentity instanceof LivingEntity _livEnt44 && _livEnt44.hasEffect(GtsModMobEffects.GTS_TIMER))
								&& (sourceentity instanceof LivingEntity _livingEntity45 && _livingEntity45.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity45.getAttribute(Attributes.SCALE).getBaseValue() : 0) >= 2
								&& !GtsModVariables.MapVariables.get(world).DisableAraTailSqueeze) {
							AraGrab1Procedure.execute(world, entity, sourceentity);
						} else if (!entity.getPersistentData().getBoolean("BeingGrabbed") && !(sourceentity instanceof LivingEntity _livEnt47 && _livEnt47.hasEffect(GtsModMobEffects.GTS_TIMER))
								&& (sourceentity instanceof LivingEntity _livingEntity48 && _livingEntity48.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity48.getAttribute(Attributes.SCALE).getBaseValue() : 0) >= 2
								&& !GtsModVariables.MapVariables.get(world).DisableAraLowGrab) {
							AraGrab2Procedure.execute(world, entity, sourceentity);
						}
					} else if ((sourceentity instanceof LivingEntity _livingEntity49 && _livingEntity49.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity49.getAttribute(Attributes.SCALE).getBaseValue() : 0) <= TargetHeight
							&& (sourceentity instanceof LivingEntity _livingEntity50 && _livingEntity50.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity50.getAttribute(Attributes.SCALE).getBaseValue() : 0) * 2 >= TargetHeight) {
						if ((MeleeAttackChoice == 1 || MeleeAttackChoice == 2) && !GtsModVariables.MapVariables.get(world).DisableAraTailWaistSlap) {
							AraMiddleKnockbackProcedure.execute(world, x, y, z, entity, sourceentity);
						} else if (!entity.getPersistentData().getBoolean("BeingGrabbed") && !(sourceentity instanceof LivingEntity _livEnt52 && _livEnt52.hasEffect(GtsModMobEffects.GTS_TIMER))
								&& (sourceentity instanceof LivingEntity _livingEntity53 && _livingEntity53.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity53.getAttribute(Attributes.SCALE).getBaseValue() : 0) >= 2
								&& (MeleeAttackChoice == 3 || MeleeAttackChoice == 4) && !GtsModVariables.MapVariables.get(world).DisableAraWaistGrab) {
							AraGrab3Procedure.execute(world, entity, sourceentity);
						} else if (!entity.getPersistentData().getBoolean("BeingGrabbed") && !(sourceentity instanceof LivingEntity _livEnt55 && _livEnt55.hasEffect(GtsModMobEffects.GTS_TIMER))
								&& (sourceentity instanceof LivingEntity _livingEntity56 && _livingEntity56.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity56.getAttribute(Attributes.SCALE).getBaseValue() : 0) >= 2
								&& (MeleeAttackChoice == 5 || MeleeAttackChoice == 6) && !GtsModVariables.MapVariables.get(world).DisableAraChomp) {
							AraBiteProcedure.execute(world, entity, sourceentity);
						} else if (!GtsModVariables.MapVariables.get(world).DisableAraTailWaistSlap) {
							AraMiddleKnockbackProcedure.execute(world, x, y, z, entity, sourceentity);
						} else if (!entity.getPersistentData().getBoolean("BeingGrabbed") && !(sourceentity instanceof LivingEntity _livEnt58 && _livEnt58.hasEffect(GtsModMobEffects.GTS_TIMER))
								&& (sourceentity instanceof LivingEntity _livingEntity59 && _livingEntity59.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity59.getAttribute(Attributes.SCALE).getBaseValue() : 0) >= 2
								&& !GtsModVariables.MapVariables.get(world).DisableAraWaistGrab) {
							AraGrab3Procedure.execute(world, entity, sourceentity);
						} else if (!entity.getPersistentData().getBoolean("BeingGrabbed") && !(sourceentity instanceof LivingEntity _livEnt61 && _livEnt61.hasEffect(GtsModMobEffects.GTS_TIMER))
								&& (sourceentity instanceof LivingEntity _livingEntity62 && _livingEntity62.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity62.getAttribute(Attributes.SCALE).getBaseValue() : 0) >= 2
								&& !GtsModVariables.MapVariables.get(world).DisableAraChomp) {
							AraBiteProcedure.execute(world, entity, sourceentity);
						}
					} else if ((sourceentity instanceof LivingEntity _livingEntity63 && _livingEntity63.getAttributes().hasAttribute(Attributes.SCALE) ? _livingEntity63.getAttribute(Attributes.SCALE).getBaseValue() : 0) * 2 < TargetHeight) {
						if (!GtsModVariables.MapVariables.get(world).DisableAraAboveAttack) {
							AraUpperHitProcedure.execute(world, x, y, z, entity, sourceentity);
						}
					}
				}
			}
		}
	}
}
