#include "bonus.h"


int main()
{
   bugtree bugs1;

   help();
   char cmd;
   do {
      cmd = getCommand();
      processCmd(cmd, bugs1);
   } while (cmd != Quit);

   // test out assignment of bug1 into bugs2
   cout << "Assigning the current tree to a new tree, then printing in it" << endl;
   bugtree bugs2;
   bugs2 = bugs1;
   bugs2.prtAll();

   cout << "Insert different entries into each tree, make sure they're separate" << endl;
   bugs2.insert("bug2xtra", "my new second tree bug", "Xfile");
   bugs1.insert("bug1xtra", "my new first tree bug", "Xfile");

   cout << "Original tree" << endl;
   bugs1.prtAll();
   cout << "Second tree" << endl;
   bugs2.prtAll();


}

void help()
{
   cout << "Available commands are:" << endl;
   cout << "   " << Help << " (this menu)" << endl;
   cout << "   " << Quit << " (end program)" << endl;
   cout << "   " << Print << " (print all bugs in tree)" << endl;
   cout << "   " << File << " (print all current-tree bugs relating to specific file)" << endl;
   cout << "   " << Insert << " (insert new bug into tree)" << endl;
   cout << "   " << Lookup << " (lookup bug in tree)" << endl;
}

char getCommand()
{
   char cmd;
   cout << "\nEnter your next command (from ";
   cout << Help << Quit << Print << File << Insert << Lookup;
   cout << ")" << endl;
   cin >> cmd;
   cmd = toupper(cmd);
   switch (cmd) {
      case Help:
      case Quit:
      case Print:
      case File:
      case Insert:
      case Lookup:
      case Remove:
         return cmd;
      default:
         cout << "Invalid command chosen: " << cmd << ", please try again" << endl;
         return getCommand();
   }
}

void processCmd(char cmd, bugtree &b)
{
   string name, desc, file, xtraNewline;
   bool found;

   switch (cmd) {
      case Help:
         help();
         break;

      case Quit:
         cout << "\nBye!\n" << endl;
         break;

      case Print:
         cout << "Contents of tree:" << endl;
         b.prtAll();
         cout << endl;
         break;

      case File:
         cout << "Enter the name of the file of interest (no spaces)" << endl;
         cin >> file;
         cout << "bugs related to " << file << ":" << endl;
         b.prtFile(file);
         break;

      case Insert:
         cout << "Enter the name of the new bug (no spaces)" << endl;
         cin >> name;
         getline(cin, xtraNewline); // gets rid of leftover newline after name
         cout << "Enter the bug description (one line)" << endl;
         getline(cin, desc);
         cout << "Enter the name of the file containing the bug (no spaces)" << endl;
         cin >> file;
         b.insert(name, desc, file);
         break;

      case Lookup:
         cout << "Enter the name of the bug of interest (no spaces)" << endl;
         cin >> name;
         found = b.lookup(name, desc, file);
         if (!found) {
            cout << "No bug named " << name << " found in tree" << endl;
         } else {
            cout << "Found: ";
            b.prtBug(name, desc, file);
            cout << endl;
         }
         break;

      case Remove:
         cout << "Enter the name of the bug of interest (no spaces)" << endl;
         cin >> name;
         found = b.remove(name);
         if (!found) {
            cout << "No bug named " << name << " found in tree" << endl;
         } else {
            cout << "Removed: " << name << endl;
         }
         break;


      default:
         cout << "Invalid command ignored: " << cmd << endl;
   }
}

