// Include statements
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <ctime>

// Include custom header files
#include "Quiz.h"
#include "printResult.h"

// This is where I would declare my std namespace...
// IF I HAD ONE!

int main(int argc, char const *argv[]) { // Start main function

    // Enumerated lists to represent command-line arguments in a readable format for error checking and file name population
    enum expectedArguments {PROGRAM_EXECUTABLE, INPUT_FILE};

    // Check that the correct number of command-line arguments are present
    if (argc != 2) {
        std::cout << "ERROR: Program expects two (2) arguments." << std::endl;
        std::cout << "Expected format: \"./[PROGRAM_EXECUTABLE].[extension] [INPUT_FILE].[extension]\"" << std::endl;
        std::cout << "Aborting program." << std::endl;
        exit(-1); // Exit with error code
    }

    // Open input file of specified name (second command-line argument)
    std::ifstream inputFile;
    inputFile.open(argv[INPUT_FILE]);
    if (!(inputFile.is_open())) { // Error check opening inputFile
        std::cout << "ERROR: Could not open " << argv[INPUT_FILE] << "." << std::endl;
        std::cout << "Aborting program." << std::endl;
        exit(-1); // Exit with error code
    }

    // Create a vector of type Quiz
    std::vector<Quiz> quizVector;

    // Declare variables for use in while loop
    std::string question; // Holds question read from file to be passed to object constructor
    std::string answer; // Holds answer read from file to be passed to object constructor
    int numLinesRead = 0; // Iterator for the number of lines read in file

    // Read each line from inputFile
    std::string currentLine; // Holds the contents of the line being read
    while(getline(inputFile, currentLine)) { // While getline() does not fail (not end of file)
        // Search for question or answer
        if (currentLine.rfind("Q: ", 0) == 0) { // If line starts with "Q: "
            question = currentLine.substr(3); // Set question string to everything after the first space
        } else if (currentLine.rfind("A: ", 0) == 0) { // If line starts with "A: "
            answer = currentLine.substr(3); // Set answer string to everything after the first space
        }

        numLinesRead++; // Increment numLinesRead by 1

        if (numLinesRead % 2 == 0) { // If numLinesRead is divisible by two (meaning a question and answer have been read)
            quizVector.push_back(Quiz(question, answer)); // Add a Quiz object to quizVector using Quiz constructor
        }
    }

    // Randomize vector
    std::srand(unsigned(time(0)));
    std::random_shuffle(quizVector.begin(), quizVector.end());

    // Variables to be used in for loop
    int numQuestions = quizVector.size();
    int numCorrect = 0; // Holds number of correct answers
    int numWrong = 0; // Holds number of wrong answers

    // Loop through each object in quizVector
    for (int i = 0; i < numQuestions; i++) {
        std::string input; // Declare string to hold user input
        
        std::cout << quizVector[i].getQuestion() << std::endl; // Print question
        std::cout << "Type in your answer: "; // Prompt for answer
        getline(std::cin, input); // Get answer (including whitespace for multi-word answers) from user

        if (input == quizVector[i].getAnswer()) { // If input equals answer
            std::cout << "Correct!" << std::endl;
            numCorrect++; // Increment numCorrect
            Quiz::updateScore(1); // Add 1 to score
        } else { // If input doesn't equal answer
            std::cout << "Wrong! Correct answer: " << quizVector[i].getAnswer() << std::endl; // Print correct answer
            numWrong++; // Increment numWrong
            Quiz::updateScore(-1); // Add -1 to score
        }

        std::cout << "Current score: " << Quiz::getScore() << std::endl; // Print current score at the end of each question/answer loop
        std::cout << std::endl; // Print new line to separate blocks of text
    }

    std::cout << printResult(numQuestions, numCorrect, numWrong) << std::endl; // Print results (assembled string)

    inputFile.close(); // Close input file
    
    return 0; // Return end code

}