#include <iostream>
#include <fstream>
#include <stdexcept>
#include <vector>

using namespace std;

#include "restaurant.h"
#include "tournament.h"
#include "io.h"

int main() {
    tournament t;
    bool keep_going = true;
    bool file_correct = false;
    string input, input_file_name;
    const string usage = "usage: iotest <input json file>";
    while(!file_correct) {
        cout << "please enter file name: ";
        getline(cin, input);
        ifstream inputs(input.c_str());
        if(inputs.good()) {
            input_file_name = input;
            file_correct = true;
        }
        else {
            cerr << "incorrect file name" << endl;
        }
    }
    t.populate_restaurants(input_file_name);
    bool succeful_pop = false;
    while(keep_going) {
        cout << "1)Display all restaurants\n2)Add a restaurant\n3)Remove a restaurant" << endl;
        cout << "4)Shufflle the array\n5)Begin the tournament" << endl;
        cin >> input;
        if(input[0] == '1'){
           cout << t << endl;
        }
        else if(input[0] == '2'){
            t.add_restaurants();
        }
        else if(input[0] == '3'){
            t.remove_restaurants();
        }
        else if(input[0] == '4'){
            t.shuffle();
        }
        else if(input[0] == '5'){
            if(t.verify_size()) {
                keep_going = false;
                succeful_pop = true;
            }
        }
        else if(input[0] == '0'){
            keep_going = false;
        }
    }
    if(succeful_pop) {
        t.start_tournament();
        string output_file_name;
        file_correct = false;
        while(!file_correct) {
            cout << "please enter file name: ";
            getline(cin, output_file_name);
            ofstream out(output_file_name.c_str());
            if(out.good()) {
                out << t;
                file_correct = true;
            }
            else {
                cerr << "incorrect file name" << endl;
            }
        }
    }
}
