package assign2startercode;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author macisac
 * @version 1.8.0
 * 
 */

public class MedicalClinic {

	Scanner in = new Scanner(System.in);
	
	private Appointment [] appointments; // the clinic's appointments 
	private Doctor [] doctors; //the clinic's doctor's currently on staff
	private Patient [] patients; //the clinic's patients
	
	//array size  
	private static final int maxAppointments = 5;
	private static final int maxPatients = 5;
	private static final int maxDoctors = 5;
	
	//used in the menu of choices
	private static final int addPatient = 1;
	private static final int addAppointment = 2;
	private static final int listAppointments = 3;
	private static final int quit = 4;
	
	//used as supporting functionality in other methods or to support debugging
	private static final int printPatients = 9;
	private static final int printDoctors = 10;
	
	//  counters to keep track of the number of array references instantiated - also serves as counter control variable when accessing array elements 
	private static int numberAppointments = 0;
	private static int numberPatients = 0;
	private static int numberDoctors = 0;
	
	
	public MedicalClinic() {
	
		// create memory space for the three arrays  
		appointments = new Appointment[maxAppointments];
		patients = new Patient[maxPatients];
		doctors = new Doctor [maxDoctors]; 
		
		// use some "test" data - just to get us started off
		doctors[0] = new Doctor("Vikash", "Singh", "endocrinologist");
		doctors[1] =  new Doctor("Susan", "Miller", "cardiologist");
		doctors[2] = new Doctor("Thanh", "Do", "neurologist");
		doctors[3] = new Doctor("Francois", "DaSilva", "internist");
		doctors[4] = new Doctor("Judy", "Chin", "Family Physician");
		numberDoctors = 5; //  because elements have been manually added, must also manually set numberDoctors
	
		patients[0] = new Patient("Josh", "Brolin", 874521, new OurDate(12, 04, 1987));
		patients[1] =  new Patient("Brack", "Dunstd", 6532147, new OurDate(12, 25, 2007));
		numberPatients = 2; // same as doctors above  
		
	}
	
	//repetitive code used when prompting user for a menu selection - make it a private method and invoke it in menu()
	private void menuOptions() {
		System.out.println("Please make a choice:  ");
		System.out.println("1.  Enter a new patient");
		System.out.println("2.  Make an appointment for a patient");
		System.out.println("3.  List all appointments");
		System.out.println(MedicalClinic.quit + "  Quit");
	}
	
	 //  interaction with the user
	public void menu() {
		
		//user's menu choice
		int choice;
		
		 // prompt once to get into the loop
		menuOptions();
		choice = in.nextInt();
		in.nextLine(); // consume last newline character of your input, otherwise next call to nextLine() will consume it 
		
		while (choice != quit) {
			
			switch(choice) {
			
			case addPatient :
				addPatient();
				break;
			case addAppointment:
				addAppointment();
				break;
			case listAppointments:
				listAppointments();
				break;
			case printDoctors:
				printDoctors();
				break;
			case printPatients:
				printPatients();
				break;
			default:
				System.out.println("iNVALID SELECTION");
			}
			
			//  prompt to stay in loop 
			menuOptions();
			
			choice = in.nextInt();
			in.nextLine();
		}// end loop - user has chosen to quit
	
	}//END menu()	
	
	// troubleshooting or auxiliary
	public void printDoctors() {
		
		for(int i = 0; i < numberDoctors; i++) {
			System.out.println((i+1) +".  "+doctors[i]);
		}
	}

	// troubleshooting or auxiliary 
	public void printPatients() {
		
		for(int i = 0; i < numberPatients; i++) {
			System.out.println(patients[i]);
		}
	}

	public void addPatient() {
		
		// helper local variables
		String bDate;
		int healthCardNumber;
		int day, month, year;
		
		//check to see if the patients array is max'd out 
		if(MedicalClinic.numberPatients == MedicalClinic.maxPatients) {
			System.out.println("Cannot add more patients");
		}
		else {			
			
			System.out.print("Enter first name: ");
			String firstName = in.nextLine();
			System.out.print("Enter last name: ");
			String lastName = in.nextLine();
			System.out.println();
			System.out.print("Enter health card number: ");
			healthCardNumber = in.nextInt();
			in.nextLine();
		
			//  first see if they are already in the database - only searching by health card field required - search by name in the future 
			boolean foundPatient = false;
			for(int i = 0; i < numberPatients; i++)
			{
				if(patients[i].getHealthCardNumber() == healthCardNumber) //  patient is found 
				{ 
					System.out.println(firstName +" " +lastName +"  is already in the system");
					foundPatient = true;  //   use this boolean to enter/not enter the if selection structure below 
					i = numberPatients; // get out of loop 
				}
			}
			
			if ( !foundPatient ) {  //the case of the user not already in the data base
				System.out.print("Enter birth date DDMMYYYY: ");
				bDate = in.nextLine();
				day = Integer.parseInt((bDate.substring(0, 2)));
				month = Integer.parseInt((bDate.substring(2, 4)));
				year = Integer.parseInt((bDate.substring(4, 8)));
				
				//basic data verification
				while ((day <= 0 || day > 31) && (month < 0 || month > 12)  && (year < 1900 || year > 2018)) { // valid patient dob goes to 1900, use Calendar to serve as the upper date limit 

					// inform user that they entered bad data 
					System.out.print("Invalid data: ");

					System.out.print("Enter birth date DDMMYYYY: ");
					bDate = in.nextLine();
					System.out.println();

					//extract day
					day = Integer.parseInt((bDate.substring(0, 2)));
					//extract month
					month = Integer.parseInt((bDate.substring(2, 4)));
					//extract year
					year = Integer.parseInt((bDate.substring(4, 8)));
				}// end data verification while loop 
				
				//  add the new patient 
				patients[numberPatients] = new Patient(lastName, firstName, healthCardNumber, new OurDate(day, month, year));
				
				//  increment the patient counter  
				numberPatients++;
			}//end adding patient to array
		}  //end the else condition that comes with detecting if the array has maximum elements
		
	} // END addPatient()
	
	public void addAppointment() {
		
		//helper local variable
		OurDate appointmentDate;
		
		if(MedicalClinic.numberAppointments == MedicalClinic.maxAppointments) {
			System.out.println("Cannot add more appointments");	
		}
		else
		{
			//  if there is room for appointments, then continue to collect patient data 
			System.out.print("Enter health card number: ");
			int hcn = in.nextInt();
			in.nextLine();
			
			int patientIndex = 0;
			boolean foundPatient = false;
			// search patient array for patient:  use health card number.
			for(int i = 0; i < numberPatients; i++)
			{
				if(patients[i].getHealthCardNumber() == hcn)
				{ 
					patientIndex = i;  // i is the position of the patient in the array 
					foundPatient = true;
					i = numberPatients; // get out of loop 
				}
			}
			
			//patient is found - collect data to make appointment
			if (foundPatient) {
				System.out.println(patients[patientIndex]); //  the variable patientIndex holds the position of the patient in the array 
				System.out.println();
				// print out the list of doctors, ask user for number option - just one of several methods that can be used to fetch doctor info 
				printDoctors();
				
				System.out.print("Enter number for doctor selection: ");
				int doctorSelection = in.nextInt(); // use this to serve as the index for the doctor array  
				in.nextLine();
				
				// get date of desired appointment  
				System.out.print("Enter desired appointment date DDMMYYYY: ");
				String date = in.nextLine();
				int day = Integer.parseInt((date.substring(0, 2)));
				int month = Integer.parseInt((date.substring(2, 4)));
				int year = Integer.parseInt((date.substring(4, 8)));
				
				//search for appointment conflict by date and doctor
				appointmentDate = new OurDate(day, month, year);
				
				// helper variable to determine next action taken if the appointment is already in database 
				boolean found = false;
				// verify that appointment does not have a doctor/date conflict 
				for (int k = 0; k < numberAppointments; k++) {

					if (appointments[k].getAppointmentDate().equals(appointmentDate) && appointments[k].getDoctor().equals(doctors[doctorSelection - 1])   ) {
						found = true;
						appointmentDate = null;
						k = numberAppointments;
						System.out.print("Doctor already has an appointment that day\n");	
					}
				}
				
				if (found == false) { // make the new appointment 

					appointments[numberAppointments] = new Appointment(patients[patientIndex], doctors[doctorSelection - 1],appointmentDate);

					numberAppointments++;
				} 
			} 	
			else {
				System.out.println("Patient not in clinic database - first add patient to database before an appointment is made");
			}
		}
	} //END addAppointment() 

	public void listAppointments() {
		if(numberAppointments  == 0) {
			System.out.println("No appointments ");
		}
		else {
			for(int i = 0; i < numberAppointments; i++) {
				System.out.println(appointments[i].toString());
		   } //end for loop 
		
		}  //end if(numApp  1= 0) 
   }//  END listPatients()

	
}//END CLASS

