package xyz.brassgoggledcoders.steamagerevolution.modules.armory.items.guns;

import java.util.List;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

import com.teamacronymcoders.base.items.ItemBase;

import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.*;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.*;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.GameRegistry.ObjectHolder;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import xyz.brassgoggledcoders.steamagerevolution.SteamAgeRevolution;
import xyz.brassgoggledcoders.steamagerevolution.modules.armory.items.guns.IGunPart.GunPartType;
import xyz.brassgoggledcoders.steamagerevolution.modules.armory.items.guns.IMechanism.ActionType;

public class ItemGun extends ItemBase {

	@ObjectHolder(value = SteamAgeRevolution.MODID + ":bullet")
	public static final Item bullet = null;

	public ItemGun() {
		super("gun");
	}

	@Override
	@SideOnly(Side.CLIENT)
	@ParametersAreNonnullByDefault
	public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
		tooltip.add(GunUtils.getOrCreateTagCompound(stack).getString("BARREL"));
		tooltip.add(GunUtils.getOrCreateTagCompound(stack).getString("MECHANISM"));
		tooltip.add(GunUtils.getOrCreateTagCompound(stack).getString("CHAMBER"));
		tooltip.add(GunUtils.getOrCreateTagCompound(stack).getString("STOCK"));
		if(GunUtils.getOrCreateTagCompound(stack).getBoolean("isLoaded")) {
			tooltip.add("Loaded");
		}
		else {
			tooltip.add("Unloaded");
		}
		super.addInformation(stack, worldIn, tooltip, flagIn);
	}

	@Override
	public void onCreated(ItemStack stack, World worldIn, EntityPlayer playerIn) {
		GunUtils.getOrCreateTagCompound(stack);
	}

	@Override
	public EnumAction getItemUseAction(ItemStack stack) {
		return EnumAction.BOW;
	}

	@Override
	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
		ItemStack stack = playerIn.getHeldItem(handIn);

		if(!GunUtils.getOrCreateTagCompound(stack).getBoolean("isLoaded")) {
			playerIn.setActiveHand(handIn);
			return new ActionResult<ItemStack>(EnumActionResult.PASS, stack);
		}
		else {
			for(int i = 0; i < 4; i++) { // TODO Don't hardcode number of parts
				GunUtils.getPartFromGun(stack, GunPartType.values()[i]).onItemRightClick(worldIn, playerIn, handIn);
			}
		}

		return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, stack);
	}

	@Override
	public void onUsingTick(ItemStack stack, EntityLivingBase entityLiving, int count) {
		for(int i = 0; i < 4; i++) { // TODO Don't hardcode number of parts
			GunUtils.getPartFromGun(stack, GunPartType.values()[i]).onUsingTick(stack, entityLiving, count);
		}
	}

	@Override
	public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft) {
		int i = this.getMaxItemUseDuration(stack) - timeLeft;
		if(i < 60) {
			ItemStack ammo = GunUtils.findAmmo((EntityPlayer) entityLiving, stack); // FIXME
			if(!ammo.isEmpty()) {
				GunUtils.getOrCreateTagCompound(stack).setTag("loaded", ammo.writeToNBT(new NBTTagCompound()));
				ammo.shrink(1);
				GunUtils.getOrCreateTagCompound(stack).setBoolean("isLoaded", true);
			}
			return;
		}

	}

	@Override
	public int getMaxItemUseDuration(ItemStack stack) {
		if(!GunUtils.getOrCreateTagCompound(stack).getBoolean("isLoaded") || ActionType.AUTO.equals(
				((IMechanism) GunPartRegistry.getPart(GunUtils.getOrCreateTagCompound(stack).getString("MECHANISM")))
						.getActionType())) {
			return 72000;
		}
		return super.getMaxItemUseDuration(stack);
	}

}

@@ -2420,33 +2420,59 @@
 (int i = 0; i %3C 
-4
+GunPartType.values().length
 ; i++) %7B // TODO
@@ -2503,24 +2503,40 @@
 f parts%0A%09%09%09%09
+IGunPart part = 
 GunUtils.get
@@ -2578,16 +2578,50 @@
 es()%5Bi%5D)
+;%0A%09%09%09%09if(part != null) %7B%0A%09%09%09%09%09part
 .onItemR
@@ -2658,16 +2658,22 @@
 andIn);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D
@@ -2871,9 +2871,35 @@
 i %3C 
-4
+GunPartType.values().length
 ; i+
@@ -2945,16 +2945,32 @@
 arts%0A%09%09%09
+IGunPart part = 
 GunUtils
@@ -3016,16 +3016,48 @@
 es()%5Bi%5D)
+;%0A%09%09%09if(part != null) %7B%0A%09%09%09%09part
 .onUsing
@@ -3090,16 +3090,21 @@
 count);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

Fix my loops