//Nathan Viniconis
//CSE230 - UCONN
//12-2001

//Code to control the Department Head Client...

import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;

public class DClientA3 {



	public static void main(String[] args) {
		String Rmessage, Smessage;
		try{
			Socket s = new Socket("127.0.0.1", 8189);
			ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
			ObjectInputStream in = new ObjectInputStream(s.getInputStream());
			DHandlerA3 DHand = new DHandlerA3(out,in);
			Smessage = "d";
			out.writeObject(Smessage);

			DHand.Communicate();

			s.close();
		}catch(Exception e) { System.out.println(e); }
	}
}

class DHandlerA3	{
	private ObjectOutputStream out;
	private	ObjectInputStream in;
	private BufferedReader InputMes = new BufferedReader(new InputStreamReader(System.in));
	private Faculty ONEON;
	private boolean LoggedOn = false;
//Adds Faculty

	DHandlerA3(ObjectOutputStream out2, ObjectInputStream in2)	{
		out = out2;
		in = in2;
	}


	private boolean LogOn()	{
		String Lname="",Pass="";
		Faculty search = new Faculty();
		try
		{
			Lname = Log_in.Name1;
			//System.out.print(Lname);
			Pass = Log_in.Pass1;
			//System.out.print(Pass);
			out.writeObject(Lname);
			out.writeObject(Pass);
			search = (Faculty)in.readObject();


		}catch (Exception e)	{	System.out.println("XXX");	}
		if(search.Lname.equals("XXX"))
		{
			JOptionPane.showMessageDialog(null, "The user information did no match",
				"Error", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
			return(false);
		}
		else if(!search.TypeFaculty)
		{
			JOptionPane.showMessageDialog(null, "You are not Department Head",
				"Error", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
			return(false);
		}
		else
		{
			ONEON = search;
			LoggedOn= true;
			Tdepthead.loginscreen.hide();
			JOptionPane.showMessageDialog(null, "Login Successful",
				"Login Successful", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
			return(true);

		}

	}



	private void AddFaculty()	{
		String Rmessage,Smessage;
		Faculty insert = new Faculty();
		int write = 1;
		int i=0;
		Vector Facs = new Vector(20,5);
		Faculty curFac = new Faculty();
		try	{

			Facs = (Vector)in.readObject();
			insert.Lname = Tdepthead.TEMP1;  //last name
			insert.Fname = Tdepthead.TEMP3;  ///first name
			insert.Dept = ONEON.Dept;
			insert.Password = Tdepthead.TEMP2; //password
			try	{
				while(i<2000) {
					curFac = (Faculty)Facs.get(i);
					if(curFac.Lname.equals(insert.Lname)) {
						write = 0;
					}
					i++;
				}
			}catch (Exception e)	{}

			if(write == 1)
			{
				out.writeObject(insert);
				JOptionPane.showMessageDialog(null, "Faculty Added",
				"Added", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
			}
			else{
				insert.Lname = "XXX";
				JOptionPane.showMessageDialog(null, "That Faculty already exists",
				"Error", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
				out.writeObject(insert);
			}
			write = 1;
		}catch (Exception e)	{	System.out.println(e);	}
	}

	private void ListFaculty()	{
		int i=0;
		Vector Facs = new Vector(20,5);
		Faculty curFac = new Faculty();
		Tdepthead.TextArea.setText("");
		try	{
			Facs = (Vector)in.readObject();
			Tdepthead.TextArea.append("The Current Faculty: \n");
			//System.out.println("The Current Faculty: ");
			i=Facs.size()-1;
			while(i>=0)
			{
				curFac = (Faculty)Facs.get(i);
				if(curFac.Dept.equals(ONEON.Dept))	{
					Tdepthead.TextArea.append("LastName: "+curFac.Lname+" FirstName: "+curFac.Fname +
						" Dept: " +curFac.Dept + " Pass: " + curFac.Password);
					if(curFac.TypeFaculty)	{
						Tdepthead.TextArea.append(" DH: Y\n");
					}
					else	{
						Tdepthead.TextArea.append(" DH: N \n");
					}

					Tdepthead.TextArea.append("   Phone: "+curFac.info[0]+ " Email: "+curFac.info[1]+"\n");
					Tdepthead.TextArea.append("   Address: "+curFac.info[2]+" Birthday: "+curFac.info[3]+"\n\n");
				}
				i--;
			}
		} catch(Exception e) { System.out.print(""); }
	}

	private void ChangeFaculty()	{
		Faculty curFac = new Faculty();
		Vector Facs = new Vector(20,5);
		String Rmessage;
		int write = 1;
		int i = 0;
		try{
			Facs = (Vector)in.readObject();
			Rmessage = Tdepthead.TEMP1;
			try	{
				while(i<2000) {
					curFac = (Faculty)Facs.get(i);
					if(curFac.Lname.equals(Rmessage)) {
						write = 0;
						i=2300;
					}
					i++;
				}
			}catch (Exception e)	{}

			if(write == 0 && curFac.Dept.equals(ONEON.Dept) )	{
				Rmessage = Tdepthead.TEMP3;  //phone
				curFac.info[0] = Rmessage;
				Rmessage = Tdepthead.TEMP2;  //Email
				curFac.info[1] = Rmessage;
				Rmessage = Tdepthead.TEMP4;  //Address
				curFac.info[2] = Rmessage;
				Rmessage = Tdepthead.TEMP5;   //Bday
				curFac.info[3] = Rmessage;

				out.writeObject(curFac);
				JOptionPane.showMessageDialog(null, "Faculty Information Updated",
				"Faculty Updated", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
			}
			else	{
				JOptionPane.showMessageDialog(null, "Either Faculty DNE, or Improper Permissions",
				"Faculty Updated", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
				curFac.Lname = "XXX";
				out.writeObject(curFac);
			}
		}catch (Exception e)	{	System.out.println(e);	}
	}


	private void RemFaculty()	{
		String Rmessage,Smessage;
		Faculty insert = new Faculty();
		int write = 1;
		int i=0;
		Vector Facs = new Vector(20,5);
		Faculty curFac = new Faculty();
		try	{

			Facs = (Vector)in.readObject();
			insert.Lname = Tdepthead.TEMP1;
			try	{
				while(i<2000 && write == 1) {
					curFac = (Faculty)Facs.get(i);
					if(curFac.Lname.equals(insert.Lname)) {
						write = 0;
					}
					i++;
				}
			}catch (Exception e)	{}

			if(write == 0 && ONEON.Dept.equals(curFac.Dept) && !ONEON.Lname.equals(curFac.Lname) )  {
				JOptionPane.showMessageDialog(null, "Faculty Deleted",
				"Deleted", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
				out.writeObject(curFac);
			}
			else if(ONEON.Lname.equals(curFac.Lname) )	{
				curFac.Lname = "XXX";
				JOptionPane.showMessageDialog(null, "You Can't Delete Yourself",
				"Error", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
				out.writeObject(curFac);
			}
			else if(write == 0)	{
				JOptionPane.showMessageDialog(null, "You Don't Have Permission to delete",
				"Error", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
				curFac.Lname = "XXX";
				out.writeObject(curFac);
			}
			else{
				curFac.Lname = "XXX";
				JOptionPane.showMessageDialog(null, "That Faculty Does Not Exist",
				"Error", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
				out.writeObject(curFac);
			}
			write = 1;
		}catch (Exception e)	{	System.out.println(e);	}
	}

	public void Communicate()	{
		boolean done = false;
		String Rmessage,Smessage;
		try	{
			Rmessage = (String)in.readObject();
			while(!done) {
				do
				{
					Smessage = condition;
				}while (Smessage.equals(""));
				if(LoggedOn)
				{
					Smessage = Smessage.concat("+");
				}
				out.writeObject(Smessage);
				Rmessage = (String)in.readObject();
				//System.out.println(Rmessage);
				//if(Rmessage == null)
				//	done = false;

				if (Rmessage.trim().equals("LOGON")){
					LogOn();
					condition="";
				}
				else if	(Rmessage.trim().equals("ADDFAC") && LoggedOn)	{
					AddFaculty();
					condition="";
				}
				else if	(Rmessage.trim().equals("LISTFAC") && LoggedOn)	{
					ListFaculty();
					condition="";
				}
				else if (Rmessage.trim().equals("REMFAC") && LoggedOn)	{
					RemFaculty();
					condition="";
				}
				else if (Rmessage.trim().equals("CHGFAC") && LoggedOn)	{
					ChangeFaculty();
					condition="";
				}
				else if (Rmessage.trim().equals("LISTCAT") && LoggedOn)	{
					ListCatalog();
					condition="";
				}
				else if (Rmessage.trim().equals("LISTSEM") && LoggedOn) {
					ListCourses();
					condition="";
				}
				else if (Rmessage.trim().equals("ADDCAT") && LoggedOn) {
					AddCourse(true);
					condition="";
				}
				else if (Rmessage.trim().equals("ADDSEM") && LoggedOn) {
					AddCourse(false);
					condition="";
				}
				else if (Rmessage.trim().equals("CHGCAT") && LoggedOn) {
					ChangeCourse();
					condition="";
				}
				else if (Rmessage.trim().equals("CHGSEM") && LoggedOn) {
					ChangeCourse();
					condition="";
				}
				else if (Rmessage.trim().equals("REMCAT") && LoggedOn) {
					RemoveCourse(true);
					condition="";
				}
				else if (Rmessage.trim().equals("REMSEM") && LoggedOn) {
					RemoveCourse(false);
					condition="";
				}
				else if (Rmessage.trim().equals("ASSIGN") && LoggedOn) {
					AssignAd();
					condition="";
				}
				else{
					if(Rmessage.trim().equals("BYE"))
					done = true;
				}
						}
		}catch(Exception e) { System.out.println(e); }
	}


	private void RemoveCourse(boolean which)	{
		String Rmessage,Smessage;
		Course insert = new Course();
		int write = 1;
		int i=0;
		Vector Cors = new Vector(20,5);
		Course curCor = new Course();
		try	{

			Cors = (Vector)in.readObject();
			insert.ID = Tdepthead.TEMP1;  //Course ID

			try	{
				while(i<2000 && write == 1) {
					curCor = (Course)Cors.get(i);
					if(curCor.ID.equals(insert.ID)) {
						write = 0;
					}
					i++;
				}
			}catch (Exception e)	{}

			if(write == 0 && ONEON.Dept.equals(curCor.Dept) )  {
				out.writeObject(curCor);
				JOptionPane.showMessageDialog(null, "Course Deleted",
				"Deleted", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));

			}
			else if(write == 0)	{

				JOptionPane.showMessageDialog(null, "You don't have permission to delete this Course",
				"Error", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
				curCor.ID = "XXX";
				out.writeObject(curCor);
			}
			else{
				curCor.ID = "XXX";
				JOptionPane.showMessageDialog(null, "That Course does not Exist",
				"Error", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
				out.writeObject(curCor);
			}
			write = 1;
		}catch (Exception e)	{	System.out.println(e);	}
	}



	private void ChangeCourse()	{
		Course curCor = new Course();
		Vector Cors = new Vector(20,5);
		String Rmessage;
		Integer convert;
		int write = 1;
		int i = 0;
		try{
			Cors = (Vector)in.readObject();
			//System.out.print("Which Course will you change the info for?==>");
			Rmessage = Tdepthead.TEMP2;
			try	{
				while(i<2000) {
					curCor = (Course)Cors.get(i);
					if(curCor.ID.equals(Rmessage)) {
						write = 0;
						i=2300;
					}
					i++;
				}
			}catch (Exception e)	{}

			if(write == 0 && curCor.Dept.equals(ONEON.Dept) )	{
				//System.out.print("What is the new Teacher: ");
				Rmessage = Tdepthead.TEMP6;
				curCor.Teacher = Rmessage;
				//System.out.print("What is the new MaxEnroll: ");
				Rmessage = Tdepthead.TEMP4;
				convert = Integer.valueOf(Rmessage);
				curCor.MaxEnroll = convert.intValue();

				out.writeObject(curCor);
				JOptionPane.showMessageDialog(null, "Course Changed",
				"Course Changed", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
			}
			else	{
				JOptionPane.showMessageDialog(null, "Either Course DNE, or Improper Permissions",
				"Faculty Updated", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
				//System.out.println("Not Gonna Happen");
				curCor.ID = "XXX";
				out.writeObject(curCor);
			}
		}catch (Exception e)	{	System.out.println(e);	}
	}

	private void AddCourse(boolean which)	{
		String Rmessage,Smessage;
		Course insert = new Course();
		int write = 1;
		int i=0;
		int found = 0;
		Vector Cors0 = new Vector(20,5);
		Vector Cors1 = new Vector(20,5);
		Integer convert;
		Course curCor = new Course();
		try	{

			Cors1 = (Vector)in.readObject();
			Cors0 = (Vector)in.readObject();

			//System.out.print("What is the ID of the course(CSE230)==>");
			insert.ID = Tdepthead.TEMP2;
			if(which)	{
				//System.out.print("\nWhat is the Name of the course(no spaces)==>");
				insert.Name = Tdepthead.TEMP1;
				//System.out.print("\nWhat is the Last Name of the teacher(Manish)==>");
				insert.Teacher = Tdepthead.TEMP6;
				insert.Dept = new String(ONEON.Dept);
				//System.out.print("\nWhat is the Max Enroll for the course==>");
				Rmessage = Tdepthead.TEMP4;
				convert = Integer.valueOf(Rmessage);
				insert.MaxEnroll = convert.intValue();
				//System.out.print("\nIs the course Grad or Undergrad==>");
				Rmessage = Tdepthead.TEMP5;;
				if(Rmessage.startsWith("g")||Rmessage.startsWith("G") )	{
					insert.TypeCourse = true;
				}
				else	{
					insert.TypeCourse = false;
				}

				try	{
					while(i<2000) {
						curCor = (Course)Cors1.get(i);
						if(curCor.ID.equals(insert.ID)) {
							write = 0;
						}
						i++;
					}
				}catch (Exception e)	{}
			}
			else	{
				try	{
					while(i<2000) {
						curCor = (Course)Cors1.get(i);
						if(curCor.ID.equals(insert.ID)) {
							found = 1;
							insert = curCor;
							i=2300;
						}
						i++;
					}
				}catch (Exception e)	{}

				i=0;
				if(found == 1)	{
					try	{
						while(i<2000) {
							curCor = (Course)Cors0.get(i);
							if(curCor.ID.equals(insert.ID)) {
								write = 0;
							}
							i++;
						}
					}catch (Exception e)	{}
				}
				else{ write = 0;}
			}

			if(write == 1){
				out.writeObject(insert);
				JOptionPane.showMessageDialog(null, "Course Added",
				"Course Added", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
			}
			else{
				insert.ID = "XXX";
				JOptionPane.showMessageDialog(null, "Somethings Wrong...",
				"Error", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
				//System.out.println("Somethings Wrong..");
				out.writeObject(insert);
			}
			write = 1;
		}catch (Exception e)	{	System.out.println(e);	}
	}

	private void ListCourses()
	{
		int i=0;
		Vector Cors = new Vector(20,5);
		Course curCor = new Course();
		Tdepthead.TextArea.setText("");
		try	{
			Cors = (Vector)in.readObject();
			Tdepthead.TextArea.append("The Current Courses in your DEPT: \n");
			i=Cors.size()-1;
			while(i>=0)
			{
				curCor = (Course)Cors.get(i);
				if(curCor.Dept.equals(ONEON.Dept))	{
					Tdepthead.TextArea.append("ID: "+curCor.ID+" Name: "+curCor.Name+ " MaxEnroll: " +curCor.MaxEnroll
						+ " Grad/Under: ");
					if(curCor.TypeCourse)	{
						Tdepthead.TextArea.append("G\n");
					}
					else	{ Tdepthead.TextArea.append("U\n"); }

					Tdepthead.TextArea.append("   Dept: " +curCor.Dept + " Teacher: " + curCor.Teacher + "\n");
				}
				i--;
			}
		} catch(Exception e) { System.out.print(""); }
	}

		private void ListCatalog()
		{
			int i=0;
			Vector Cors = new Vector(20,5);
			Course curCor = new Course();
			Tdepthead.TextArea.setText("");
			try	{
				Cors = (Vector)in.readObject();
				Tdepthead.TextArea.append("The Current Course Catalog: \n");
				i=Cors.size()-1;
				while(i>=0)
				{
					curCor = (Course)Cors.get(i);
					//if(curCor.Dept.equals(ONEON.Dept))	{
						Tdepthead.TextArea.append("ID: "+curCor.ID+" Name: "+curCor.Name+ " MaxEnroll: " +curCor.MaxEnroll
							+ " Grad/Under: ");
						if(curCor.TypeCourse)	{
							Tdepthead.TextArea.append("G\n");
						}
						else	{ Tdepthead.TextArea.append("U\n"); }

						Tdepthead.TextArea.append("   Dept: " +curCor.Dept + " Teacher: " + curCor.Teacher + "\n");
					//}
					i--;
				}
			} catch(Exception e) { System.out.print(""); }
		}

		private void AssignAd()
		{
			Vector Stus = new Vector(20,5);
			String IDin = "";
			String AdvisorIn = "";
			Student curStu = new Student();
			int i = 0, found = 0;
			try	{
				Stus = (Vector)in.readObject();
				//System.out.print("What is the ID of the student who's Advisor will change?==>");
				IDin = Tdepthead.TEMP2;
				//System.out.print("Who will the student's new advisor be?==>");
				AdvisorIn = Tdepthead.TEMP1;
				i = Stus.size() - 1;
				while(i>=0)	 {
					curStu = (Student)Stus.elementAt(i);
					if(curStu.ID.equals(IDin))	{
						found = 1;
						i = -1;
						//System.out.println("YSY");
					}
					i--;
				}
				if(found != 1)	 {
					JOptionPane.showMessageDialog(null, "Student Not Found",
					"Error", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
					//System.out.println("The student could not be found.");
					curStu.ID = "XXX";
					out.writeObject(curStu);
				}
				else	{
					out.writeObject(curStu);
					out.flush();
					curStu.Advisor = AdvisorIn;
					out.writeObject(AdvisorIn);
					JOptionPane.showMessageDialog(null, "Student added to Advisor",
					"Added", JOptionPane.ERROR_MESSAGE, new ImageIcon("photo.jpg"));
				}
			}catch (Exception e)	{	System.out.println(e);	}
		}


public static String condition;

}
