using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;

namespace SpaceInvaders
{
    public class Menu
    {
        string[] menuOptions = { };
        int selectedOption = 0;
        public Menu()
        {
            menuOptions = new string[] {"  1. Jouer     ",
                                        "  2. Options   ",
                                        "  3. Highscore ",
                                        "  4. Résultats ",
                                        "  5. Quitter   " };
            selectedOption = 0;
        }

        public void Start()
        {
            

            while (true)
            {
                //Console.SetWindowSize(50, 30);
                Console.CursorVisible = false;
                Console.BackgroundColor = ConsoleColor.DarkMagenta;
                Console.Clear();
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkMagenta;

                Console.WriteLine("-----------------");
                Console.WriteLine("Spicy Invader  II");
                Console.WriteLine("-----------------");

                for (int i = 0; i < menuOptions.Length; i++)
                {
                    if (i == selectedOption)
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.BackgroundColor = ConsoleColor.DarkMagenta;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.BackgroundColor = ConsoleColor.Black;
                    }

                    Console.WriteLine(menuOptions[i]);
                }

                Console.ResetColor();

                ConsoleKeyInfo keyInfo = Console.ReadKey();
                if (keyInfo.Key == ConsoleKey.UpArrow)
                {
                    selectedOption = (selectedOption - 1 + menuOptions.Length) % menuOptions.Length;
                }
                else if (keyInfo.Key == ConsoleKey.DownArrow)
                {
                    selectedOption = (selectedOption + 1) % menuOptions.Length;
                }
                else if (keyInfo.Key == ConsoleKey.Enter)
                {
                    if (selectedOption == menuOptions.Length - 1)
                    {
                        break; // Sortir du boucle
                    }
                    else
                    {
                        PerformAction(selectedOption);

                        Console.ReadKey();
                    }
                }
            }
        }

        static void PerformAction(int option)
        {
            DatabaseConnection databaseConnection = new DatabaseConnection();
            switch (option)
            {
                case 0: //Jouer

                    Console.Write("Ingrese su nombre: ");
                    string playerName1 = Console.ReadLine();

                    Console.Clear();

                    Console.BackgroundColor = ConsoleColor.White;
                    ennemi monEnnemi = new ennemi(18, 0);
                    vaisseau monVaisseau = new vaisseau(18, 10);

                    jeu jeu = new jeu(monEnnemi, monVaisseau);
                    jeu.Start();

                    int finalScore = jeu.score;

                   
                  
                    databaseConnection.SaveScore(playerName1, finalScore);




                    break;

                case 1: //Optioons
                    Console.Clear();
                    Console.WriteLine("");
                    break;

                case 2:     //Highscore
                    Console.Clear();
                    Console.BackgroundColor = ConsoleColor.Magenta;
                   
                    var scores = databaseConnection.GetTop5Highscores();

                    Console.WriteLine("------- Highscores -------");
                    foreach (var (playerName, score) in scores)
                    {
                        Console.WriteLine($"{playerName}: {score}");
                    }

                    Console.WriteLine("\nAppuyez sur une touche pour retourner au menu");
                    

                    //Highscore();
                    break;

                case 3:
                    Console.Clear();
                    Console.WriteLine("");
                    break;

                case 4:
                    Console.ReadKey();
                    
                    break;

            }
        }
        public class DatabaseConnection
        {
            public string connectionString = "Server=localhost;Port=6033;Database=db_space_invaders;User Id=root;Password=root;";

            public void TestConnection()
            {
                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
                    try
                    {
                        connection.Open();
                        Console.WriteLine("Connexion OK");
                    }
                    catch (MySqlException ex)
                    {
                        Console.WriteLine($"Erreur {ex.Message}");
                    }
                }
            }
            public List<(string playerName, int score)> GetTop5Highscores()
            {
                List<(string playerName, int score)> highscores = new List<(string playerName, int score)>();

                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
                    try
                    {
                        connection.Open();

                        string query = "SELECT jouPseudo, jouNombrePoints FROM t_joueur ORDER BY jouNombrePoints DESC LIMIT 5";
 
                        MySqlCommand cmd = new MySqlCommand(query, connection);
                        using (MySqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                highscores.Add((reader.GetString(0), reader.GetInt32(1)));
                            }
                        }
                    }
                    catch (MySqlException ex)
                    {
                        Console.WriteLine($"Erreur {ex.Message}");

                    }
                }
                return highscores;
            }
            public void SaveScore(string playerName, int score)
            {
                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
                    try
                    {
                        connection.Open();

                        string query = "INSERT INTO t_joueur (jouPseudo, jouNombrePoints) VALUES (@playerName, @score)";
                        MySqlCommand cmd = new MySqlCommand(query, connection);
                        cmd.Parameters.AddWithValue("@playerName", playerName);
                        cmd.Parameters.AddWithValue("@score", score);

                        cmd.ExecuteNonQuery();
                    }
                    catch (MySqlException ex)
                    {
                        Console.WriteLine($"Erreur {ex.Message}");
                    }
                }
            }








        }
    }
}
