package nbModule;
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.border.BevelBorder;
public class NbClientHandler extends ClientHandler
{

	JDesktopPane desktop = null;
	AgentIdentity id ;
	public Gui g = null;
	public GuiNotice gn =null;
	boolean loggedIn = false;
	public NbClientHandler()
	{

  		g =new Gui();

	}
	public NbClientHandler(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("NoticeResponse") == 0)
			{

				NoticeResponse noticeRes = (NoticeResponse)res.getObject();
				//System.out.println(noticeRes.noticeId + noticeRes.displayer+noticeRes.datePosting+noticeRes.noticeTitle);
				System.out.println("Got notice Response");
				StringBuffer buffer =null;
				NbDataAgent nbda =new NbDataAgent();
				try
				{
					nbda.cliHandler.readStream();
				}
				catch(Exception e)
				{
					System.out.println("cudn't read file from server");
				}
				System.out.println("waiting for sream reading to be finished ");
				while(!nbda.cliHandler.readingComplete)
				{

				}
				System.out.println("reading fininshed getting buffer .");
				buffer = nbda.cliHandler.getBuffer();
				gn =  new GuiNotice(buffer,noticeRes);
				desktop.add(gn,JLayeredPane.PALETTE_LAYER);





			}
			else
			if( res.getResponseType().compareTo("NoticeInfo") == 0)
			{
				NoticeInfo info = (NoticeInfo)res.getObject();
				Vector notices = info.getNotices();
				g.removeAll();
				g.repaint();
				g.setNotices(notices);
				g.buildGui();
				g.repaint();

			}
			
			else
			if( res.getResponseType().compareTo("Allert") == 0)
			{
				JOptionPane.showMessageDialog(null,(String)res.getObject());
			}
			else
				System.out.println("in doThis method: Unknown response"+res.getResponseType());
		}
	}

	public boolean sendNotice(File file,String dateRemoval,String noticeName)
	{
		NbDataAgent nbda =new NbDataAgent();
		boolean success = false;
		NoticeUpload up = new NoticeUpload(id,dateRemoval,noticeName);
		Request req = new Request(up,"NoticeUpload");
		ClientHeader toBeSent = new ClientHeader(req);
		try
		{
				sendRequest(toBeSent);
		}
		catch(Exception e)
		{
				e.printStackTrace();
		}
		System.out.println("sending notice to server ");
		//nbda =new NbDataAgent();
		try
		{
			success = nbda.cliHandler.sendStream(file);
		}
		catch(Exception e)
		{
			System.out.println("cudn't read file from server");
			success =false;
		}
		return success;
	}
	public void setAgentId (AgentIdentity id)
	{
		this.id = id;
	}
	public class Gui extends JPanel
	{

		Vector notices = null;
		public Gui()
		{
		}
		void setNotices(Vector notices)
		{
			this.notices = notices;
		}
		void buildGui()
		{

			if(notices != null)
			{
				int i =0; int j=notices.size();
				if(j!=0)
				{

					final Color col = getBackground();
					setLayout(new GridLayout(j+1,3));
					add(new JLabel("Date" ));
					add(new JLabel("Notice" ));
					add(new JLabel("Displayer"));
					Properties prop[] = new Properties[j];
					for(i=0;i<j;++i)
					{
						prop[i]=(Properties)notices.elementAt(i);
						final JLabel notice =new JLabel(prop[i].getProperty("NoticeTitle"));
						add(new JLabel(prop[i].getProperty("DatePosting")));
						add(notice);
						add(new JLabel(prop[i].getProperty("Displayer")));
						int temp =0;
						try
						{
				 			temp =Integer.parseInt(prop[i].getProperty("NoticeId"));
						}
						catch(Exception e)
						{
							System.out.println(e);
						}
						final int noticeId = temp;
						notice.addMouseListener(new MouseListener()
						{
							public void mousePressed(MouseEvent e) {}
							public void mouseClicked(MouseEvent e)
							{

								NoticeRequest notice = new NoticeRequest(noticeId,id);
								Request req = new Request(notice,"NoticeRequest");
								ClientHeader toBeSent = new ClientHeader(req);
								try
								{
									sendRequest(toBeSent);

								}
								catch(IOException ex)
								{
									System.out.println("Exception :"+ex);
								}



							}
							public void mouseReleased(MouseEvent e)
							{

							}
							public void mouseEntered(MouseEvent e)
							{
			    				notice.setOpaque(true);
			    				notice.setBackground(Color.cyan);
			 				}
							public void mouseExited(MouseEvent e)
							{
			    					notice.setOpaque(false);
			    					notice.setBackground(col);
			    				}
		    				});

					}
				}
				if(j==0)
				{
					add(new JLabel("No Notices Posted" ));
				}
			}
		}

		public void showGui()
		{
			System.out.println("showing Gui");
			loggedIn = true;
			setVisible(true);

		}


	}





	public class GuiNotice extends JInternalFrame
	{

		String buffer = null;
		NoticeResponse noticeRes = null;
		public GuiNotice(StringBuffer buffer,NoticeResponse noticeRes)
		{
			super("Notice",false,true,false,true);
			this.buffer = String.valueOf(buffer);
			this.noticeRes =noticeRes;
			buildGui();
		}
		void buildGui()
		{
			Container con = getContentPane();
			JTextArea textArea = new JTextArea(buffer);
			textArea.setEditable(false);
			textArea.setVisible(true);
			JScrollPane jsp = new JScrollPane(textArea);
			JPanel jp = new JPanel();
			JScrollPane jspComment = new JScrollPane(jp);
			jp.setLayout(new BoxLayout(jp,BoxLayout.Y_AXIS));
			jp.setVisible(true);
			//jp.add(Box.createVerticalStrut(5));

			JPanel p = new JPanel();
			p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS));
			p.add(new JLabel(noticeRes.noticeTitle));
			p.setBorder(new BevelBorder(BevelBorder.RAISED));
			jp.add(p);

			JPanel p1 = new JPanel();
			p1.setLayout(new BoxLayout(p1,BoxLayout.X_AXIS));
			p1.add(new JLabel("Displayer"));
			JTextField t1 = new JTextField(noticeRes.displayer);
			t1.setBorder(new BevelBorder(BevelBorder.RAISED));
			t1.setEditable(false);
			p1.add(Box.createHorizontalStrut(70));
			p1.add(t1);
			jp.add(p1);

			JPanel p2 = new JPanel();
			p2.setLayout(new BoxLayout(p2,BoxLayout.X_AXIS));
			p2.add(new JLabel("Date of posting"));
			JTextField t2 = new JTextField(noticeRes.datePosting);
			t2.setEditable(false);
			t2.setBorder(new BevelBorder(BevelBorder.RAISED));
			p2.add(Box.createHorizontalStrut(25));
			p2.add(t2);
			jp.add(p2);

			JPanel p3 = new JPanel();
			p3.setLayout(new BoxLayout(p3,BoxLayout.X_AXIS));
			p3.add(new JLabel("Date of removal"));
			JTextField t3 = new JTextField(noticeRes.dateRemoving);
			t3.setEditable(false);
			t3.setBorder(new BevelBorder(BevelBorder.RAISED));
			p3.add(Box.createHorizontalStrut(25));
			p3.add(t3);
			jp.add(p3);
			//jp.add(Box.createVerticalStrut(5));

			int i =0;
			Vector v = noticeRes.noticeRead;
			int j= noticeRes.noticeRead.size();
			JPanel read = new JPanel();
			if(j!=0)
			{

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

				read.add(new JLabel("Notice is read by"));
				for(i=0;i<2;++i)
					read.add(Box.createHorizontalStrut(0));
				for(i=0;i<j;++i)
				{
					read.add(new JLabel((String)(((java.util.Vector)v.elementAt(i)).elementAt(0))));
					//read.add(Box.createHorizontalStrut(5));
					read.add(new JLabel("on"));
					//read.add(Box.createHorizontalStrut(5));
					read.add(new JLabel((String)(((java.util.Vector)v.elementAt(i)).elementAt(1))));
				}
			}
			jp.add(read);
			JTabbedPane jtp = new JTabbedPane();
			jtp.addTab("Notice",jsp);
			jtp.addTab("Comments",jspComment);
			con.add(jtp);
			setVisible(true);
			setBounds(190,65,425,220);
			setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		}
	}
}

