package org.squiddev.plethora.gameplay.redstone;

import com.google.common.collect.Sets;
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.peripheral.IPeripheralProvider;
import dan200.computercraft.shared.common.BlockGeneric;
import dan200.computercraft.shared.common.TileGeneric;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.Item;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.Optional;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.squiddev.plethora.gameplay.ItemBlockBase;
import org.squiddev.plethora.gameplay.Plethora;
import org.squiddev.plethora.gameplay.registry.IClientModule;
import org.squiddev.plethora.utils.Helpers;

import java.util.HashSet;
import java.util.Iterator;

/**
 * We extends {@link BlockGeneric} as the bundled redstone provider requires it:
 * {@link ComputerCraftAPI#getBundledRedstoneOutput(World, BlockPos, EnumFacing)}.
 *
 * This means
 */
public class BlockRedstoneIntegrator extends BlockGeneric implements IClientModule, IPeripheralProvider {
	private static final HashSet<TileRedstoneIntegrator> toTick = Sets.newHashSet();

	private static final String NAME = "redstone_integrator";

	public BlockRedstoneIntegrator() {
		super(Material.rock);

		setHardness(2);
		setUnlocalizedName(Plethora.RESOURCE_DOMAIN + "." + NAME);
		setCreativeTab(Plethora.getCreativeTab());
	}

	@Override
	@Optional.Method(modid = "This mod should never exist: just a hack to remove this method")
	public TileEntity createNewTileEntity(World worldIn, int meta) {
		return new TileRedstoneIntegrator();
	}

	@Override
	protected IBlockState getDefaultBlockState(int meta, EnumFacing direction) {
		return blockState.getBaseState();
	}

	@Override
	protected TileGeneric createTile(IBlockState blockState) {
		return new TileRedstoneIntegrator();
	}

	@Override
	protected TileGeneric createTile(int meta) {
		return new TileRedstoneIntegrator();
	}

	@Override
	public int getRenderType() {
		return 3;
	}

	@Override
	public boolean canLoad() {
		return true;
	}

	@Override
	public void preInit() {
		GameRegistry.registerBlock(this, ItemBlockBase.class, NAME);
		GameRegistry.registerTileEntity(TileRedstoneIntegrator.class, Plethora.RESOURCE_DOMAIN + ":" + NAME);

		MinecraftForge.EVENT_BUS.register(this);
		ComputerCraftAPI.registerPeripheralProvider(this);
	}

	@Override
	public void init() {
	}

	@Override
	public void postInit() {
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void clientInit() {
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void clientPreInit() {
		Helpers.setupModel(Item.getItemFromBlock(this), 0, NAME);
	}

	public static void enqueueTick(TileRedstoneIntegrator tile) {
		synchronized (toTick) {
			toTick.add(tile);
		}
	}

	@SubscribeEvent
	public void handleTick(TickEvent.ServerTickEvent e) {
		if (e.phase == TickEvent.Phase.START) {
			synchronized (toTick) {
				for (TileRedstoneIntegrator tile : toTick) {
					tile.updateOnce();
				}
				toTick.clear();
			}
		}
	}

	@SubscribeEvent
	public void handleUnload(WorldEvent.Unload e) {
		if (!e.world.isRemote) {
			synchronized (toTick) {
				Iterator<TileRedstoneIntegrator> iter = toTick.iterator();
				while (iter.hasNext()) {
					World world = iter.next().getWorld();
					if (world == null || world == e.world) iter.remove();
				}
			}
		}
	}

	@Override
	public IPeripheral getPeripheral(World world, BlockPos blockPos, EnumFacing enumFacing) {
		TileEntity te = world.getTileEntity(blockPos);
		return te instanceof TileRedstoneIntegrator ? (IPeripheral) te : null;
	}
}

@@ -374,123 +374,365 @@
 ort 
-net.minecraft.block.material.Material;%0Aimport net.minecraft.block.state.IBlockState;%0Aimport net.minecraft.item.Item
+dan200.computercraft.shared.peripheral.PeripheralType;%0Aimport dan200.computercraft.shared.peripheral.common.PeripheralItemFactory;%0Aimport net.minecraft.block.material.Material;%0Aimport net.minecraft.block.state.IBlockState;%0Aimport net.minecraft.init.Blocks;%0Aimport net.minecraft.init.Items;%0Aimport net.minecraft.item.Item;%0Aimport net.minecraft.item.ItemStack
 ;%0Aim
@@ -3197,32 +3197,234 @@
 c void init() %7B%0A
+%09%09GameRegistry.addShapedRecipe(new ItemStack(this),%0A%09%09%09%22SRS%22,%0A%09%09%09%22RCR%22,%0A%09%09%09%22SRS%22,%0A%09%09%09'S', Blocks.stone,%0A%09%09%09'C', PeripheralItemFactory.create(PeripheralType.Cable, null, -1),%0A%09%09%09'R', Items.redstone%0A%09%09);%0A
 %09%7D%0A%0A%09@Override%0A%09

Add recipe for RedstoneIntegrator