package net.viddeno.technical;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.VillagerRegistry;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnumEnchantmentType;
import net.minecraft.item.Item;
import net.minecraft.potion.Potion;
import net.minecraftforge.common.AchievementPage;
import net.minecraftforge.common.MinecraftForge;
import net.viddeno.technical.block.TechnicalBlock;
import net.viddeno.technical.ceativeTabs.TabTechnicalAuto;
import net.viddeno.technical.ceativeTabs.TabTechnicalAxe;
import net.viddeno.technical.ceativeTabs.TabTechnicalBlock;
import net.viddeno.technical.ceativeTabs.TabTechnicalCrusher;
import net.viddeno.technical.ceativeTabs.TabTechnicalHoe;
import net.viddeno.technical.ceativeTabs.TabTechnicalItem;
import net.viddeno.technical.ceativeTabs.TabTechnicalItemOre;
import net.viddeno.technical.ceativeTabs.TabTechnicalMachine;
import net.viddeno.technical.ceativeTabs.TabTechnicalMetal;
import net.viddeno.technical.ceativeTabs.TabTechnicalPickaxe;
import net.viddeno.technical.ceativeTabs.TabTechnicalSpade;
import net.viddeno.technical.ceativeTabs.TabTechnicalSword;
import net.viddeno.technical.ceativeTabs.TabTechnicalTechnology;
import net.viddeno.technical.crafting.Recipes;
import net.viddeno.technical.enchantment.EnchantmentRadioactivity;
import net.viddeno.technical.event.EventListener;
import net.viddeno.technical.event.TechnicalEventHooks;
import net.viddeno.technical.event.TechnicalOreGenEventHooks;
import net.viddeno.technical.gen.TechnicalWorldGenerator;
import net.viddeno.technical.item.TechnicalItem;
import net.viddeno.technical.item.TechnicalTools;
import net.viddeno.technical.potion.PotionTechnical;

@Mod(modid = "Technical", name = Technical.modName, version = Technical.modVersion)
public class Technical {
	public static final String modName = "Technical";

	public static final String modVersion = "B1.0.251";

	public static final String authors = "viddeno";

	public static CreativeTabs tabTechnicalBlocks = new TabTechnicalBlock(CreativeTabs.getNextID(), "tabTechnicalBlock");
	public static CreativeTabs tabTechnicalItemOre = new TabTechnicalItemOre(CreativeTabs.getNextID(), "tabTechnicalItemOre");
	public static CreativeTabs tabTechnicalItems = new TabTechnicalItem(CreativeTabs.getNextID(), "tabTechnicalItem");
	public static CreativeTabs tabTechnicalMetals = new TabTechnicalMetal(CreativeTabs.getNextID(), "tabTechnicalMetal");
	public static CreativeTabs tabTechnicalTechnology = new TabTechnicalTechnology(CreativeTabs.getNextID(), "tabTechnicalTechnology");
	public static CreativeTabs tabTechnicalMachines = new TabTechnicalMachine(CreativeTabs.getNextID(), "tabTechnicalMachine");
	public static CreativeTabs tabTechnicalAuto = new TabTechnicalAuto(CreativeTabs.getNextID(), "tabTechnicalAuto");
	public static CreativeTabs tabTechnicalSword = new TabTechnicalSword(CreativeTabs.getNextID(), "tabTechnicalSword");
	public static CreativeTabs tabTechnicalPickaxe = new TabTechnicalPickaxe(CreativeTabs.getNextID(), "tabTechnicalPickaxe");
	public static CreativeTabs tabTechnicalAxe = new TabTechnicalAxe(CreativeTabs.getNextID(), "tabTechnicalAxe");
	public static CreativeTabs tabTechnicalShovel = new TabTechnicalSpade(CreativeTabs.getNextID(), "tabTechnicalShovel");
	public static CreativeTabs tabTechnicalHoe = new TabTechnicalHoe(CreativeTabs.getNextID(), "tabTechnicalHoe");
	public static CreativeTabs tabTechnicalCrusher = new TabTechnicalCrusher(CreativeTabs.getNextID(), "tabTechnicalCrusher");

	public static AchievementPage technicalAchievementPage;

	TechnicalWorldGenerator eventWorldGen = new TechnicalWorldGenerator();

	public static Potion radioactivityPotion;
	
	public static final Enchantment enchantmantRadioactivity = new EnchantmentRadioactivity(170, 1, EnumEnchantmentType.weapon);

	@Instance(modName)
	public static Technical instance;

	@SidedProxy(clientSide = "net.viddeno.technical.TechnicalClientProxy", serverSide = "net.viddeno.technical.TechnicalServerProxy")
	public static TechnicalServerProxy proxy;

	public static final int guiIdElectricFurnaceBasic = 10;
	public static final int guiIdElectricFurnace = 11;
	public static final int guiIdElectricFurnaceMedium = 12;
	public static final int guiIdElectricFurnaceAdvanced = 13;
	
	public static final int guiIdGrinder = 21;
	public static final int guiIdGrinderMedium = 22;
	public static final int guiIdGrinderAdvanced = 23;
	
	public static final int guiIdElectrolyser = 31;
	public static final int guiIdElectrolyserMedium = 32;
	
	public static final int guiIdBlastFurnaceBasic = 40;
	public static final int guiIdBlastFurnace = 41;
	public static final int guiIdBlastFurnaceMedium = 42;
	public static final int guiIdBlastFurnaceAdvanced = 43;
	
	public static final int guiIdCentrifuge = 51;
	public static final int guiIdCentrifugeMedium = 52;
	public static final int guiIdCentrifugeAdvanced = 53;
	
	public static final int guiIdRepairingForge = 61;
	public static final int guiIdRepairingForgeMedium = 62;
	public static final int guiIdRepairingForgeAdvanced = 63;
	
	public static final int guiIdWasher = 71;
	public static final int guiIdWasherMedium = 72;
	
	public static final int guiIdMiner = 80;
	public static final int guiIdMinerMedium = 81;
	public static final int guiIdMinerAdvanced = 82;

	public static final int guiIdPump = 91;

	public static final int guiIdAutoWorkBench = 101;

	public static final int radioactivityPotionId = 32;

	@Mod.EventHandler
	public void preInit(FMLPreInitializationEvent event) {
		TechnicalBlock.registerBlocks();
		TechnicalItem.registerItems();
		TechnicalTools.registerTools();
		GameRegistry.registerWorldGenerator(this.eventWorldGen, 0);

		proxy.registerTileEntities();
		
		((Item) Item.itemRegistry.getObject("water_bucket")).setMaxStackSize(16);
		
		for (int i = 0; i < 5; ++i) {
			VillagerRegistry.instance().registerVillageTradeHandler(i, new TradeHandler());
		}

		Potion[] potionTypes;
		for (Field f : Potion.class.getDeclaredFields()) {
			f.setAccessible(true);
			try {
				if (f.getName().equals("potionTypes") || f.getName().equals("field_76425_a")) {
					Field modfield = Field.class.getDeclaredField("modifiers");
					modfield.setAccessible(true);
					modfield.setInt(f, f.getModifiers() & ~Modifier.FINAL);
					potionTypes = (Potion[]) f.get(null);
					final Potion[] newPotionTypes = new Potion[256];
					System.arraycopy(potionTypes, 0, newPotionTypes, 0, potionTypes.length);
					f.set(null, newPotionTypes);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		MinecraftForge.EVENT_BUS.register(new TechnicalEventHooks());
		MinecraftForge.ORE_GEN_BUS.register(new TechnicalOreGenEventHooks());
	}

	@Mod.EventHandler
	public void init(FMLInitializationEvent event) {
		proxy.registerNetworkStuff();
		NetworkRegistry.INSTANCE.registerGuiHandler(this, new TechnicalGuiHandler());
		TechnicalItem.removeVanillaRecipes();
		Recipes.init();
		FMLCommonHandler.instance().bus().register(new EventListener());
		radioactivityPotion = (new PotionTechnical(radioactivityPotionId, true, 0)).setIconIndex(0, 0).setPotionName("potion.radioactivityPotion");
		TechnicalAchievement.init();
		AchievementPage.registerAchievementPage(technicalAchievementPage);
	}

	@Mod.EventHandler
	public void postInit(FMLPostInitializationEvent event) {
		
	}
}