#include "lab5.h"


int main()
{
   bugtree bugs1;

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

}

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:
         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;


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

