package gvclib.item;

import gvclib.item.ItemGunBase;
import gvclib.item.gun.AttachmentBase_LoadStatus;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
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.common.extensions.IForgeItem;
import objmodel.IModelCustom;

import java.io.BufferedReader;

public class ItemAttachment extends Item implements Vanishable, IForgeItem {

    public ItemAttachment(Item.Properties durability) {
        super(durability);
    }

    public int id = 0;
    public String obj_model_string = "gvclib:textures/model/ar.png";
    public IModelCustom obj_model = null;//AdvancedModelLoader.loadModel(new ResourceLocation("gvclib:textures/model/ar.obj"));
    public String obj_tex = "gvclib:textures/model/ar.png";
    public boolean objtrue = false;
    public boolean zoomrender = true;
    public boolean zoomrendertex = false;
    public String ads_tex = "gvclib:textures/misc/scope.png";
    public float zoom = 1.0F;
    public float x = 0;
    public float y = 0;
    public float z = 0;

    public float eye_relief = - 2.0F;

    public float xr = 0;
    public float yr = 0;
    public float zr = 0;
    public float[] colorlevel = new float[1024];
    public int light_kazu = 1;

    public double recoil = 0.8F;
    public double recoil_ads = 0.6F;
    public float bure = 0.8F;
    public float bure_ads = 0.4F;

    public int p_id = 0;
    public int p_level = 0;
    public int p_time = 0;

    public float exlevel = 0F;
    public float power = 1F;
    public int bulletid = 0;
    /**使用用途不*/
    public boolean notpelletbase = false;
    public boolean pellet_priority = true;
    public int pellet = 1;

    //19/1/14
    public boolean rendering_light = true;

    //19/2/16
    public boolean grip_gripping_point = false;
    public float grip_gripping_point_x = 0;
    public float grip_gripping_point_y = 0;
    public float grip_gripping_point_z = 0;

    public String bullet_name;

    public String designated_attachment_name = null;


    public String information1 = null;
    public String information2 = null;
    public String information3 = null;

    //public TextFormatting information1_color = TextFormatting.WHITE;
    //public TextFormatting information2_color = TextFormatting.WHITE;
    //public TextFormatting information3_color = TextFormatting.WHITE;

    public ItemAttachment LoadingEvent(BufferedReader br, String domain, int am_id) {

        AttachmentBase_LoadStatus.load(this, br, domain, am_id);


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

        ItemStack itemstack = playerIn.getItemInHand(p_40674_);
        if(!itemstack.isEmpty()){
            if(!playerIn.getOffhandItem().isEmpty() && playerIn.getOffhandItem().getItem() instanceof ItemGunBase) {
                ItemGunBase gun = (ItemGunBase) playerIn.getOffhandItem().getItem();
                /*CompoundTag compoundtag = playerIn.getOffhandItem().getOrCreateTag();
                if (!compoundtag.contains("Items")) {
                    compoundtag.put("Items", new ListTag());
                } else {
                    if (playerIn instanceof ServerPlayer) {
                        ListTag listtag = compoundtag.getList("Items", 1);

                       /* for(int i = 0; i < listtag.size(); ++i) {
                            CompoundTag compoundtag1 = listtag.getCompound(i);
                            ItemStack itemstack2 = ItemStack.of(compoundtag1);
                        }*
                        listtag.set(1, itemstack.getTag());
                    }
                }*/

        //        gun.sight = itemstack;
                CompoundTag compoundtag = playerIn.getOffhandItem().getOrCreateTag();
                if(id == 101){
                    if (!compoundtag.contains("Items_SIGHT")) {
                        compoundtag.put("Items_SIGHT", new ListTag());
                    }
                    ListTag listtag = compoundtag.getList("Items_SIGHT", 10);
                    CompoundTag compoundtag2 = new CompoundTag();
                    itemstack.save(compoundtag2);
                    listtag.add(0, (Tag)compoundtag2);
                }
                if(id == 102){
                    if (!compoundtag.contains("Items_LIGHT")) {
                        compoundtag.put("Items_LIGHT", new ListTag());
                    }
                    ListTag listtag = compoundtag.getList("Items_LIGHT", 10);
                    CompoundTag compoundtag2 = new CompoundTag();
                    itemstack.save(compoundtag2);
                    listtag.add(0, (Tag)compoundtag2);
                }
                if(id == 8){
                    if (!compoundtag.contains("Items_SUPP")) {
                        compoundtag.put("Items_SUPP", new ListTag());
                    }
                    ListTag listtag = compoundtag.getList("Items_SUPP", 10);
                    CompoundTag compoundtag2 = new CompoundTag();
                    itemstack.save(compoundtag2);
                    listtag.add(0, (Tag)compoundtag2);
                }
                if(id == 9){
                    if (!compoundtag.contains("Items_GRIP")) {
                        compoundtag.put("Items_GRIP", new ListTag());
                    }
                    ListTag listtag = compoundtag.getList("Items_GRIP", 10);
                    CompoundTag compoundtag2 = new CompoundTag();
                    itemstack.save(compoundtag2);
                    listtag.add(0, (Tag)compoundtag2);
                }

            }
        }
        playerIn.startUsingItem(p_40674_);
        return InteractionResultHolder.sidedSuccess(itemstack, worldIn.isClientSide());
    }
}
