package gvclib.item;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Vanishable;
import net.minecraft.world.level.Level;

public class ItemGun_AR extends ItemGunBase implements Vanishable {
    public ItemGun_AR(Item.Properties builder) {
        super(builder);
    }

    public boolean fire_flag = true;
    public int fire_count = 0;

    public void inventoryTick(ItemStack stack, Level worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
        super.inventoryTick(stack, worldIn, entityIn, itemSlot, isSelected);
        boolean lflag = cycleBolt(stack);
        if(entityIn instanceof Player){
            Player player = (Player) entityIn;
            if (player.getUseItem() == stack && stack == player.getMainHandItem())
            {
                if (lflag) {
                    if (!this.isReload(stack)) {

                        FireBullet(stack, worldIn, player);
                        resetBolt(stack);
                        ItemGunBase.updateCheckinghSlot(player, stack);
                    } else {
                       ItemGunBase.updateCheckinghSlot(player, stack);
                    }
                } else {
                    ItemGunBase.updateCheckinghSlot(player, stack);
                }
            }
            ItemGunBase.updateCheckinghSlot(player, stack);
        }
    }

    public void releaseUsing(ItemStack stack, Level worldIn, LivingEntity entityLiving, int timeLeft) {
        if (entityLiving instanceof Player) {
            Player playerIn = (Player)entityLiving;
        }
    }

    @Override
    public byte getCycleCount(ItemStack pItemstack)
    {
        return (byte) (this.cycle - 1);
    }

    public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, InteractionHand p_40674_) {

        ItemStack itemstack = playerIn.getItemInHand(p_40674_);
        //if(fire_flag)
        boolean flags = false;
        if(itemstack.hasTag()) {
            CompoundTag nbt = itemstack.getTag();
            int reloadset = nbt.getInt("reloadset");
            if(reloadset > 0){
                if (!worldIn.isClientSide) {
                    for (int pe = 0; pe < (itemstack.getMaxDamage() - itemstack.getDamageValue() - 1); ++pe) {
                        playerIn.drop((new ItemStack(this.magazine)), true);
                    }
                }
                setDamage(itemstack, itemstack.getMaxDamage() - 1);
                flags = true;
            }
        }
        if(!flags){
            //     FireBullet(itemstack, worldIn, playerIn);
        }
        //playerIn.awardStat(Stats.ITEM_USED.get(this));
        playerIn.startUsingItem(p_40674_);
        return InteractionResultHolder.sidedSuccess(itemstack, worldIn.isClientSide());
    }



}

