using Snake.Core;

namespace Snake.CLI;

internal static class ConsoleRenderer
{
    public static void Render(SnakeGame game)
    {
        Console.Clear();
        Console.WriteLine($"Score: {game.Score}  Level: {game.Level}");

        if (game.IsGameOver)
            Console.WriteLine("Game Over! Press R to restart or Q to quit.");

        var board = game.Board;
        var snakeCells = game.Snake.Segments.ToHashSet();
        var food = game.Food.Position;

        Console.Write('+');
        Console.Write(new string('-', board.Width * 2));
        Console.WriteLine('+');

        for (var y = 0; y < board.Height; y++)
        {
            Console.Write('|');

            for (var x = 0; x < board.Width; x++)
            {
                var position = new Position(x, y);
                var symbol = position == food
                    ? '@'
                    : position == game.Snake.Head
                        ? 'O'
                        : snakeCells.Contains(position)
                            ? 'o'
                            : ' ';

                Console.Write(symbol);
                Console.Write(' ');
            }

            Console.WriteLine('|');
        }

        Console.Write('+');
        Console.Write(new string('-', board.Width * 2));
        Console.WriteLine('+');
        Console.WriteLine("Controls: Arrow keys to move, R to restart, Q to quit.");
    }
}
