package net.minecraft.src;

import java.io.*;

/**
 * PowerCraft Logic module, providing flat devices, pulsar, crafting tool and
 * indicators
 * 
 * @author MightyPork
 * @copy (c) 2012
 * 
 */
public class mod_PClogic extends BaseMod {

	/**
	 * Textures folder for this module, ending with slash.
	 */
	public static String imgDir = "/PowerCraft/logic/";

	/**
	 * Configuration file
	 */
	public static PCMP conf = new PCMP("/config/PC_LOGIC.properties", "PowerCraft logic plugin - setup file\nBlock IDs: 122-255\n");


	@Override
	public String getPriorities() {
		return "after:mod_PCcore";
	}


	@Override
	public String getVersion() {
		return mod_PCcore.VERSION;
	}

	public static boolean recursive_crafting = false;
	public static boolean survival_cheating = false;

	static {
		// renaming of the config files.
		String oldName = "./config/FC_LOGIC.properties";
		String newName = "./config/PC_LOGIC.properties";
		if ((new File(oldName)).exists()) {
			File f = new File(oldName);
			f.renameTo(new File(newName));
		}

		conf.put("device_LogicGate_Off", 223, PCMPType.BLOCK);
		conf.put("device_LogicGate_On", 224, PCMPType.BLOCK);
		conf.put("device_RedstoneClock", 225, PCMPType.BLOCK);
		conf.put("indicator_Off", 226, PCMPType.BLOCK);
		conf.put("indicator_On", 227, PCMPType.BLOCK);
		conf.put("ItemGateCrafter_id", 19003, PCMPType.ITEM);
		conf.put("indicator_BRIGHTNESS", 12, PCMPType.INT);
		conf.put("gate_on_BRIGHTNESS", 8, PCMPType.INT);
		conf.put("ENABLE_GateCrafter_recursiveCrafting", false, PCMPType.BOOL);
		conf.put("ENABLE_GateCrafter_cheating-in-survival", false, PCMPType.BOOL);

		conf.apply();

		recursive_crafting = conf.getBoolean("ENABLE_GateCrafter_recursiveCrafting");
		survival_cheating = conf.getBoolean("ENABLE_GateCrafter_cheating-in-survival");
		System.out.println("loadin: " + survival_cheating);
	}


	public mod_PClogic() {
	}


	@Override
	public void load() {
		ModLoader.getMinecraftInstance().renderEngine.getTexture(imgDir + "tiles.png");

		ModLoader.registerTileEntity(net.minecraft.src.PClo_TileEntityGate.class, "FCLogicGate");
		ModLoader.registerTileEntity(net.minecraft.src.PClo_TileEntityPulsar.class, "FCRedstonePulsar");
		ModLoader.registerTileEntity(net.minecraft.src.PClo_TileEntityIndicator.class, "FCRedstoneIndicator");

		ModLoader.registerBlock(pulsar);
		ModLoader.registerBlock(gateOff);
		ModLoader.registerBlock(gateOn);

		ModLoader.registerBlock(lightOff);
		ModLoader.registerBlock(lightOn);

		// empty the slots
		Item.itemsList[gateOn.blockID] = null;
		// replace with the builder item
		Item.itemsList[gateOn.blockID] = new PClo_ItemBlockGate(gateOn.blockID - 256).setItemName("PCloLogicGate");

		// empty the slots
		Item.itemsList[lightOn.blockID] = null;
		// replace with the builder item
		Item.itemsList[lightOn.blockID] = new PClo_ItemBlockIndicator(lightOn.blockID - 256).setItemName("PCloIndicator");

		// names

		gatecrafter.setIconIndex(ModLoader.addOverride("/gui/items.png", imgDir + "gatecrafter.png"));

		ModLoader.addName(gatecrafter, "PowerCraft's Crafting Tool");

		ModLoader.addName(lightOn, "Control Light");
		ModLoader.addName(lightOff, "Control Light");

		ModLoader.addLocalization("tile.PCloIndicator.color0.name", "Black Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color1.name", "Red Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color2.name", "Green Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color3.name", "Brown Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color4.name", "Blue Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color5.name", "Purple Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color6.name", "Cyan Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color7.name", "Light-gray Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color8.name", "Gray Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color9.name", "Pink Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color10.name", "Lime Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color11.name", "Yellow Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color12.name", "Light-blue Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color13.name", "Magenta Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color14.name", "Orange Control Light");
		ModLoader.addLocalization("tile.PCloIndicator.color15.name", "White Control Light");

		ModLoader.addLocalization("tile.PCloLogicGate.crossing.name", "Redstone Crossing");
		ModLoader.addLocalization("tile.PCloLogicGate.holddelayer.name", "Slow Repeater");
		ModLoader.addLocalization("tile.PCloLogicGate.fifodelayer.name", "Buffered Delayer");
		ModLoader.addLocalization("tile.PCloLogicGate.special.name", "Redstone Special Controller");
		ModLoader.addLocalization("tile.PCloLogicGate.chestEmpty.name", "Empty Chest Detector");
		ModLoader.addLocalization("tile.PCloLogicGate.chestFull.name", "Full Chest Detector");
		ModLoader.addLocalization("tile.PCloLogicGate.day.name", "Daytime Sensor");
		ModLoader.addLocalization("tile.PCloLogicGate.rain.name", "Rain Sensor");
		ModLoader.addLocalization("tile.PCloLogicGate.not.name", "Redstone Inverter");
		ModLoader.addLocalization("tile.PCloLogicGate.and.name", "Redstone AND gate");
		ModLoader.addLocalization("tile.PCloLogicGate.nand.name", "Redstone NAND gate");
		ModLoader.addLocalization("tile.PCloLogicGate.or.name", "Redstone OR gate");
		ModLoader.addLocalization("tile.PCloLogicGate.nor.name", "Redstone NOR gate");
		ModLoader.addLocalization("tile.PCloLogicGate.xor.name", "Redstone XOR gate");
		ModLoader.addLocalization("tile.PCloLogicGate.xnor.name", "Redstone XNOR gate");
		ModLoader.addLocalization("tile.PCloLogicGate.xnor3.name", "Redstone 3-input XNOR gate");
		ModLoader.addLocalization("tile.PCloLogicGate.and3.name", "Redstone 3-input AND gate");
		ModLoader.addLocalization("tile.PCloLogicGate.nand3.name", "Redstone 3-input NAND gate");
		ModLoader.addLocalization("tile.PCloLogicGate.or3.name", "Redstone 3-input OR gate");
		ModLoader.addLocalization("tile.PCloLogicGate.nor3.name", "Redstone 3-input NOR gate");
		ModLoader.addLocalization("tile.PCloLogicGate.xor3.name", "Redstone 3-input XOR gate");
		ModLoader.addLocalization("tile.PCloLogicGate.d.name", "Redstone D flip-flop");
		ModLoader.addLocalization("tile.PCloLogicGate.rs.name", "Redstone RS flip-flop");
		ModLoader.addLocalization("tile.PCloLogicGate.t.name", "Redstone T flip-flop");
		ModLoader.addName(pulsar, "Redstone Pulsar");

		//extra stuff
		
		ModLoader.addRecipe(
				new ItemStack(pulsar, 1, 0),
				new Object[] { " r ", "ror", " r ",
					Character.valueOf('r'), Item.redstone, Character.valueOf('o'), Block.obsidian });

		// basic gates
		
		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.NOT),
				new Object[] { "RST",
					Character.valueOf('R'), Item.redstone, Character.valueOf('S'), Block.stone, Character.valueOf('T'), Block.torchRedstoneActive });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.AND),
				new Object[] { " R ", "SSS", "R R",
					Character.valueOf('R'), Item.redstone, Character.valueOf('S'), Block.stone });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.OR),
				new Object[] { " R ", "RSR",
					Character.valueOf('R'), Item.redstone, Character.valueOf('S'), Block.stone });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.XOR),
				new Object[] { "R", "X",
					Character.valueOf('X'), new ItemStack(gateOn, 1, PClo_GType.OR), Character.valueOf('R'), Item.redstone });

		// negated
		
		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.NAND),
				new Object[] { "I", "G",
					Character.valueOf('I'), new ItemStack(gateOn, 1, PClo_GType.NOT), Character.valueOf('G'), new ItemStack(gateOn, 1, PClo_GType.AND) });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.NOR),
				new Object[] { "I", "G",
					Character.valueOf('I'), new ItemStack(gateOn, 1, PClo_GType.NOT), Character.valueOf('G'), new ItemStack(gateOn, 1, PClo_GType.OR) });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.XNOR),
				new Object[] { "I", "G",
					Character.valueOf('I'), new ItemStack(gateOn, 1, PClo_GType.NOT), Character.valueOf('G'), new ItemStack(gateOn, 1, PClo_GType.XOR) });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.NOR3),
				new Object[] { "I", "G",
					Character.valueOf('I'), new ItemStack(gateOn, 1, PClo_GType.NOT), Character.valueOf('G'), new ItemStack(gateOn, 1, PClo_GType.OR3) });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.NAND3),
				new Object[] { "I", "G",
					Character.valueOf('I'), new ItemStack(gateOn, 1, PClo_GType.NOT), Character.valueOf('G'), new ItemStack(gateOn, 1, PClo_GType.AND3) });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.XNOR3),
				new Object[] { "I", "G",
					Character.valueOf('I'), new ItemStack(gateOn, 1, PClo_GType.NOT), Character.valueOf('G'), new ItemStack(gateOn, 1, PClo_GType.XOR3) });

		// basic 3-input
		
		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.AND3),
				new Object[] { " R ", "SSS", "RRR",
					Character.valueOf('R'), Item.redstone, Character.valueOf('S'), Block.stone });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.OR3),
				new Object[] { " R ", "RSR", " R ",
					Character.valueOf('R'), Item.redstone, Character.valueOf('S'), Block.stone });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.XOR3),
				new Object[] { "R", "X",
					Character.valueOf('X'), new ItemStack(gateOn, 1, PClo_GType.OR3), Character.valueOf('R'), Item.redstone });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.D),
				new Object[] { " S ", "RSR", " S ",
					Character.valueOf('S'), Block.stone, Character.valueOf('R'), Item.redstone });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.RS),
				new Object[] { " R ", "SLS", "R R",
					Character.valueOf('R'), Item.redstone, Character.valueOf('S'), Block.stone, Character.valueOf('L'), Block.lever });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.T),
				new Object[] { "RSR",
					Character.valueOf('R'), Item.redstone, Character.valueOf('S'), Block.stone });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.DAY),
				new Object[] { "G", "P",
					Character.valueOf('G'), Item.lightStoneDust, Character.valueOf('P'), Block.pressurePlatePlanks });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.DAY),
				new Object[] { "G", "P",
					Character.valueOf('G'), Item.lightStoneDust, Character.valueOf('P'), Block.pressurePlateStone });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.RAIN),
				new Object[] { "L", "P",
					Character.valueOf('L'), new ItemStack(Item.dyePowder, 1, 4), Character.valueOf('P'), Block.pressurePlatePlanks });
		
		ModLoader.addRecipe(
				new ItemStack(gateOn, 1,
						PClo_GType.RAIN),
				new Object[] { "L", "P",
					Character.valueOf('L'), new ItemStack(Item.dyePowder, 1, 4), Character.valueOf('P'), Block.pressurePlateStone });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.CHEST_EMPTY),
				new Object[] { "C", "P",
					Character.valueOf('C'), Block.chest, Character.valueOf('P'), Block.pressurePlatePlanks });
		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.CHEST_EMPTY),
				new Object[] { "C", "P",
					Character.valueOf('C'), Block.chest, Character.valueOf('P'), Block.pressurePlateStone });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.CHEST_FULL),
				new Object[] { "I", "G",
					Character.valueOf('I'), new ItemStack(gateOn, 1, PClo_GType.NOT), Character.valueOf('G'), new ItemStack(gateOn, 1, PClo_GType.CHEST_EMPTY) });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.SPECIAL),
				new Object[] { " I", "RS",
					Character.valueOf('R'), Item.redstone, Character.valueOf('S'), Block.stone, Character.valueOf('I'), Item.ingotIron });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.FIFO_DELAYER),
				new Object[] { "DDD", "SSS",
					Character.valueOf('D'), Item.redstoneRepeater, Character.valueOf('S'), Block.stone });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.HOLD_DELAYER),
				new Object[] { "DD", "SS",
					Character.valueOf('D'), Item.redstoneRepeater, Character.valueOf('S'), Block.stone });

		ModLoader.addRecipe(
				new ItemStack(gateOn, 1, PClo_GType.CROSSING),
				new Object[] { " + ", "+++", " + ",
					Character.valueOf('+'), Item.redstone });

		// lights
		
		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 1),
				new Object[] { Item.redstone, Item.lightStoneDust });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 0),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 0) });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 2),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 2) });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 3),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 3) });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 4),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 4) });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 5),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 5) });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 6),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 6) });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 7),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 7) });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 8),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 8) });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 9),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 9) });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 10),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 10) });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 11),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 11) });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 12),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 12) });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 13),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 13) });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 14),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 14) });

		ModLoader.addShapelessRecipe(
				new ItemStack(lightOn, 1, 15),
				new Object[] { new ItemStack(lightOn, 1, 1), new ItemStack(Item.dyePowder, 1, 15) });

		// TMI
		PCco_Utils.TMI_maxDamage(gateOn.blockID, PClo_GType.TOTAL_GATE_COUNT - 1);
		PCco_Utils.TMI_hideId(gateOff.blockID);
		PCco_Utils.TMI_hideId(lightOff.blockID);

	}

	// registering blocks and items

	/** flat device, off state */
	public static Block gateOff = new PClo_BlockGate(conf.getInt("device_LogicGate_Off"), false).
			setBlockName("PCloLogicGate").
			setHardness(0.0F).
			setResistance(0.0F).
			setStepSound(Block.soundWoodFootstep).
			disableStats().
			setRequiresSelfNotify().
			setLightValue(0);

	/** flat device, on state */
	public static Block gateOn = new PClo_BlockGate(conf.getInt("device_LogicGate_On"), true).
			setBlockName("PCloLogicGate").
			setHardness(0.0F).
			setResistance(0.0F).
			setStepSound(Block.soundWoodFootstep).
			disableStats().
			setRequiresSelfNotify().
			setLightValue(conf.getInt("gate_on_BRIGHTNESS") * 0.0625F);

	/** pulsar block */
	public static Block pulsar = new PClo_BlockPulsar(conf.getInt("device_RedstoneClock")).
			setHardness(0.8F).
			setBlockName("PCloRedstonePulsar").
			setRequiresSelfNotify().
			setStepSound(Block.soundWoodFootstep);

	/** light, off state */
	public static Block lightOff = new PClo_BlockIndicator(conf.getInt("indicator_Off"), false).
			setHardness(0.0F).
			setBlockName("PCloIndicator").
			setStepSound(Block.soundStoneFootstep).
			setRequiresSelfNotify();

	/** light, on state */
	public static Block lightOn = new PClo_BlockIndicator(conf.getInt("indicator_On"), true).
			setHardness(0.0F).
			setBlockName("PCloIndicator").
			setStepSound(Block.soundStoneFootstep).
			setRequiresSelfNotify().
			setLightValue(conf.getInt("indicator_BRIGHTNESS") * 0.0625F);


	/**
	 * Get description bubble for gate
	 * 
	 * @param dmg
	 *            gate item damage value
	 * @return the description string
	 */
	public static String getDescriptionForGate(int dmg) {
		return PClo_GType.descriptions[MathHelper.clamp_int(dmg, 0, PClo_GType.TOTAL_GATE_COUNT - 1)];
	}
}
