#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;

class GameData {
protected:
    string gameName;
    int hoursPerDay;
    int daysPerWeek;

public:
    GameData(string name = "", int hours = 0, int days = 0)
        : gameName(name), hoursPerDay(hours), daysPerWeek(days) {}

    void inputData() {
        cout << "Enter the game name: ";
        getline(cin, gameName);
        cout << "Hours played per day: ";
        cin >> hoursPerDay;
        cout << "Days played per week: ";
        cin >> daysPerWeek;
        cin.ignore();
    }

    string getGameName() const { return gameName; }
    int getHoursPerDay() const { return hoursPerDay; }
    int getDaysPerWeek() const { return daysPerWeek; }
    int getTotalHours() const { return hoursPerDay * daysPerWeek; }
};

class FileHandler : public GameData {
public:
    FileHandler(string name = "", int hours = 0, int days = 0)
        : GameData(name, hours, days) {}

    void saveGameDataToFile() {
        ofstream gameFile("game_report.txt", ios::app);
        if (gameFile) {
            gameFile.seekp(0, ios::end);
            if (gameFile.tellp() == 0) {
                gameFile << setw(15) << left << "Game Name"
                         << setw(10) << "Hours/Day"
                         << setw(10) << "Days/Week"
                         << setw(10) << "Total Hours" << endl;
                gameFile << "-----------------------------------------\n";
            }

            gameFile << setw(15) << left << gameName
                     << setw(10) << hoursPerDay
                     << setw(10) << daysPerWeek
                     << setw(10) << (hoursPerDay * daysPerWeek) << endl;
            gameFile.close();
            cout << "Game data saved successfully.\n";
        } else {
            cout << "Error saving game data.\n";
        }
    }
};

class GameReport : public FileHandler {
public:
    GameReport(string name = "", int hours = 0, int days = 0)
        : FileHandler(name, hours, days) {}

    void displayReport() {
        cout << "\nGame Report:\n";
        cout << "Game: " << gameName << "\n"
             << "Hours per Day: " << hoursPerDay << "\n"
             << "Days per Week: " << daysPerWeek << "\n"
             << "Total Hours: " << getTotalHours() << " hours\n";
    }

    // New method to calculate and display the average total hours
    static void calculateAverage() {
        ifstream gameFile("game_report.txt");
        if (!gameFile) {
            cout << "Error opening game_report.txt file.\n";
            return;
        }

        string line;
        int totalHours = 0, gameCount = 0;
        while (getline(gameFile, line)) {
            if (line.find("Game Name") != string::npos || line.empty()) continue;

            stringstream ss(line);
            string gameName;
            int hoursPerDay, daysPerWeek, totalGameHours;
            ss >> gameName >> hoursPerDay >> daysPerWeek;

            // Calculating Total Hours for the current game entry
            totalGameHours = hoursPerDay * daysPerWeek;

            // Checking if the calculated hours is valid
            if (totalGameHours > 0) {
                totalHours += totalGameHours;
                gameCount++;
            }
        }

        gameFile.close();
        if (gameCount > 0) {
            cout << "Average Total Hours played per game: " << totalHours / gameCount << " hours.\n";
        } else {
            cout << "No valid game data available to calculate average.\n";
        }
    }
};

int main() {
    GameReport player1;
    GameReport player2("Minecraft", 3, 5);

    cout << "\nManually Enter Data for Player 1:\n";
    player1.inputData();
    player1.saveGameDataToFile();
    player1.displayReport();

    cout << "\nSaving Preset Data for Player 2:\n";
    player2.saveGameDataToFile();
    player2.displayReport();

    // Calculate and display the average of total hours played
    GameReport::calculateAverage();

    return 0;
}
