package shModule;
import utility.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.tree.*;
public class SchClientHandler extends ClientHandler
{
	JDesktopPane desktop = null;
	AgentIdentity id ;
	Vector members = null;
	public Gui g = null;
	public GuiSch gs =null;
	boolean loggedIn = false;
	public SchClientHandler()
	{

		g =new Gui();

	}
	public SchClientHandler(InputStream in, OutputStream out,JDesktopPane desktop)
	{
		super(in,out);
		this.desktop = desktop;
		g =new Gui();

	}


	public void doThis(Response res)
	{

		if (loggedIn)
		{
			if( res.getResponseType().compareTo("ScheduleInfo") == 0)
			{
				ScheduleInfo info = (ScheduleInfo)res.getObject();
				members = info.getMembers();
				g.removeAll();
				g.repaint();
				g.buildGui();
				g.repaint();
			}

			else
			if( res.getResponseType().compareTo("ScheduleResponse") == 0)
			{
				ScheduleResponse sr =(ScheduleResponse)res.getObject();
				Vector schedule = sr.schedule;
				String memberName = sr.memberName;
				gs = new GuiSch(schedule,memberName);
				desktop.add(gs,JLayeredPane.PALETTE_LAYER);
			}
			else if( res.getResponseType().compareTo("Allert") == 0)
			{
				String got = (String)res.getObject();
				JOptionPane.showMessageDialog(null, got);
			}
			else
				System.out.println("in doThis method: Unknown response"+res.getResponseType());
		}
	}
	public void setAgentId (AgentIdentity id)
	{
		this.id = id;
	}
	
	public void addSchedule()
	{
		//JOptionPane.showMessageDialog(null, "add Schedule clicked");
		desktop.add(new GuiAddSch(),JLayeredPane.PALETTE_LAYER);
	}

	public class Gui extends JPanel
	{
		public Gui()
		{

		}
		void buildGui()
		{

			DefaultMutableTreeNode top = new DefaultMutableTreeNode( "Select user ");
			DefaultTreeModel model = new DefaultTreeModel(top);
			final JTree  tree =new JTree(model);
			int i=0;int j=members.size();
			DefaultMutableTreeNode node[] = new DefaultMutableTreeNode[j];
			for(i=0;i<j;++i)
			{

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

			add(tree);
			tree.addTreeSelectionListener(new TreeSelectionListener()
			{
				public void valueChanged(TreeSelectionEvent e)
				{
					DefaultMutableTreeNode node =(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
           				if(node != null)
					{
						if(node.isLeaf())
           					{
							String memberName = (String)node.getUserObject();
        						SchRequest sr = new SchRequest(id,memberName);
							Request req = new Request(sr,"SchRequest");
							ClientHeader toBeSent = new ClientHeader(req);
							try
							{
								sendRequest(toBeSent);
							}
							catch(Exception ex)
							{
								JOptionPane.showMessageDialog(null, "cudent send :"+ex);
							}
						}
						else 
							return;
					}
				}
			});
		}
		public void showGui()
		{
			System.out.println("showing Gui");
			loggedIn = true;
			setVisible(true);

		}
	}
	public class GuiSch extends JInternalFrame
	{
		Vector schedule = null;
		String memberName = null;
		public GuiSch(Vector schedule,String memberName)
		{
			super("Schedule",false,true,false,true);
			this.schedule = schedule;
			this.memberName = memberName;
			buildGui();
		}
		void buildGui()
		{
			Container con = getContentPane();
			setVisible(true);
			JPanel jp = new JPanel();
			jp.setLayout(new BoxLayout(jp,BoxLayout.Y_AXIS));
			jp.setVisible(true);

			int i =0; int j=schedule.size();
			JPanel jSched  = new JPanel();
			if(j!=0)
			{

				jSched.setLayout(new GridLayout(j+1,3));

				jSched.add(new JLabel("Schedules for "+memberName ));
				for(i=0;i<2;++i)
					jSched.add(Box.createHorizontalStrut(0));
				for(i=0;i<j;++i)
				{
					jSched.add(new JLabel((String)(((java.util.Vector)schedule.elementAt(i)).elementAt(0))));
					//read.add(Box.createHorizontalStrut(5));
					jSched.add(new JLabel((String)(((java.util.Vector)schedule.elementAt(i)).elementAt(1))));
					//read.add(Box.createHorizontalStrut(5));
					jSched.add(new JLabel((String)(((java.util.Vector)schedule.elementAt(i)).elementAt(2))));

				}
			}
			if(j==0)
			{
				jSched.add(new JLabel("No Schedules for "+memberName ));
			}
			jp.add(jSched );


			JScrollPane jsp = new JScrollPane(jp);
			con.add(jsp);
			setBounds(190,65,425,220);
			setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		}
	}
	public class GuiAddSch extends JInternalFrame
	{


		public GuiAddSch()
		{
			super("Schedule Add",false,true,false,true);
			buildGui();
		}

		String[] items = {	
						"00:00", "00:30","01:30","02:00","02:30","03:00","03:30","04:00",
						"04:30","05:00","05:30","06:00","06:30","07:00","07:30","08:00",
						"08:30","09:00","09:30","10:00", "10:30","11:30","12:00","12:30",
						"13:00","13:30","14:00","14:30","15:00","15:30","16:00","16:30",
						"17:00","17:30","18:00","18:30","19:00","19:30","20:00", "20:30",
						"21:30","22:00","22:30","23:00","23:30"
					};
		JComboBox time = new JComboBox(items);
		DateComboBox d =new DateComboBox();
		JTextField schedField = new JTextField(25);

		void buildGui()
		{
			Container con = getContentPane();
			setVisible(true);
			JPanel jp = new JPanel();
			jp.setLayout(new BoxLayout(jp,BoxLayout.Y_AXIS));
			jp.setVisible(true);

			jp.add(new JLabel("Add Schedule"));

			JPanel jp1 = new JPanel();
			jp1.setLayout(new GridLayout(1,2));
			jp1.add(new JLabel("Date"));
			jp1.add(d);
			jp.add(jp1);

			time.setSelectedItem(null);
			JPanel jp2 = new JPanel();
			jp2.setLayout(new GridLayout(1,2));
			jp2.add(new JLabel("Time"));
			jp2.add(time);
			jp.add(jp2);

			jp.add(Box.createHorizontalStrut(0));

			JButton button = new JButton("Add");
			button.addActionListener(new ButtonHandler());
			JPanel j3 = new JPanel();
			j3.setLayout(new FlowLayout());
			j3.add(schedField);
			j3.add(button);
			jp.add(j3);

			JScrollPane jsp = new JScrollPane(jp);
			con.add(jsp);
			setBounds(190,65,425,220);
			setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		}
		public class ButtonHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				String sch = schedField.getText();
				String date = (String)d.getSelectedItem();
				String timeString = (String)time.getSelectedItem();
				if(date == null)
				{
					JOptionPane.showMessageDialog(null, "choose date ");
				}
				else if(timeString == null)
				{
					JOptionPane.showMessageDialog(null, "choose time ");
				}
				else if (sch.length() > 0)
				{

					dispose();
					try
					{
        	        			ScheduleAdd sa = new ScheduleAdd(id.getLoginName() ,date,timeString ,sch);
						Request req =new Request(sa,"ScheduleAdd");
						ClientHeader toBeSent = new ClientHeader(req);
						sendRequest(toBeSent);
      					}
      					catch (Exception ex)
					{
      						JOptionPane.showMessageDialog(null, ex);
					}
				}
			}
		}
	}
}

