#include "menu.hpp"
#include "game.hpp"
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <Windows.h>
#include <conio.h>
#include <iomanip>
using namespace std;

bool GameOver();
bool run = false;
void signalHandler(int signum)
{
    if (run && Player.name != 0)
    {
        strcat(Player.name, "(auto save)");
        saveGame(Player);
    }
    exit(signum);
}
int main()
{
    signal(SIGINT, signalHandler);
    signal(SIGTERM, signalHandler);
#ifdef _WIN32
    signal(SIGBREAK, signalHandler);
#endif

    system("cls");
    while (true)
    {
        menu();
        switch (option)
        {
        case 1:
        {
            newGame();
            run = true;
            bool playAgain = false;
            int playCount = 1;
            do
            {
                if (playCount > 1)
                {
                    char temp[10]; // یک آرایه موقت برای نگه داشتن عدد
                    // تبدیل عدد به رشته و ریختن در temp
                    sprintf(temp, "%d", playCount);

                    // حالا چسباندن به آرایه اصلی
                    strcat(Player.name, temp);
                }
                while (!game())
                    ;
                if (pauseOption == 3)
                {
                    saveGame(Player);
                    run = false;
                    playAgain = false;
                }
                else if (pauseOption <= 2)
                {
                    saveHistory();
                    playAgain = GameOver();
                    if (playAgain)
                    {
                        run = true;
                        playCount++;
                    }
                }
            } while (playAgain);
            break;
        }
        case 2:
        {
            // printLoadGame();
            while (true)
            {
                loadPlayer();
                if (printLoadGame())
                {
                    run = true;
                    setAndMarkAsActive(loadOption);
                    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
                    system("cls");
                    SetConsoleCursorPosition(h, {0, 5});
                    render();
                    renderPanel();
                    _getch();
                    Beep(440, 200);
                    Sleep(50);
                    Beep(660, 200);
                    Sleep(50);
                    Beep(880, 400);
                    bool playAgain = false;
                    int playCount = 1;
                    do
                    {
                        if (playCount > 1)
                        {
                            char temp[10]; // یک آرایه موقت برای نگه داشتن عدد
                            // تبدیل عدد به رشته و ریختن در temp
                            sprintf(temp, "%d", playCount);

                            // حالا چسباندن به آرایه اصلی
                            strcat(Player.name, temp);
                        }
                        while (!game(2))
                            ;
                        if (pauseOption == 3)
                        {
                            saveGame(Player);
                            run = false;
                            playAgain = false;
                        }
                        else if (pauseOption <= 2)
                        {
                            saveHistory();
                            playAgain = GameOver();
                            if (playAgain)
                            {
                                run = true;
                                playCount++;
                            }
                        }
                    } while (playAgain);
                }
                else
                {
                    break;
                }
            }
            break;
        }
        case 3:
        {
            loadHistory();
            sort();
            while (true)
            {
                if (printHistory(historyCount))
                {
                    loadScoresAndPrint(history[Hoption - 1].name);
                    _getch();
                }
                else
                {
                    break;
                }
            }
            break;
        }
        case 4:
        {
            help();
            break;
        }
        case 5:
        {
            return 0;
        }
        }
    }
}
bool GameOver()
{
    HideCursor(false);
    setColor(11);
    system("cls");
    loadHistory();
    int High = sort();
    string newGame[35][80];
    for (int H = 0; H < 35; H++)
    {
        for (int S = 0; S < 80; S++)
        {
            newGame[H][S] = " ";
        }
    }
    newGame[0][0] = "╔";
    newGame[0][78] = "╗";
    newGame[34][0] = "╚";
    newGame[34][78] = "╝";
    for (int i = 1; i < 34; i++)
    {
        newGame[i][0] = "║";
        newGame[i][78] = "║";
    }
    for (int i = 1; i < 78; i++)
    {
        newGame[0][i] = "═";
        newGame[34][i] = "═";
    }
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(h, {0, 3});
    for (int i = 0; i < 17; i++)
    {
        cout << setw(45) << "";
        for (int j = 0; j < 79; j++)
        {
            cout << newGame[i][j];
        }
        cout << endl;
    }
    SetConsoleCursorPosition(h, {45, 20});
    cout << "╚═════════════════════════════════════════════════════════════════════════════╝";
    SetConsoleCursorPosition(h, {79, 4});
    cout << "Game Over";
    SetConsoleCursorPosition(h, {46, 5});
    cout << "═════════════════════════════════════════════════════════════════════════════";
    SetConsoleCursorPosition(h, {48, 8});
    cout << "╔═══════════════════╗";
    SetConsoleCursorPosition(h, {48, 9});
    cout << "║ Score: " << setw(5) << score;
    SetConsoleCursorPosition(h, {68, 9});
    cout << "║";
    SetConsoleCursorPosition(h, {48, 10});
    cout << "╚═══════════════════╝";
    SetConsoleCursorPosition(h, {48, 12});
    cout << "╔═══════════════════╗";
    SetConsoleCursorPosition(h, {48, 13});
    cout << "║ High: " << setw(6) << High;
    SetConsoleCursorPosition(h, {68, 13});
    cout << "║";
    SetConsoleCursorPosition(h, {48, 14});
    cout << "╚═══════════════════╝";
    SetConsoleCursorPosition(h, {90, 7});
    cout << "╔══════════════════════════════╗";
    SetConsoleCursorPosition(h, {90, 8});
    cout << "║         Top Players          ║";
    SetConsoleCursorPosition(h, {90, 9});
    cout << "║══════════════════════════════║";
    SetConsoleCursorPosition(h, {90, 10});
    cout << "║ " << left << setw(18) << history[0].name << right << setw(10) << history[0].score;
    SetConsoleCursorPosition(h, {121, 10});
    cout << "║";
    SetConsoleCursorPosition(h, {90, 11});
    cout << "║══════════════════════════════║";
    SetConsoleCursorPosition(h, {90, 12});
    cout << "║ " << left << setw(18) << history[1].name;
    history[1].score > 0 ? cout << right << setw(10) << history[1].score : cout << "";
    SetConsoleCursorPosition(h, {121, 12});
    cout << "║";
    SetConsoleCursorPosition(h, {90, 13});
    cout << "║══════════════════════════════║";
    SetConsoleCursorPosition(h, {90, 14});
    cout << "║ " << left << setw(18) << history[2].name;
    history[2].score > 0 ? cout << right << setw(10) << history[2].score : cout << "";
    SetConsoleCursorPosition(h, {121, 14});
    cout << "║";
    SetConsoleCursorPosition(h, {90, 15});
    cout << "╚══════════════════════════════╝";
    SetConsoleCursorPosition(h, {76, 17});
    cout << "╔═══════════════╗";
    SetConsoleCursorPosition(h, {76, 18});
    cout << "║               ║";
    SetConsoleCursorPosition(h, {78, 18});
    setColor(15);
    cout << "X: Play Again";
    setColor(11);
    SetConsoleCursorPosition(h, {76, 19});
    cout << "╚═══════════════╝";
    setColor(7);
    char input = _getch();
    if (input == 'X' || input == 'x')
    {
        return true;
    }
    return false;
}