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;
import net.minecraftforge.fml.loading.FMLLoader;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ItemGun_SR extends ItemGunBase implements Vanishable {



    public ItemGun_SR(Item.Properties builder) {
        super(builder);
        /*try {
                String str;
                while ((str = br.readLine()) != null) { // 1行ずつ読み込む
                    // System.out.println(str);
                    String[] type = str.split(",");
                    if (type.length != 0) {// 1
                        if (type[0].equals("RemainingBullet")) {
                            builder.durability(Integer.parseInt(type[1]));
                        }
                    }
                }
                br.close(); // ファイルを閉じる
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
*/
    }

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

    /**
     * Called each tick as long the item is on a player inventory. Uses by maps to check if is on a player hand and
     * update it's contents.
     */



    public void inventoryTick(ItemStack stack, Level worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
        super.inventoryTick(stack, worldIn, entityIn, itemSlot, isSelected);

    }

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

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

        ItemStack itemstack = playerIn.getItemInHand(p_40674_);
        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.startUsingItem(p_40674_);
        return InteractionResultHolder.sidedSuccess(itemstack, worldIn.isClientSide());
    }
}

