#include "Textbook.h"

Textbook::Textbook(void) : year(0), edition(0) {};
Textbook::Textbook(const Textbook& t) { *this = t; }
Textbook::Textbook(string t, string i, string p, string a, int y, int e):
  title(t), isbn(i), publisher(p), year(y), edition(e) {
  setAuthors(a);
}

inline string Textbook::getTitle()       const { return title; }
inline string Textbook::getISBN()        const { return isbn; }
inline string Textbook::getPublisher()   const { return publisher; }
inline string Textbook::getAuthor(int i) const { return authors[i]; }
inline int    Textbook::getYear()        const { return year; }
inline int    Textbook::getEdition()     const { return edition; }
inline int    Textbook::getNumAuthors()  const { return numauthors; }

bool Textbook::setAuthors(string a) {
  int i=0;
  Tokenizer t(a,',');
  while(t.hasMoreTokens()) {
    authors[++i] = t.getToken();
    t++;
  }
  numauthors = i;
  return true;
}
bool Textbook::setAuthors(string a[], int n) {
    if( (n < 1) || (n > 5) ) return false;
    numauthors = n;
    for(int i=1; i<=n; i++)
      authors[i] = a[i];
    return true;
  }

istream& operator>> (istream& in, Textbook& t)
{
  char* input = new char[50];
  string temp;

  cout << "Title [" << t.title <<"] : "; 
  in.get(input,50,'\n');
  in.ignore();
  temp = input;
  if( temp.length() > 0 ) t.title = temp;

  do { 
    cout << "Number of Authors: "; cin >> t.numauthors;
    in.ignore();
    if( t.numauthors > 5 ) {
      cout << "Please enter up to five authors" << endl;
      t.numauthors = 5;
    }
    if( t.numauthors < 1 )
      cout << "You must enter at least one author" << endl;
  } while( t.numauthors < 1 );

  for(int i=1; i<=t.numauthors; i++) {
    cout << "Author No. " << i << " [" << t.authors[i] << "] : ";
    in.get(input,50,'\n');
    in.ignore();
    temp = input;
    if( temp.length() > 0 ) t.authors[i] = temp;
  }


  do {
    cout << "ISBN [" << t.isbn << "] : ";
    in.get(input,50,'\n');
    in.ignore();
    temp = input;
    if( temp.length() == 10 ) t.isbn = temp;
    else if( temp.length() == 0 ) continue;
    else cout << "ERROR: ISBN must consist of 10 numbers" << endl;
  } while( (temp.length() != 10) && (t.isbn.length() == 0));

  cout << "Publisher [" << t.publisher << "] : ";
  in.get(input,50,'\n');
  in.ignore();
  temp = input;
  if( temp.length() > 0 ) t.publisher = temp;

  cout << "Year [" << t.year << "] : ";
  in.get(input,7,'\n');
  in.ignore();
  temp = input;
  if( temp.length() > 0 ) t.year = atoi(temp.c_str());

  cout << "Edition [" << t.edition << "] : ";
  cin.get(input,5,'\n');
  in.ignore();
  temp = input;
  if( temp.length() > 0 ) t.edition = atoi(temp.c_str());

  return in;
}

ostream& operator<< (ostream& out, Textbook& t)
{
  out << "T e x t b o o k" << endl;
  out << "Title: " << t.title << endl;
  for(int i=1; i<=t.numauthors; i++)
    cout << "Author No. " << i << " : " << t.authors[i] << endl;
  out << "ISBN: " << t.isbn <<endl;
  out << "Publisher: " << t.publisher << endl;
  out << "Year: " << t.year << endl;
  out << "Edition: " << t.edition << endl;

  return out;
}

const Textbook& Textbook::operator=(const Textbook& t) {
  if (this == &t) return *this;
  
  title = t.getTitle();
  isbn = t.getISBN();
  publisher = t.getPublisher();
  numauthors = t.getNumAuthors();
  for(int i=1; i<=numauthors; i++) {
    authors[i] = t.getAuthor(i);
  }
  year = t.getYear();
  edition = t.getEdition();
  
  return *this;
}

bool Textbook::operator== (const Textbook& t) {
  return( isbn == t.getISBN() );
}

bool Textbook::operator!= (const Textbook& t) {
  return( isbn != t.getISBN() );
}

string Textbook::toString(void)
{
  char* buf = new char[50];
  string s(isbn + ":" + title + ":");
  for(int i=1; i<=numauthors; i++) {
    s += authors[i];
    if(i!=numauthors) s += ",";
  }
  s += (":" + publisher + ":");

  sprintf(buf,"%d:%d",year,edition);
  string s2(buf);
  s += s2;
  return s;
}

Textbook Textbook::parse(string s)
{
  Tokenizer t(s,':');
  return( Textbook(t.getToken(1), 
	       t.getToken(0),
	       t.getToken(3),
	       t.getToken(2),
	       atoi(t.getToken(4).c_str()),
	       atoi(t.getToken(5).c_str())));
}
