package loginModule;
import utility.*;
import java.io.*;
import java.net.*;
import java.util.Vector;
import java.util.Properties;
import chatModule.ChatAgent;
import wbModule.WbAgent;
import nbModule.NbAgent;
import nbModule.NbDataAgent;
import fsModule.FsAgent;
import fsModule.FsDataAgent;
import shModule.SchAgent;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

public class LoginClientHandler extends  ClientHandler
{

	AgentIdentity id;
	public Gui g = null;
	protected EventListenerList changeListeners = new EventListenerList() ;
	Vector agentInfo = null;

	public LoginClientHandler()
	{
		g =new Gui();
	}
	public LoginClientHandler(InputStream in, OutputStream out)
	{
		super(in,out);
		g =new Gui();
	}
	public void doThis(Response res)
	{


		if( res.getResponseType().compareTo("LoginAccepted") == 0)
		{
			System.out.println("Login Accepted ....");
			LoginResponse logRes = (LoginResponse) res.getObject();
			AgentIdentity id = logRes.getId() ;
			System.out.println("Login name :"+id.getLoginName() +" conn id  "+id.getId());
			LoginAgent.id = id;
			ChatAgent.id = id ;
			WbAgent.id = id ;
			NbAgent.id = id ;
			NbDataAgent.id =id;
			FsAgent.id = id;
			FsDataAgent.id = id;
			SchAgent.id = id;
		}

		else if( res.getResponseType().compareTo("Allert") == 0)
		{
			String got = (String)res.getObject();

			JOptionPane.showMessageDialog(null, got);

		}

		else if( res.getResponseType().compareTo("ServerInfo") == 0)
		{
			System.out.println("got server info");
			Vector serverInfo = (Vector)res.getObject();
			for(int i = 0; i<serverInfo.size();++i)
			{
				Properties prop1 = (Properties)serverInfo.elementAt(i);
				System.out.println(prop1.getProperty("serverName"));
				if(prop1.getProperty("serverName").compareTo("chatServer")==0)
				{

					ChatAgent.serverIP =prop1.getProperty("serverIP");
					try
					{
						ChatAgent.portNo = Integer.parseInt(prop1.getProperty("portNo"));
					}
					catch (NumberFormatException e)
					{
						System.out.println("port no number format exception:");
						ChatAgent.portNo = 7000;
					}
				}
				if(prop1.getProperty("serverName").compareTo("wbServer")==0)
				{
					WbAgent.serverIP =prop1.getProperty("serverIP");
					try
					{
						WbAgent.portNo = Integer.parseInt(prop1.getProperty("portNo"));
					}
					catch (NumberFormatException e)
					{
						System.out.println("port no number format exception:");
						WbAgent.portNo = 7000;
					}
				}
				if(prop1.getProperty("serverName").compareTo("nbServer")==0)
				{
					NbAgent.serverIP =prop1.getProperty("serverIP");

					try
					{
						NbAgent.portNo = Integer.parseInt(prop1.getProperty("portNo"));
					}
					catch (NumberFormatException e)
					{
						System.out.println("port no number format exception:");
						NbAgent.portNo =6050;
						System.out.println("starting nd agent");

					}
				}
				if(prop1.getProperty("serverName").compareTo("nbDataServer")==0)
				{
					NbDataAgent.serverIP =prop1.getProperty("serverIP");

					try
					{
						NbDataAgent.portNo = Integer.parseInt(prop1.getProperty("portNo"));
					}
					catch (NumberFormatException e)
					{
						System.out.println("port no number format exception:");
						NbDataAgent.portNo =7050;
						System.out.println("starting nd agent");

					}
				}
				if(prop1.getProperty("serverName").compareTo("fileServer")==0)
				{
					FsAgent.serverIP =prop1.getProperty("serverIP");

					try
					{
						FsAgent.portNo = Integer.parseInt(prop1.getProperty("portNo"));
					}
					catch (NumberFormatException e)
					{
						System.out.println("port no number format exception:");
						FsAgent.portNo =8050;
						System.out.println("starting nd agent");

					}
				}
				if(prop1.getProperty("serverName").compareTo("fsDataServer")==0)
				{
					FsDataAgent.serverIP =prop1.getProperty("serverIP");

					try
					{
						FsDataAgent.portNo = Integer.parseInt(prop1.getProperty("portNo"));
					}
					catch (NumberFormatException e)
					{
						System.out.println("port no number format exception:");
						FsDataAgent.portNo =9050;
						System.out.println("starting nd agent");

					}
				}
				if(prop1.getProperty("serverName").compareTo("schServer")==0)
				{
					SchAgent.serverIP =prop1.getProperty("serverIP");

					try
					{
						SchAgent.portNo = Integer.parseInt(prop1.getProperty("portNo"));
					}
					catch (NumberFormatException e)
					{
						System.out.println("port no number format exception:");
						SchAgent.portNo =9075;
						System.out.println("starting scedule agent");

					}
				}

			}
			fireChange();
		}


		else if( res.getResponseType().compareTo("LoginRejected") == 0)
		{

			JOptionPane.showMessageDialog(null,"Login Rejected ...","error",JOptionPane.ERROR_MESSAGE);
			System.out.println("Login Rejected ....Better luck next time");
			removeConnection();
		}
		else if( res.getResponseType().compareTo("AgentInfo") == 0)
		{
			agentInfo = (Vector)res.getObject();
			g.removeAll();
			g.repaint();
			g.buildGui();
			g.repaint();

		}

		else
			System.out.println("in doThis method: Unknown response"+res.getResponseType());
	}
	public void setAgentId (AgentIdentity id)
	{
		this.id = id;
	}


	public void addChangeListener(ChangeListener x)
	{
		changeListeners.add (ChangeListener.class, x);

	}
	public void removeChangeListener(ChangeListener x)
	{
		changeListeners.remove (ChangeListener.class, x);
	}
	protected void fireChange()
	{

		ChangeEvent c = new ChangeEvent(this);

		Object[] listeners = changeListeners.getListenerList();
		for (int i = listeners.length-2; i >= 0; i -= 2)
		{
			try
			{
				if (listeners[i] == ChangeListener.class)
				{
					ChangeListener cl = (ChangeListener)listeners[i+1];
					cl.stateChanged(c);
				}
			}
			catch(ArrayIndexOutOfBoundsException ex)
			{
					JOptionPane.showMessageDialog(null,ex+" : loginAgent Fire Change");
			}

		}
	}
	public class Gui extends JPanel
	{
		public Gui()
		{

		}
		public void buildGui()
		{
			DefaultMutableTreeNode top = new DefaultMutableTreeNode( "Online Members ");
			DefaultTreeModel model = new DefaultTreeModel(top);
			final JTree  tree =new JTree(model);
			int i=0;int j=agentInfo.size();
			DefaultMutableTreeNode node[] = new DefaultMutableTreeNode[j];
			for(i=0;i<j;++i)
			{

				node[i]= new DefaultMutableTreeNode((String)agentInfo.elementAt(i));
				top.add(node[i]);
			}

			add(tree);
		}
	}
}
