import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.File;
import javax.swing.border.BevelBorder;
import utility.DateComboBox;

public class ClientFrame extends JInternalFrame
{

	JDesktopPane desktop;
	Container frameContainer;
	JTabbedPane left = new JTabbedPane();
	JPanel center = new JPanel();
	JTabbedPane bottom = new JTabbedPane();

	JMenuBar mbar = new JMenuBar();
	JMenu toolMenu = new JMenu("Tool");
	JMenu fileMenu = new JMenu("File");
	JMenu noticeMenu = new JMenu("Notice");
	JMenu fsMenu = new JMenu("File Manager");
	JMenu schMenu = new JMenu("Schedules");
	JMenuItem item1, item2 , item3,item4,item5,item6,item7,item8;

	/*JPanel verti1 = new JPanel();
	JPanel verti2 = new JPanel();
	final JPanel hori1 = new JPanel();
	final JPanel hori2 = new JPanel();*/
	
	JScrollPane verti1 = new JScrollPane();
	JScrollPane verti2 = new JScrollPane();
	JScrollPane hori1 = new JScrollPane();
	JScrollPane hori2 = new JScrollPane();



	loginModule.LoginAgent la  = null;
	chatModule.ChatAgent ca = null;
	wbModule.WbAgent wba = null;
	nbModule.NbAgent nba = null;
	fsModule.FsAgent fsa = null;
	shModule.SchAgent scha = null;
	public ClientFrame(JDesktopPane desktop)
	{
		this.desktop = desktop;
		buildGUI();
		int inset = 100;
       		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
       		setSize(screenSize.width - inset*4-3,screenSize.height-inset*2-25);

		//setLF();
		setMBarFalse();

	}
	void buildGUI()
	{
		frameContainer = getContentPane();
		center.setLayout(new BorderLayout());;
		center.setSize(375,215);
		JSplitPane right = new JSplitPane (JSplitPane.VERTICAL_SPLIT,center, bottom);
		right.setDividerLocation(225);
		right.setOneTouchExpandable(true);
		JSplitPane splitPane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left ,right);
		splitPane1.setDividerLocation(175);
		splitPane1.setOneTouchExpandable(true);
		frameContainer.add(splitPane1);


		fileMenu.add(item1 = new JMenuItem("Login"));
		fileMenu.add(item2 = new JMenuItem("Log Out"));
		fileMenu.add(item3 = new JMenuItem("Exit"));
		toolMenu.add(item4 = new JMenuItem("Chat Room"));
		toolMenu.add(item5 = new JMenuItem("Shared WhiteBoard"));
		noticeMenu.add(item6 = new JMenuItem("Upload Notice"));
		fsMenu.add(item7=new JMenuItem("Add New File"));
		schMenu.add(item8=new JMenuItem("Add Schedule"));

		mbar.add(fileMenu);
		mbar.add(toolMenu);
		mbar.add(noticeMenu);
		mbar.add(fsMenu);
		mbar.add(schMenu);
		setJMenuBar(mbar);



		item1.addActionListener(new LoginMenuHandler());
		item4.addActionListener(new ChatRoomMenuHandler());
		item5.addActionListener(new SharedWbMenuHandler());
		item3.addActionListener(new ExitMenuHandler());
		item2.addActionListener(new LogOutMenuHandler());
		item6.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				showChooser();

			}
		});

		item7.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if(fsa != null)
					fsa.addFile();
			}
		});

		item8.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if(scha != null)
					scha.addSchedule();
			}
		});


		left.addTab("Shared Files",verti1);
		left.addTab("Online Members",verti2);
		//left.setSelectedIndex(0);
		bottom.addTab("Notice Board",hori1);
		bottom.addTab("Schedules",hori2);
		//bottom.setSelectedIndex(0);

	}

	public void showChooser()
	{
		JFileChooser chooser = new JFileChooser();
		chooser.setApproveButtonText("Send");
		chooser.setApproveButtonToolTipText("Send as Notice");
		DateComboBox date =new DateComboBox();
		JTextField noticeName = new JTextField(10);
		JPanel jp = new JPanel();
		jp.setLayout(new BoxLayout(jp,BoxLayout.Y_AXIS));
		jp.add(Box.createVerticalStrut(5));
		JLabel label  = new JLabel("Date of Removal");
		JLabel name = new JLabel("Notice Title");
		label.setBorder(new BevelBorder(BevelBorder.RAISED));
		name.setBorder(new BevelBorder(BevelBorder.RAISED));
		jp.add(label);
		jp.add(date);
		jp.add(name);
		jp.add(noticeName);

		chooser.setAccessory(jp);

		int state = chooser.showDialog(null,"send");
		File file = chooser.getSelectedFile();
		if(file != null && state == JFileChooser.APPROVE_OPTION )
		{
			//JOptionPane.showMessageDialog(null, noticeName.getText());
			if((String)date.getSelectedItem() == null)
			{
				JOptionPane.showMessageDialog(null, "choose date of removal");
				showChooser();
			}
			else if(noticeName.getText().compareTo("")==0)
			{
				JOptionPane.showMessageDialog(null, "mention title name");
				showChooser();
			}
			else if(nba != null)
			{
				nba.sendNotice(file,(String)date.getSelectedItem(),noticeName.getText());
			}
		}

		else if(state == JFileChooser.CANCEL_OPTION)
		{
			JOptionPane.showMessageDialog(chooser, "Canceled");
		}
	}

	void setMBarFalse()
	{
		item1.setEnabled(true);
		item2.setEnabled(false);
		item3.setEnabled(true);
		item4.setEnabled(false);
		item5.setEnabled(false);
		item6.setEnabled(false);
		item7.setEnabled(false);
		item8.setEnabled(false);
	}

	void setMBarTrue()
	{
			item1.setEnabled(false);
			item2.setEnabled(true);
			item3.setEnabled(true);
			item4.setEnabled(true);
			item5.setEnabled(true);
			item6.setEnabled(true);
			item7.setEnabled(true);
			item8.setEnabled(true);
	}



	private  class LoginAdaptor implements ChangeListener
	{
		public void stateChanged(ChangeEvent e)
		{
			setMBarTrue();
			la.setVisible(false);

			nba =new nbModule.NbAgent (desktop);
			fsa = new fsModule.FsAgent (desktop);
			scha=new shModule.SchAgent(desktop);
			//nbda = new nbModule.NbDataAgent() ;
			JScrollPane jp = new JScrollPane(nba.cliHandler.g);
			JScrollPane jp1 = new JScrollPane(fsa.cliHandler.g);
			JScrollPane jp2 = new JScrollPane(scha.cliHandler.g);
			JScrollPane jp3 = new JScrollPane(la.cliHandler.g);
			jp1.setVisible(true);

			left.remove(verti1);
			left.addTab("File Server",jp1);

			left.remove(verti2);
			left.addTab("Online Members",jp3);
			bottom.remove(hori1);
			bottom.addTab("Notice Board ",jp);

			//bottom.setSelectedIndex(0);
			bottom.remove(hori2);
			bottom.addTab("Schedules",jp2);
			//bottom.setSelectedIndex(0);
			desktop.remove(la);
		}
	}



	private void setLF()
	{
		String laf = UIManager.getSystemLookAndFeelClassName();
		try
		{
			UIManager.setLookAndFeel(laf);
		}
		catch (UnsupportedLookAndFeelException exc)
		{
			System.err.println("Unsupported: " + laf);
		}
		catch (Exception exc)
		{
			System.err.println("Error loading " + laf);
		}
	}


	public class LoginMenuHandler implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{


			la = new loginModule.LoginAgent();
			la.setBounds(190,65,425,220);
			//la.setBounds(190,52,(int)center.getSize().getWidth(),(int)center.getSize().getHeight());
			desktop.add(la,JLayeredPane.PALETTE_LAYER);
			la.setVisible(true);
			la.addChangeListener(new LoginAdaptor());
		}
	}

	public class ChatRoomMenuHandler implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			if (ca==null)
			{
				ca = new chatModule.ChatAgent ();
				ca.cliHandler.g.setBounds(190,65,425,220);
				//ca.cliHandler.g.setBounds(190,65,(int)center.getSize().getWidth(),(int)center.getSize().getHeight());
				ca.cliHandler.g.addChangeListener(new ChatLoggedOff ());
				desktop.add(ca.cliHandler.g,JLayeredPane.PALETTE_LAYER);
				//ca.cliHandler.g.setVisible(true);
				item4.setEnabled(false);
			}
			//System.out.println("already logged in");
		}
	}

	public class SharedWbMenuHandler implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{

			if (wba==null)
			{
				wba = new wbModule.WbAgent ();
				wba.cliHandler.g.setBounds(190,65,425,220);
				//wba.cliHandler.g.setBounds(190,65,(int)center.getSize().getWidth(),(int)center.getSize().getHeight());
				wba.cliHandler.g.addChangeListener(new WbLoggedOff ());
				desktop.add(wba.cliHandler.g,JLayeredPane.PALETTE_LAYER);
				//wba.cliHandler.g.setVisible(true);
				item5.setEnabled(false);
			}
		}
	}
	public class ExitMenuHandler implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			if(la != null)
				la.logOff();
			if(ca != null)
				ca.logOff();
			if(wba != null)
				wba.logOff();
			if(nba != null)
				nba.logOff();
			if(fsa != null)
				fsa.logOff();
			if(scha != null)
				scha.logOff();
			System.exit(0);
		}
	}
	public class ChatLoggedOff implements ChangeListener
	{
		public void stateChanged(ChangeEvent e)
		{
			desktop.remove(ca.cliHandler.g);
			ca = null;
			item4.setEnabled(true);
		}
	}

	public class WbLoggedOff implements ChangeListener
	{
		public void stateChanged(ChangeEvent e)
		{
			desktop.remove(wba.cliHandler.g);
			wba = null;
			item5.setEnabled(true);
		}
	}
	public class LogOutMenuHandler implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			if(la != null)
				la.logOff();
			if(ca != null)
				ca.logOff();
			if(wba != null)
				wba.logOff();
			 if(nba != null)
				nba.logOff();
			 if(fsa != null)
				fsa.logOff();
			if(scha != null)
				scha.logOff();
			 la = null;
			 ca = null;
			 wba = null;
			 nba = null;
			 fsa = null;
			 scha = null;
			 setMBarFalse();
		}
	}
}

