#include "sims.h"
#include <vector>

void CLISims::newstudent_menu(void) 
{
  cout << "-----------------------------" << endl;
  cout << " Add Student                 " << endl;
  cout << "-----------------------------" << endl;
  Student s;
  cin >> s;
  sc.insert(s);
  cout << "-----------------------------" << endl;
  cout << s;
  cout << "-----------------------------" << endl;
}

void CLISims::newcourse_menu(void) 
{
  string txtbook;
  cout << "-----------------------------" << endl;
  cout << " Add Course                  " << endl;
  cout << "-----------------------------" << endl;    
  Course c;
  Textbook t;
  cin >> c;

  cout << "Textbook ISBN [] : "; cin >> txtbook;
  cin.ignore();
  if( tc.contains(txtbook) ) {
    c.setTextbook(txtbook);
  } else {
    cin >> t;
    c.setTextbook(t.getISBN());
    tc.insert(t);
  }
  cc.insert(c);
  cout << "-----------------------------" << endl;
  cout << c;
  cout << "-----------------------------" << endl;
}

void CLISims::student_registration_menu(void) 
{
  string studentid;
  string courseid;
  string grade;
  cout << "-----------------------------" << endl;
  cout << " Add Student to Course       " << endl;
  cout << "-----------------------------" << endl;    
  cout << " Student ID: "; cin >> studentid;
  cin.ignore();
  if( !sc.contains(studentid) ) {
    cout << "Student not found in DB" << endl;
    return;
  }  
  Student s = sc.find(studentid);

  cout << "Course Number: "; cin >> courseid;
  cin.ignore();
  if( !cc.contains(courseid) ) {
    cout << "Course not found in DB" << endl;
    return;
  }

  Course c = cc.find(courseid);

  bool prereqs_satisfied = true;
  if(c.hasPrereqs() ) {
    cout << "Course has " << c.getNumPrereqs() << " prereqs" << endl;
    for(int i=c.getNumPrereqs();i>0;i--) {
      prereqs_satisfied = prereqs_satisfied && 
	(s.satisfiedPrereq(c.getPrereq(i)));
    }
  }

  bool course_open = c.isOpen();
  if( !course_open ) cout << "Course closed" << endl;

  if( prereqs_satisfied && course_open) { 
    s.setRegistration(courseid);
    sc.replace(studentid, s);
    cc.replace(courseid, c);
  } else {
    cout << "Unable to register for course" << endl;
  }
}

void CLISims::grade_menu(void) 
{
  string studentid;
  string courseid;
  string grade;
  cout << "-----------------------------" << endl;
  cout << " Add Grade for Student       " << endl;
  cout << "-----------------------------" << endl;
  cout << " Student ID: "; cin >> studentid;
  cin.ignore();
  if( !sc.contains(studentid) ) {
    cout << "Student not found in DB" << endl;
    return;
  }  
  Student s = sc.find(studentid);

  cout << "Course Number: "; cin >> courseid;
  cin.ignore();
  if( !cc.contains(courseid) ) {
    cout << "Course not found in DB" << endl;
    return;
  }

  cout << "Grade: "; cin >> grade;
  cin.ignore();
  s.setGrade(courseid,grade);

  sc.replace(studentid, s);
}

void CLISims::search_menu(void) 
{
  int    choice;
  string key;
  int    i;
  vector<Student>  vs;
  vector<Course>   vc;
  vector<Textbook> vt;
  cout << "-----------------------------" << endl;
  cout << " Search                      " << endl;
  cout << "-----------------------------" << endl;
  cout << "                             " << endl;
  cout << "  [1] Search Students        " << endl;
  cout << "  [2] Search Courses         " << endl;
  cout << "  [3] Search Textbooks       " << endl;
  cout << "-----------------------------" << endl;
  cout << "  Selection: "; cin >> choice;
  cout << "  Key: "; cin >> key;
  cin.ignore();
  switch(choice) {
  case 1: 
    vs = sc.search(key);
    vector<Student>::iterator is;
    if( vs.size() > 0 ) {
      cout << "-----------------------------" << endl;
      cout << "Select one: " << endl;
      for(i=1,is=vs.begin(); is!=vs.end(); is++)
	cout << "["<<i++<<"]"<< " " << is->getName() << endl;
      
      cout << "Choice: "; cin >> choice;
      cin.ignore();
      cout << "-----------------------------" << endl;
      cout << vs[choice-1] << endl;
    } else {
      cout << "Search returned no matches" << endl;
    }
    break;
  case 2: 
    vc = cc.search(key);
    vector<Course>::iterator ic;
    if( vc.size() > 0 ) {
      cout << "-----------------------------" << endl;
      cout << "Select one: " << endl;
      for(i=1,ic=vc.begin(); ic!=vc.end(); ic++)
	cout << "["<<i++<<"]"<< " " << ic->getName() << endl;
      
      cout << "Choice: "; cin >> choice;
      cin.ignore();
      cout << "-----------------------------" << endl;
      cout << vc[choice-1] << endl;
    } else {
      cout << "Search returned no matches" << endl;
    }
    break;
  case 3: 
    vt = tc.search(key);
    vector<Textbook>::iterator it;
    if( vt.size() > 0 ) {
      cout << "-----------------------------" << endl;
      cout << "Select one: " << endl;
      for(i=1,it=vt.begin(); it!=vt.end(); it++)
	cout << "["<<i++<<"]"<< " " << it->getTitle() << endl;
      
      cout << "Choice: "; cin >> choice;
      cin.ignore();
      cout << "-----------------------------" << endl;
      cout << vt[choice-1] << endl;
    } else {
      cout << "Search returned no mathes" << endl;
    }
    break;
  }
}

void CLISims::update_menu(void) 
{
  int      choice;
  string   key;
  Student  s;
  Course   c;
  Textbook t;
  cout << "-----------------------------" << endl;
  cout << " Update                      " << endl;
  cout << "-----------------------------" << endl;
  cout << "                             " << endl;
  cout << "  [1] Update Student         " << endl;
  cout << "  [2] Update Course          " << endl;
  cout << "  [3] Update Textbook        " << endl;
  cout << "-----------------------------" << endl;
  cout << "  Selection: "; cin >> choice;
  cout << "  ID: "; cin >> key;
  cin.ignore();
  switch(choice) {
  case 1: 
    s = sc.find(key);
    cin >> s;
    sc.replace(key,s);
    break;
  case 2: 
    c = cc.find(key);
    cin >> c;
    cc.replace(key,c);
    break;
  case 3: 
    t = tc.find(key);
    cin >> t;
    tc.replace(key,t);
    break;
  }
}

void CLISims::statistics_menu(void) 
{
  int choice;
  cout << "-----------------------------" << endl;
  cout << " Statistics                  " << endl;
  cout << "-----------------------------" << endl;
  cout << "  [1] Average GPA            " << endl;
  cout << "-----------------------------" << endl;
  cout << "  Selection: "; cin >>choice;
  cin.ignore();
  switch(choice) {
  case 1:
    cout << "AVERAGE = " << sc.computeAvgGPA() << endl;
    break;
  }
}

void CLISims::main_menu(void) 
{
  int choice;
  cout << "-----------------------------" << endl;
  cout << "   ***** *** *     * *****   " << endl;
  cout << "   *      *  **   ** *       " << endl;
  cout << "   *****  *  * * * * *****   " << endl;
  cout << "       *  *  *  *  *     *   " << endl;
  cout << "   ***** *** *     * *****   " << endl;
  cout << "-----------------------------" << endl;
  cout << "                             " << endl;
  cout << "  [1] Add New Student        " << endl;
  cout << "  [2] Add New Course         " << endl;
  cout << "  [3] Add Student to Course  " << endl;
  cout << "  [4] Assign Grade           " << endl;
  cout << "  [5] Search                 " << endl;
  cout << "  [6] Update                 " << endl;
  cout << "  [7] Statistics             " << endl;
  cout << "  [8] Quit                   " << endl;
  cout << "-----------------------------" << endl;
  cout << "  Selection: "; cin >> choice;
  cin.ignore();
  switch(choice) {
  case 1: 
    newstudent_menu();
    break;
  case 2:
    newcourse_menu();
    break;
  case 3:
    student_registration_menu();
    break;
  case 4:
    grade_menu();
    break;
  case 5:
    search_menu();
    break;
  case 6:
    update_menu();
    break;
  case 7:
    statistics_menu();
    break;
  case 8:
    exit(0);
    break;
  default:
    main_menu();
  }
  
  main_menu();
}

int main(void)
{
  CLISims ui;
  ui.main_menu();
}
