//
// Address Book Program     Goro Takei      revised 6/18/98
//
// This program keeps track of the name and address of your friends.
// It can search for the profile of your friend by doing a conditional search
//

#include <stdlib.h>
#include <fstream.h>
#include "profile.h"
#include "list.h"
#include "compare.h"
#include "perm.h"


// This file contains very short main() at the very end of this file.
// All the other functions are included in this file
// because if you change the class Profile, you may
// have to change these functions as well.

// "Menu()" is the main user interface for the program.
// It directs the commands to be executed after the
// user makes a selection.

// "SearchSelector()" is the user interface for search
// functions.

// "ChangeSelector()" is the user interface for change
// functions.

// "LoadList()" reads in the list of profiles from data file.

// "MakeProfile()" produces new profile.

// "AddNewProfile()" adds the profile created by "MakeProfile()"
// to the list.

void Menu();
List<Profile *> SearchSelector(List<Profile *> list);
void ChangeList(List<Profile *> &list);
void ChangeSelector(List <Profile *> &list, Profile &ProfToBeChanged);
List<Profile *> LoadList(char* ifname);
Profile* MakeProfile();
void AddNewProfile(List<Profile *> &list);



void Menu()
  {
    char choice[5];
    strcpy(choice," ");
    int val=0;
    int Exit=0;
//                                         declairs the list of profiles
    List<Profile *> list;
    List<Profile *> SearchResult;
    List<Profile *> readlist;

//                                         program reads in data from 
//                                         list.dat as default.
    char ifname_default[20]="list.dat";
    list=LoadList(ifname_default);
    cout<<".... read from inlist.dat"<<endl;

    while(Exit!=1)
      {
        cout<<endl<<endl;
        cout<<"<<<<<   Menu for Address Book  >>>>>"<<endl<<endl;
        cout<<"1.  Read in list"<<endl;
        cout<<"2.  Save list"<<endl;
        cout<<"3.  Display the whole list"<<endl;
        cout<<"4.  Sort the list by Last Names"<<endl;
        cout<<"5.  Change a profile"<<endl;
        cout<<"6.  Add a profile"<<endl;
        cout<<"7.  Remove a profile"<<endl;
        cout<<"8.  Search for a profile"<<endl;
        cout<<"9.  Search the search result"<<endl;
        cout<<"10. Sort the search result by Last Names"<<endl;    
        cout<<"11. Save search result"<<endl;
        cout<<"12. Recover original list"<<endl;
        cout<<"13.       Quit"<<endl;

        cout<<endl<<"Enter your choice >"<<endl;
        cin>>choice;
        val=atoi(choice);

        switch(val)
          {
            case 1:
              {
                char ifname[100];
                cout<<"Enter the input filename >"<<endl;
                cin>>ifname;
                readlist=LoadList(ifname);
                DisplayList(readlist);
                cout<<".... read from input file: "<<endl;
                list=readlist;
                break;
              }

            case 2:
              {
                char ofname[100];
                cout<<"Enter the output filename >"<<endl;
                cin>>ofname;
                SaveList(list,ofname);
                cout<<".... list saved"<<endl;
                break;
              }
    
            case 3:
              {
                DisplayList(list);
                break;
              }

            case 4:
              {
                list=SortList(list);
                break;
              }

            case 5:
              {
                ChangeList(list);
                break;
              }

            case 6:
              {
                AddNewProfile(list);
                break;
              }

            case 7:
              {
                RemoveProfile(list);
                break;
              }

            case 8:
              {
                SearchResult=SearchSelector(list);
                break;
              }

            case 9:
              {
                SearchResult=SearchSelector(SearchResult);
                break;
              }

            case 10:
              {
                SearchResult=SortList(SearchResult);
                break;
              }

            case 11:
              {
                char ofname[100];
                cout<<"Enter the output filename >"<<endl;
                cin>>ofname;
                SaveList(SearchResult,ofname);
                cout<<".... list saved"<<endl;
                break;
              }

            case 12:
              {
                list=readlist;
                break;
              }


            case 13:
              {      
                Exit=1;
                break;
              }

            default:
              {
                cout<<"Invalid Choice >"<<endl;
                break;
              }
           }
       }
  }

// "SearchSelector()" makes derived class of base, Compare 
// to search different field according to the type 
// of data you are comparing. 

// Some caution is drawn when reading in the desired
// string using get() or getline().  Sometimes, 
// the end-of-line character is read as first character
// and does not read the desired string.
// so I read those characters in junk.

List<Profile *> SearchSelector(List<Profile *> list)
  {
    List<Profile *> SearchResult;    	
    char choice=' ';
    int exit=0; 
    char ch=' ';
    char endofline='\n';
    char junk=cin.get();  // get '\n' of last entry
    int i=0;



    while(exit!=1)
      {
        cout<<endl<<"Search by what? >"<<endl;
        cout<<"1: First Name"<<endl;
        cout<<"2: Last  Name"<<endl;
        cout<<"3: Street Address"<<endl;
        cout<<"4: City"<<endl;
        cout<<"5: State"<<endl<<endl;

        cin>>choice;

        switch(choice)
          {
            case '1':
              {
                char LookString[100];
                cout<<"Who should I look for? >"<<endl;
                cin>>LookString;

                CompareFirstName Comp(LookString);     
                SearchResult=SearchList(Comp,list);
                exit=1;
                break;
              }

            case '2':
              {
                char LookString[100];
                cout<<"Who should I look for? >"<<endl;
                cin>>LookString;
                CompareLastName Comp(LookString);     
                SearchResult=SearchList(Comp,list);	
                exit=1;
                break;
              }

            case '3':
              {
                char LookString[100];
                cout<<"What street should I look for? >"<<endl;
                junk=cin.get();         // get '\n' of last entry
                while(ch!=endofline)
                {
                  ch=cin.get();
                  if(ch!=endofline) LookString[i]=ch; //if statement to avoide 
                  i++;                               //attatching '\n' at end 
                }
                CompareStreet Comp(LookString);     
                SearchResult=SearchList(Comp,list);
                exit=1;
                break;
              }
           
            case '4':
              {
                char LookString[100];
                cout<<"Who should I look for? >"<<endl;
                junk=cin.get();         // get '\n' of last entry
                while(ch!=endofline)
                {
                  ch=cin.get();
                  if(ch!=endofline) LookString[i]=ch; //if statement to avoide 
                  i++;                               //attatching '\n' at end 
                }
                CompareCity Comp(LookString);     
                SearchResult=SearchList(Comp,list);
                exit=1;
                break;
              }

            case '5':
              {
                char LookString[100];
                cout<<"Which state should I look for? >"<<endl;
                cin>>LookString;
                exit=1;
                break;
              }

            default:
              cout<<"Invalid Choice >"<<endl;
              break;
          
          }
      }

    cout<<endl<<"The result of the search was: "<<endl;
    DisplayList(SearchResult);
    return SearchResult;
  }



// "ChangeList()" asks for last name of person whose
// profile is to be changed.  It will confirm the user
// to change the profile, and changes the profile
// according to the user input.

void ChangeList(List<Profile *> &list)
  {
    char flag[100];
    char WhoseProfile[100];
    
    cin.getline(WhoseProfile,100);     //this is to get rid 
                                       //of '\n' of previous entry
    cout<<endl<<"Whose profile would you like to change? >"<<endl;
    cout<<"enter the last name of that person >";
    cin.getline(WhoseProfile,100);
    cout<<endl;

    for(ListIterator<Profile *> LI(list);LI.more();LI.advance())
      {  
//        strcmp returns integer of 0 if the two strings are the same.
//        otherwise it retruns some other interger.
        if(strcmp(LI.current()->GetLastName(),WhoseProfile)==0)
          {
            strcpy(flag,"");
            cout<<endl<<"Do you want to change this profile?"<<endl;
            LI.current()->Display();
            while((strcmp(flag,"y")!=0)&&(strcmp(flag,"n")!=0))
              {
                cout<<"Enter y or n >"<<endl;
                cin>>flag;
                cout<<"you entered "<<flag<<endl;
              }
            if(strcmp(flag,"y")==0)
              {
                ChangeSelector(list,*LI.current());
                cout<<"changed profile to:"<<endl;
                LI.current()->Display();
              }
           }
      }
 }


// "ChangeSelector()" has a menu where an user can select 
// the desired data type to be changed.  It promps to enter
// the new value for that data type and calls an appropriate
// data replacement function "ChangeXXX.Edit()".


void ChangeSelector(List <Profile *> &list, Profile &ProfToBeChanged)
  {
    int choice=0;
    int exit=0;
    char whose[100];
    int i=0;
    char ch=' ';
    char endofline='\n';
    char junk=cin.get();  // get '\n' of last entry


    while(exit!=1)
      {
        switch(choice)
          {
            case 1:
              {
                char NameTo[100];
                cout<<"Change first name to what? >"<<endl;
                cin>>NameTo;
                ChangeFirstName CH(NameTo);
// This is not using inheritance but for other algorithms,
// by passing CH as base type will be useful later on
                CH.Edit(ProfToBeChanged);
                exit=1;
                break;
              }

            case 2:
              {
                char NameTo[100];
                cout<<"Change last name to what? >"<<endl;
                cin>>NameTo;
                ChangeLastName CH(NameTo);
                CH.Edit(ProfToBeChanged);
                exit=1;
                break;
              }

            case 3:
              {

                char StreetTo[100];
                cout<<"Change street address to what? >"<<endl;
                junk=cin.get();  // get '\n' of last entry
                while(ch!=endofline)
                {
                  ch=cin.get();
                  if(ch!=endofline) StreetTo[i]=ch;       //if statement to avoide 
                  i++;                               //attatching '\n' at end 
                }

                ChangeStreet CH(StreetTo);
                CH.Edit(ProfToBeChanged);
                exit=1;
                break;
              }

            case 4:
              {

                char CityTo[100];
                cout<<"Change city to what? >"<<endl;
                junk=cin.get();         // get '\n' of last entry
                while(ch!=endofline)
                {
                  ch=cin.get();
                  if(ch!=endofline) CityTo[i]=ch;       //if statement to avoide 
                  i++;                               //attatching '\n' at end 
                }
                ChangeCity CH(CityTo);
                CH.Edit(ProfToBeChanged);
                exit=1;
                break;
              }

            case 5:
              {

                char StateTo[100];
                cout<<"Change state to what? >"<<endl;
                cin.getline(StateTo,100);                
                ChangeState CH(StateTo);
                CH.Edit(ProfToBeChanged);
                exit=1;
                break;
              }
            
           default:
              {
                cout<<"Which data do you want to change? >"<<endl;
                cout<<"1: First Name"<<endl;
                cout<<"2: Last  Name"<<endl;
                cout<<"3: Street Address"<<endl;
                cout<<"4: City"<<endl;
                cout<<"5: State"<<endl;
                cin>>choice;
                break;
              }
          }
      }
  }




// "LoadList()" reads in the profile list from 
// the input file.  The filename is passed from 
// the Menu() where it asks the user to input 
// the filename.

List<Profile *> LoadList(char * ifname)
  {
    List<Profile *> NewList;
    int TotNumProf=0;
    int ProfNum=0;;
    char FirstName[100];
    char LastName[100];
    char Strt[100];
    char City[100];
    char State[100];
    Profile *p=0;
    char junk[100];
    ifstream in(ifname);


    in.getline(junk,100);   //get text line, "Total Number 
    in>>TotNumProf;         //of Profiles" in input file 
    cout<<"Total Number of Profiles read: "<<TotNumProf<<endl;

    for(int i=0; i<TotNumProf;i++)
      {    
        ProfNum=0;
        in>>ProfNum;
        in.getline(junk,100);          //skip to next line after Prof#
        in.getline(FirstName,100);
        in.getline(LastName,100);
        in.getline(Strt,100);
        in.getline(City,100);
        in.getline(State,100);
        in.getline(junk,100);   //skip a line after last line of profile
        p=new Profile(ProfNum,FirstName,LastName,Strt,City,State);
        NewList.add(p);            
      }                      //creates new profile and adds to the list
    in.close();
    return NewList;
  }

// MakeProfile creates new profile from the 
// data inputted by the user.  The while statement
// used when reading Strt and City reads the line 
// of user entry.  Somehow getline() did not work for cin.

Profile* MakeProfile()
  {
    char FirstName[100];
    char LastName[100];
    char Strt[100];
    char City[100];
    char State[100];
    int ProfNum=0;

    cout<<"Enter the First Name >"<<endl;
    cin>>FirstName;
    cout<<"Enter the Last Name >"<<endl;
    cin>>LastName;
    cout<<"Enter the Street Address >"<<endl;

    int i=0;
    char ch=' ';
    char endofline='\n';
    char junk=cin.get();  // get '\n' of last entry
    while(ch!=endofline)
      {
        ch=cin.get();
        if(ch!=endofline) Strt[i]=ch;       //if statement to avoide 
        i++;                               //attatching '\n' at end 
      }

    cout<<"Enter the City >"<<endl;
    ch=' ';              // re-initialize flag character
    i=0;                 // re-initialize iterator index
    while(ch!=endofline)
      {
        ch=cin.get();
        if(ch!=endofline)  City[i]=ch;    //if statement to avoide 
        i++;                              //attatching '\n' at end 
      }
    cout<<"Enter the State >"<<endl;
    cin>>State;

    Profile *p=new Profile(ProfNum,FirstName,LastName,Strt,City,State); 
    cout<<"new profile made"<<endl;
    return p;
  }


// "AddNewProfile()" adds new profile made by
//  "MakeProfile()" at the end of the list.
// Notice the Profille # is updated right before
// "add()" because you don't know the how many is already in
// the list when creating the new profile.

void AddNewProfile(List<Profile *> &list)
  {
    Profile *p=MakeProfile();
    p->EditProfNum(list.NumElts()+1);
    list.add(p);
    cout<<"profile added:"<<endl;
    p->Display();          
  }





int main()
  {
    Menu();
    return 0;
}
