#include "lab4.h"


int main()
{
   buglist bugs1;
   buglist* bugs2 = nullptr;

   help();
   char cmd;
   bool useBugs1 = true; // used to toggle between using bugs1 list and bugs2 list
   do {
      cmd = getCommand();
      processCmd(cmd, bugs1, bugs2, useBugs1);
   } while (cmd != Quit);

   if (bugs2) {
      delete bugs2;
   }
}

void help()
{
   cout << "Available commands are:" << endl;
   cout << "   " << Help << " (this menu)" << endl;
   cout << "   " << Quit << " (end program)" << endl;
   cout << "   " << Print << " (print all bugs in current list)" << endl;
   cout << "   " << File << " (print all current-list bugs relating to specific file)" << endl;
   cout << "   " << Insert << " (insert new bug into current list)" << endl;
   cout << "   " << Lookup << " (lookup bug in current list)" << endl;
   cout << "   " << Remove << " (remove bug from current list)" << endl;
   cout << "   " << Duplicate << " (create second bugs list as duplicate of first)" << endl;
   cout << "   " << Switch << " (switch between working on first/second lists)" << endl;
}

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

void processCmd(char cmd, buglist &b1, buglist* &b2, bool &useb1)
{
   string name, desc, file, xtraNewline;
   bool found;

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

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

      case Print:
         if (useb1) {
            cout << "Contents of list 1:" << endl;
            b1.prtAll();
            cout << endl;
         } else if (!b2) {
            cerr << "Error: no list currently allocated for list 2" << endl;
         } else {
            cout << "Contents of list 2:" << endl;
            b2->prtAll();
            cout << endl;
         }
         break;

      case File:
         if (!useb1 && !b2) {
            cerr << "Error: no list currently allocated for list 2" << endl;
         } else {
            cout << "Enter the name of the file of interest (no spaces)" << endl;
            cin >> file;
            if (useb1) {
               cout << "List 1 bugs related to " << file << ":" << endl;
               b1.prtFile(file);
            } else {
               cout << "List 2 bugs related to " << file << ":" << endl;
               b2->prtFile(file);
            }
         }
         break;

      case Insert:
         if (!useb1 && !b2) {
            cerr << "Error: no list currently allocated for list 2" << endl;
         } else {
            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;

            if (useb1) {
               b1.insert(name, desc, file);
            } else {
               b2->insert(name, desc, file);
            }
         }
         break;

      case Lookup:
         if (!useb1 && !b2) {
            cerr << "Error: no list currently allocated for list 2" << endl;
         } else {
            cout << "Enter the name of the bug of interest (no spaces)" << endl;
            cin >> name;
            if (useb1) {
               found = b1.lookup(name, desc, file);
               if (!found) {
                  cout << "No bug named " << name << " found in list 1" << endl;
               } else {
                  cout << "Found: ";
                  b1.prtBug(name, desc, file);
                  cout << endl;
               }
            } else {
               found = b2->lookup(name, desc, file);
               if (!found) {
                  cout << "No bug named " << name << " found in list 2" << endl;
               } else {
                  cout << "Found: ";
                  b2->prtBug(name, desc, file);
                  cout << endl;
               }
            }
         }
         break;

      case Remove:
         if (!useb1 && !b2) {
            cerr << "Error: no list currently allocated for list 2" << endl;
         } else {
            cout << "Enter the name of the bug of interest (no spaces)" << endl;
            cin >> name;
            if (useb1) {
               found = b1.remove(name);
               if (!found) {
                  cout << "No bug named " << name << " found in list 1" << endl;
               } else {
                  cout << "Removed bug " << name << endl;;
               }
            } else {
               found = b2->remove(name);
               if (!found) {
                  cout << "No bug named " << name << " found in list 2" << endl;
               } else {
                  cout << "Removed bug " << name << endl;;
               }
            }
         }
         break;

      case Duplicate:
         cout << "Making list2 a duplicate of list1" << endl;
         if (b2) {
            cout << "(deleting old list 2 before duplicating)" << endl;
            delete b2;
            b2 = nullptr;
         }
         b2 = new buglist(b1);
         break;

      case Switch:
          cout << "Switching to ";
          if (useb1) {
             cout << "list 2" << endl;
          } else {
             cout << "list 1" << endl;
          }
          useb1 = !useb1;
          break;

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

