package ncrashed.registration;

import java.util.HashMap;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.NetLoginHandler;
import net.minecraft.network.packet.NetHandler;
import net.minecraft.network.packet.Packet1Login;
import net.minecraft.server.MinecraftServer;
import cpw.mods.fml.common.network.IConnectionHandler;
import cpw.mods.fml.common.network.Player;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

@SideOnly(Side.SERVER)
public class ConnectionTracker implements IConnectionHandler 
{
	private class LoginInfo
	{
		String login;
		String passhash;
		
		public LoginInfo(String login, String passhash)
		{
			this.login = login;
			this.passhash = passhash;
		}
	}
	
	@Override
	public void playerLoggedIn(Player player, NetHandler netHandler,
			INetworkManager manager) 
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public String connectionReceived(NetLoginHandler netHandler,
			INetworkManager manager) 
	{
		String[] strs = netHandler.clientUsername.split("#", 2);
		if(strs.length != 2)
			return "Invalid username format! <name>#<passhash>";
		
		AuthServer server = new AuthServer(RegistrationMod.AUTH_SERVER_HOST, RegistrationMod.AUTH_SERVER_MIRROW);
		LoginInfo info = new LoginInfo(strs[0], strs[1]);
		
		Boolean answer = server.query("/api/authorization", "GET", 
				info, Boolean.class, false);
		
		if(answer == null)
		{
			server = new AuthServer(RegistrationMod.AUTH_SERVER_HOST, RegistrationMod.AUTH_SERVER_MIRROW);
			answer = server.query("/api/authorization", "GET", 
					info, Boolean.class, true);
			
			if(answer == null)
				return "Authorization server down! Get request.";
		}
		System.out.println("Authorization answer is: " + answer.toString());
		
		server = new AuthServer(RegistrationMod.AUTH_SERVER_HOST, RegistrationMod.AUTH_SERVER_MIRROW);
		if(!answer)
		{
			answer = server.query("/api/register_user", "POST", info, Boolean.class, false);
			if(answer == null)
			{
				server = new AuthServer(RegistrationMod.AUTH_SERVER_HOST, RegistrationMod.AUTH_SERVER_MIRROW);
				answer = server.query("/api/register_user", "POST", info, Boolean.class, true);
				
				if(answer == null)
					return "Authorization server down! Post request.";
			}
			
			System.out.println("Registration answer is: " + answer.toString());
			if(!answer)
			{
				return "Authorization failed!";
			}
		}
		
		netHandler.clientUsername = info.login;
		return null;
	}

	@Override
	public void connectionOpened(NetHandler netClientHandler, String server,
			int port, INetworkManager manager) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void connectionOpened(NetHandler netClientHandler,
			MinecraftServer server, INetworkManager manager) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void connectionClosed(INetworkManager manager) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void clientLoggedIn(NetHandler clientHandler,
			INetworkManager manager, Packet1Login login) {
		// TODO Auto-generated method stub
		
	}
}
