#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cassert>


#include "person.h"
#include "graph.h"
#include "message.h"

using std::ifstream;
using std::istream;
using std::ostream;
using std::cout;
using std::endl;

// -----------------------------------------------------
// helper function for reading quoted messages

string read_quoted_string(istream &istr) {
  string answer;
  istr >> answer;
  // first character of first word must be double quote
  assert (answer[0] == '"');

  // check to see if it's just a quoted word
  if (answer[answer.size()-1] == '"') 
    return answer;

  // read each subsequent word
  while (1) {
    string s;
    istr >> s;
    // append with spaces between
    answer += ' ' + s;
    // stop when a word ends with another double quote
    if (answer[answer.size()-1] == '"')
      return answer;
  } 
}


// -----------------------------------------------------

int main(int argc, char* argv[]) {
  
  // Open and check the input stream
  if (argc != 2) {
    cout << "Wrong number of arguments.\n";
    cout << "Usage:  " << argv[0] << " <graph-file>" << endl;
    return 1;
  }

  ifstream in_str(argv[1]);
  if (!in_str) {
    cout << "Bad file name: " << argv[1] << endl;
    return 1;
  }

  // seed the random number generator
  srand(time(NULL));

  // The main object
  Graph friend_graph;

  // temp variables for parsing
  string request, name1, name2;
  int count;
  
  // While there are more requests, determine the type of request and handle it. 
  while (in_str >> request) {

    // adding & removing people and adding friendship links
    if (request == "add_person") {
      in_str >> name1;
      if (friend_graph.add_person(name1))
        cout << "Added person " << name1 << endl;
      else
        cout << "Error trying to add person " << name1 << endl;
    } else if (request == "remove_person") {
      in_str >> name1;
      if (friend_graph.remove_person(name1))
        cout << "Removed person " << name1 << endl;
      else 
        cout << "Error trying to remove person " << name1 << endl;
    } else if (request == "add_friendship") {
      in_str >> name1 >> name2;
      if (friend_graph.add_friendship(name1, name2)) 
        cout << "Linked friends " << name1 << " and " << name2 << endl;
      else
        cout << "Error trying to link friends " << name1 << " and " << name2 << endl;
    } 

    // adding and passing messages
    else if (request == "add_message") {
      in_str >> name1;
      string msg = read_quoted_string(in_str);
      if (friend_graph.add_message(name1,msg)) 
        cout << "Added message " << msg << " to person " << name1 << endl;
      else
        cout << "Error trying to add message " << msg << " to person " << name1 << endl;
    } else if (request == "pass_messages") {
      friend_graph.pass_messages_randomly();
    } 

    // print the invitation list
    else if (request == "print_invite_list") {
      in_str >> name1 >> count;
      if (!friend_graph.print_invite_list(cout,name1,count))
        cout << "Error trying to print invitation list for person " << name1 << endl;
    } 

    // print a text version of the whole graph
    else if (request == "print") {
      cout << friend_graph;
    } else {
      cout << "Illegal input: " << request << endl;
    }
  }
  
  // The memory allocated for the graph is deleted when it goes out of scope.
  return 0;
}


