package com.acethewildfire.acesbs.mixin;

import com.acethewildfire.acesbs.component.ModDataComponentTypes;
import com.acethewildfire.acesbs.item.ModItems;
import com.acethewildfire.acesbs.item.custom.SovsPlateCarrierArmorItem;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Objects;

@Mixin(LivingEntity.class)
public abstract class LivingEntityMixin {

    @ModifyVariable(
            method = "damage",
            at = @At("HEAD"),
            argsOnly = true
    )
    private float modifyDamage(float amount) {

        LivingEntity entity = (LivingEntity)(Object)this;

        if (entity instanceof PlayerEntity player) {

            ItemStack chest = player.getEquippedStack(EquipmentSlot.CHEST);

            if (chest.getItem() instanceof SovsPlateCarrierArmorItem) {

                float multiplier =
                        SovsPlateCarrierArmorItem.getDamageMultiplier(chest);

                amount *= multiplier;
            }
        }

        return amount;
    }

    @Inject(method = "takeKnockback", at = @At("HEAD"), cancellable = true)
    private void cancelKnockback(double strength, double x, double z, CallbackInfo ci) {
        if ((Object)this instanceof PlayerEntity player) {
            for (ItemStack stack : player.getInventory().getMainStacks()) {
                if (stack.isOf(ModItems.COMMAND_CORE)) {
                    if (Boolean.TRUE.equals(stack.get(ModDataComponentTypes.COMMAND_ACTIVE))){
                        if (Objects.equals(stack.get(ModDataComponentTypes.COMMAND), "Immovable")){
                            ci.cancel();
                        }
                    };
                }
            }
        }
    }
}
