#include "menu.hpp"
#include "game.hpp"
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <windows.h>
#include <string>
using namespace std;
void options(string menu[][80], string option, int optionLength, int i);
int option = 1;
void menu()
{
    setColor(7);
    HideCursor(false);
    string menu[30][80];
    for (int H = 0; H < 30; H++)
    {
        for (int S = 0; S < 80; S++)
        {
            menu[H][S] = " ";
        }
    }
    menu[0][0] = "╔";
    menu[0][79] = "╗";
    menu[29][0] = "╚";
    menu[29][79] = "╝";
    for (int i = 1; i < 29; i++)
    {
        menu[i][0] = "║";
        menu[i][79] = "║";
    }
    for (int i = 1; i < 79; i++)
    {
        menu[0][i] = "═";
        menu[29][i] = "═";
    }
    options(menu, "New Game", 8, 3);
    options(menu, "Load Game", 9, 8);
    options(menu, "Game History", 12, 13);
    options(menu, "Help", 4, 18);
    // options(menu, "Settings", 8, 23);
    options(menu, "Exit", 4, 23);
    system("cls");
    while (true)
    {
        HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD cursorPosition = {0, 5};
        SetConsoleCursorPosition(h, cursorPosition);
        for (int i = 0; i < 30; i++)
        {
            cout << setw(45) << "";
            for (int j = 0; j < 80; j++)
            {
                if (option == 1)
                {
                    if (i > 2 && i < 6 && j > 31 && j < 48)
                    {
                        SetConsoleTextAttribute(h, 1);
                        cout << menu[i][j];
                        SetConsoleTextAttribute(h, 7);

                        continue;
                    }
                }
                else if (option == 2)
                {
                    if (i > 7 && i < 11 && j > 31 && j < 48)
                    {
                        SetConsoleTextAttribute(h, 1);
                        cout << menu[i][j];
                        SetConsoleTextAttribute(h, 7);

                        continue;
                    }
                }
                else if (option == 3)
                {
                    if (i > 12 && i < 16 && j > 31 && j < 48)
                    {
                        SetConsoleTextAttribute(h, 1);
                        cout << menu[i][j];
                        SetConsoleTextAttribute(h, 7);

                        continue;
                    }
                }
                else if (option == 4)
                {
                    if (i > 17 && i < 21 && j > 31 && j < 48)
                    {
                        SetConsoleTextAttribute(h, 1);
                        cout << menu[i][j];
                        SetConsoleTextAttribute(h, 7);

                        continue;
                    }
                }
                else if (option == 5)
                {
                    if (i > 22 && i < 26 && j > 31 && j < 48)
                    {
                        SetConsoleTextAttribute(h, 1);
                        cout << menu[i][j];
                        SetConsoleTextAttribute(h, 7);
                        continue;
                    }
                }
                // else
                // {
                //     if (i > 27 && i < 31 && j > 31 && j < 48)
                //     {
                //         SetConsoleTextAttribute(h, 1);
                //         cout << menu[i][j];
                //         SetConsoleTextAttribute(h, 7);

                //         continue;
                //     }
                // }
                cout << menu[i][j];
            }
            cout << endl;
        }
        while (true)
        {
            if (_kbhit())
            {
                char userInput = _getch();
                if ((userInput == 'w' || userInput == 'W') && option > 1)
                {
                    option -= 1;
                    break;
                }
                else if ((userInput == 's' || userInput == 'S') && option < 5)
                {
                    option += 1;
                    break;
                }
                if (userInput == 13)
                {
                    return;
                }
            }
        }
        Sleep(10);
    }
}
void options(string menu[][80], string option, int optionLength, int i)
{
    int sapce = (15 - optionLength) / 2;
    menu[i][32] = "╔";
    menu[i][47] = "╗";
    menu[i + 1][32] = "║";
    menu[i + 1][47] = "║";
    menu[i + 2][32] = "╚";
    menu[i + 2][47] = "╝";
    for (int width = 33; width < 47; width++)
    {
        menu[i][width] = "═";
        menu[i + 2][width] = "═";
    }
    for (int T = 0; T < optionLength; T++)
    {
        menu[i + 1][33 + sapce + T] = option.at(T);
    }
}
bool repeat(char name[])
{
    loadHistory();
    for (int i = 0; i < historyCount; i++)
    {
        if (strcmp(name, history[i].name) == 0)
        {
            return true;
        }
    }
    return false;
}
void newGame()
{
    Player = {};
    HideCursor(true);
    system("cls");
    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][79] = "╗";
    newGame[34][0] = "╚";
    newGame[34][79] = "╝";
    for (int i = 1; i < 34; i++)
    {
        newGame[i][0] = "║";
        newGame[i][79] = "║";
    }
    for (int i = 1; i < 79; i++)
    {
        newGame[0][i] = "═";
        newGame[34][i] = "═";
    }
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(h, {0, 5});
    for (int i = 0; i < 17; i++)
    {
        cout << setw(45) << "";
        for (int j = 0; j < 80; j++)
        {
            cout << newGame[i][j];
        }
        cout << endl;
    }
    SetConsoleCursorPosition(h, {45, 21});
    cout << "╚══════════════════════════════════════════════════════════════════════════════╝";
    SetConsoleTextAttribute(h, 10);
    SetConsoleCursorPosition(h, {81, 5});
    cout << "Breakout";

    while (Player.name[0] == 0 || repeat(Player.name))
    {
        if(repeat(Player.name))
        {
            SetConsoleCursorPosition(h, {64, 8});
            setColor(4);
            cout << "This name is alrady used!";
            _getch();
             SetConsoleCursorPosition(h, {64, 8});
             cout << "                         ";

        }

        SetConsoleTextAttribute(h, 7);
        SetConsoleCursorPosition(h, {47, 7});
        cout << "Enter your name: ";
        SetConsoleCursorPosition(h, {64, 7});
        cout << "                                                            ";
        SetConsoleCursorPosition(h, {64, 7});
        cin.getline(Player.name, 200);
    }

    // ══════════════════════════════════Breakout════════════════════════════════════
}
void help()
{
    setColor(7);
    HideCursor(false);
    string menu[30][80];
    for (int H = 0; H < 30; H++)
    {
        for (int S = 0; S < 80; S++)
        {
            menu[H][S] = " ";
        }
    }
    menu[0][0] = "╔";
    menu[0][79] = "╗";
    menu[29][0] = "╚";
    menu[29][79] = "╝";
    for (int i = 1; i < 29; i++)
    {
        menu[i][0] = "║";
        menu[i][79] = "║";
    }
    for (int i = 1; i < 79; i++)
    {
        menu[0][i] = "═";
        menu[29][i] = "═";
    }
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(h, {0, 5});
    for (int i = 0; i < 30; i++)
    {
        cout << setw(45) << "";
        for (int j = 0; j < 80; j++)
        {
            cout << menu[i][j];
        }
        cout << endl;
    }
    SetConsoleCursorPosition(h, {47, 7});
    cout << "\033[38;5;215m" << "1.Use " << "\033[38;5;11m" << "W/S" << "\033[38;5;215m" << " to move between options.";
    SetConsoleCursorPosition(h, {47, 9});
    cout << "\033[38;5;215m" << "1.Use " << "\033[38;5;11m" << "A/D" << "\033[38;5;215m" << " to move the Paddle.";
    SetConsoleCursorPosition(h, {47, 11});
    cout << "\033[38;5;215m" << "2.Press " << "\033[38;5;141m" << "Backspace" << "\033[38;5;215m" << " to back from loadGame menu.";
    SetConsoleCursorPosition(h, {47, 14});
    cout << "\033[38;5;33mB\033[38;5;82mR\033[38;5;51mI\033[38;5;196mI\033[38;5;201mK\033[38;5;226ms\033[38;5;215m:";
    SetConsoleCursorPosition(h, {47, 16});
    cout << "\033[38;5;33m1.Hard Brick: These bricks take two hits to break.";
    SetConsoleCursorPosition(h, {47, 18});
    cout << "\033[38;5;196m2.TNT Brick: explode and destroy nearby bricks";
    SetConsoleCursorPosition(h, {47, 20});
    cout << "\033[38;5;226m3.Golden brick: grant extra points when destroyed";
    SetConsoleCursorPosition(h, {47, 22});
    cout << "\033[38;5;215m4.Other colors are just for your eyes:)";
    cout << "\033[0m";
    _getch();
}
