package info.jbcs.minecraft.chisel;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.block.BlockPane;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Icon;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;

public class CarvableHelper implements Carvable {
	static final String modName="Chisel";

	CarvableHelper() {
	}

	static class CarvableVariation {
		String description;
		int metadata;
		int kind;

		Block block;

		String texture;

		Icon icon;
		Icon iconTop;
		Icon iconBot;
	}

	ArrayList<CarvableVariation> variations = new ArrayList<CarvableVariation>();
	CarvableVariation[] map = new CarvableVariation[16];

	public void addVariation(String description, int metadata, Block bb) {
		addVariation(description, metadata, null, bb);
	}

	public void addVariation(String description, int metadata, String texture) {
		addVariation(description, metadata, texture, null);
	}

	public void addVariation(String description, int metadata, String texture, Block block) {
		if (variations.size() > 15)
			return;

		CarvableVariation variation = new CarvableVariation();
		variation.description = description;
		variation.metadata = metadata;

		if (texture != null) {
			variation.texture = texture;

			String path="/mods/"+modName+"/textures/blocks/"+variation.texture;

			boolean  any=Chisel.class.getResource(path + ".png")!=null;
			boolean side=Chisel.class.getResource(path + "-side.png")!=null;
			boolean  top=Chisel.class.getResource(path + "-top.png")!=null;
			boolean  bot=Chisel.class.getResource(path + "-bottom.png")!=null;

			if(bot && top && side){
				variation.kind=2;
			} else if(top && side){
				variation.kind=1;
			} else if(any){
				variation.kind=0;
			} else{
				throw new RuntimeException("No valid textures found for chisel block variation '"+description+"' ("+variation.texture+")");
			}
		} else {
			variation.block = block;
			variation.kind=2;
		}

		variations.add(variation);
		map[metadata] = variation;
	}

	@Override
	public int variations() {
		return variations.size();
	}

	@Override
	public String description(int variationIndex) {
		return variations.get(variationIndex).description;
	}

	@Override
	public int metadata(int variationIndex) {
		return variations.get(variationIndex).metadata;
	}

	public Icon getIcon(int side, int metadata) {
		if (metadata < 0 || metadata > 15)
			metadata = 0;

		CarvableVariation variation=map[metadata];
		if (variation == null)
			return null;

		switch (variation.kind) {
		case 0:
			return variation.icon;
		case 1:
			if (side == 0 || side == 1)
				return variation.iconTop;
			else
				return variation.icon;
		case 2:
			if (side == 0)
				return variation.iconTop;
			else if (side == 1)
				return variation.iconBot;
			else
				return variation.icon;
		}

		return null;
	}

	void register(Block block, String name) {
		block.setUnlocalizedName(name);

		Item.itemsList[block.blockID] = null;
		GameRegistry.registerBlock(block, ItemCarvable.class, name);

		for (CarvableVariation variation : variations) {
			LanguageRegistry.addName(new ItemStack(block.blockID, 1, variation.metadata), variation.description);
		}
	}

	public void registerIcons(String modName, Block block, IconRegister register) {
		for (CarvableVariation variation : variations) {
			if (variation.block != null){
				variation.block.registerIcons(register);
				
				if(variation.block instanceof BlockPane){
					variation.icon = variation.block.getBlockTextureFromSide(2);
					variation.iconTop = ((BlockPane) variation.block).getSideTextureIndex();
					variation.iconBot = ((BlockPane) variation.block).getSideTextureIndex();
				} else{
					switch (variation.kind) {
					case 0:
						variation.icon = variation.block.getBlockTextureFromSide(2);
						break;
					case 1:
						variation.icon = variation.block.getBlockTextureFromSide(2);
						variation.iconTop = variation.block.getBlockTextureFromSide(0);
						break;
					case 2:
						variation.icon = variation.block.getBlockTextureFromSide(2);
						variation.iconTop = variation.block.getBlockTextureFromSide(0);
						variation.iconBot = variation.block.getBlockTextureFromSide(1);
						break;
					}
				}
			} else{
				switch (variation.kind) {
				case 0:
					variation.icon = register.registerIcon(modName + ":" + variation.texture);
					break;
				case 1:
					variation.icon = register.registerIcon(modName + ":" + variation.texture + "-side");
					variation.iconTop = register.registerIcon(modName + ":" + variation.texture + "-top");
					break;
				case 2:
					variation.icon = register.registerIcon(modName + ":" + variation.texture + "-side");
					variation.iconTop = register.registerIcon(modName + ":" + variation.texture + "-top");
					variation.iconBot = register.registerIcon(modName + ":" + variation.texture + "-bottom");
					break;
				}
			}
		}
	}

	public void registerSubBlocks(Block block, CreativeTabs tabs, List list) {
		for (CarvableVariation variation : variations) {
			list.add(new ItemStack(block.blockID, 1, variation.metadata));
		}
	}
}
