package zmaster587.advancedRocketry.item;

import java.util.List;

import com.mojang.realmsclient.gui.ChatFormatting;

import zmaster587.advancedRocketry.api.ISatelliteIdItem;
import zmaster587.advancedRocketry.api.SatelliteRegistry;
import zmaster587.advancedRocketry.api.satellite.SatelliteBase;
import zmaster587.advancedRocketry.api.satellite.SatelliteProperties;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;

public class ItemSatelliteIdentificationChip extends Item implements ISatelliteIdItem {

	private static String name = "name";

	@Override
	public boolean isDamageable() {
		return false;
	}

	public long getSatelliteId(ItemStack stack) {
		if(stack.hasTagCompound()) {
			NBTTagCompound nbt = stack.getTagCompound();

			return nbt.getLong("satelliteId");
		}
		return -1;
	}

	public SatelliteBase getSatellite(ItemStack stack) {
		if(stack.hasTagCompound()) {
			NBTTagCompound nbt = stack.getTagCompound();

			long satId = nbt.getLong("satelliteId");

			SatelliteBase satellite = zmaster587.advancedRocketry.dimension.DimensionManager.getInstance().getSatellite(satId);

			if(satellite != null) {

				if(!nbt.hasKey("dimId") || nbt.getInteger("dimId") == -1) {
					nbt.setInteger("dimId", satellite.getDimensionId());
				}

				World world;
				if( !nbt.hasKey(null) && (world = DimensionManager.getWorld(satellite.getDimensionId())) != null && world.isRemote)
					nbt.setString(name, ChatFormatting.RED + "Dim names broken as of 1.10");
			}


			return satellite;
		}
		return null;
	}

	public void setSatellite(ItemStack stack, SatelliteBase satellite) {
		NBTTagCompound nbt;
		if(stack.hasTagCompound())
			nbt = stack.getTagCompound();
		else 
			nbt = new NBTTagCompound();

		nbt.setString("satelliteName", satellite.getName());
		nbt.setInteger("dimId", satellite.getDimensionId());
		nbt.setLong("satelliteId", satellite.getId());
	}

	/**
	 * Note: this method does not modify dimension info
	 * @param stack itemStack
	 * @param satellite properties of satellite to set info with
	 */
	public void setSatellite(ItemStack stack, SatelliteProperties satellite) {
		erase(stack);
		SatelliteBase satellite2 = SatelliteRegistry.getSatallite(satellite.getSatelliteType());
		if(satellite2 != null) {
			NBTTagCompound nbt;
			if(stack.hasTagCompound())
				nbt = stack.getTagCompound();
			else 
				nbt = new NBTTagCompound();


			nbt.setString("satelliteName", satellite2.getName());
			nbt.setLong("satelliteId", satellite.getId());

			stack.setTagCompound(nbt);
		}
	}

	public void erase(ItemStack stack) {
		stack.setTagCompound(null);
	}

	public void setDim(ItemStack stack, int dimId) {
		NBTTagCompound nbt;
		if(stack.hasTagCompound())
			nbt = stack.getTagCompound();
		else 
			return;

		nbt.setInteger("dimId", dimId);
	}

	public String getSatelliteName(ItemStack stack) {
		if(stack.hasTagCompound()) {
			NBTTagCompound nbt = stack.getTagCompound();

			return nbt.getString("satelliteName");
		}
		return "";
	}

	public int getWorldId(ItemStack stack) {
		NBTTagCompound nbt;

		if(stack.hasTagCompound() && (nbt = stack.getTagCompound()).hasKey("dimId") ) {


			return nbt.getInteger("dimId");
		}
		return -1; // Cant have a nether satellite anyway
	}

	@Override
	public void addInformation(ItemStack stack, EntityPlayer player,
			List list, boolean bool) {
		int worldId = getWorldId(stack);
		long satId = getSatelliteId(stack);

		String satelliteName = getSatelliteName(stack);

		if(satId != -1) {

			if(worldId != -1) {

				if(stack.getTagCompound().hasKey(name)) {

					list.add("ID: " + satId);
					list.add("Planet: " + stack.getTagCompound().getString(name));
					list.add("Satellite: " + satelliteName);
				}
				else {
					list.add("Planet: " +  "Unknown");
					list.add("Satellite: " + "Contact Lost"); //TODO: make satellite respond with name until
				}
			}
			else {
				list.add("ID: " + satId);
				list.add("Planet: Unknown");
				list.add("Satellite: " + satelliteName);
			}
		}
		else
			list.add("Unprogrammed");
	}
}

@@ -71,52 +71,244 @@
 ort 
-com.mojang.realmsclient.gui.ChatFormatting;%0A
+net.minecraft.entity.player.EntityPlayer;%0Aimport net.minecraft.item.Item;%0Aimport net.minecraft.item.ItemStack;%0Aimport net.minecraft.nbt.NBTTagCompound;%0Aimport net.minecraft.world.World;%0Aimport net.minecraftforge.common.DimensionManager;
 %0Aimp
@@ -557,250 +557,58 @@
 es;%0A
-import net.minecraft.entity.player.EntityPlayer;%0Aimport net.minecraft.item.Item;%0Aimport net.minecraft.item.ItemStack;%0Aimport net.minecraft.nbt.NBTTagCompound;%0Aimport net.minecraft.world.World;%0Aimport net.minecraftforge.common.DimensionManager
+%0Aimport com.mojang.realmsclient.gui.ChatFormatting
 ;%0A%0Ap
@@ -1444,25 +1444,8 @@
 %09%7D%0A%0A
-%09%09%09%09World world;%0A
 %09%09%09%09
@@ -1473,42 +1473,99 @@
  && 
-(world = DimensionManager.getWorld
+zmaster587.advancedRocketry.dimension.DimensionManager.getInstance().getDimensionProperties
 (sat
@@ -1592,17 +1592,16 @@
 d())
-)
  != null
  && 
@@ -1600,26 +1600,8 @@
 null
- && world.isRemote
 )%0A%09%09
@@ -1627,58 +1627,137 @@
 me, 
-ChatFormatting.RED + %22Dim names broken as of 1.10%22
+zmaster587.advancedRocketry.dimension.DimensionManager.getInstance().getDimensionProperties(satellite.getDimensionId()).getName()
 );%0A%09

Fix satellites displaying incorrect status