// See https://aka.ms/new-console-template for more information
using Riceboard;

Console.WriteLine("Hello! I will now calculate the amount of rice grains on a chessboard.");
Console.WriteLine("The rules are:");
Console.WriteLine("|_ A grain of rice is placed upon the first position.");
Console.WriteLine("L_ The number of rice grains on each subsequent position is twice that of the previous position.");
Console.WriteLine("________________________________________________________________________________________________\n");

Console.WriteLine($"The number of grains on a standard chessboard is: [{Calculations.GetCount()}]");

Console.Write("Would you like to know the amount on a given chessboard position? [Y/N]: ");
var response = Console.ReadLine();

if (!string.IsNullOrWhiteSpace(response) && response.StartsWith("y", StringComparison.InvariantCultureIgnoreCase)) {
    bool repeat;
    do {
        repeat = false;

        var x = GetNumericalInputFromConsole("Please input your desired X position: ");
        var y = GetNumericalInputFromConsole("Please input your desired Y position: ");

        var width = 8;

        Console.Write("Would you like to also set a custom chessboard width? [Y/N]: ");
        response = Console.ReadLine();
        if (!string.IsNullOrWhiteSpace(response) && response.StartsWith("y", StringComparison.InvariantCultureIgnoreCase))
            width = GetNumericalInputFromConsole("Please input your desired chessboard width: ");

        while (x > width) {
            x -= width;
            y++;
        }

        Console.WriteLine($"The are {Calculations.GetCount(x-1, y-1, width)} grains of rice at position [{x},{y}]");

        Console.Write("\nWould you like to check a different position? [Y/N]: ");
        response = Console.ReadLine();

        if (!string.IsNullOrWhiteSpace(response) && response.StartsWith("y", StringComparison.InvariantCultureIgnoreCase))
            repeat = true;

    } while (repeat);
}

Console.Write("Would you like to know the total amount on a custom chessboard? [Y/N]: ");
response = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(response) && response.StartsWith("y", StringComparison.InvariantCultureIgnoreCase)) {
    bool repeat;

    do {
        repeat = false;
        var width = GetNumericalInputFromConsole("Please input your desired width: ");
        var height = GetNumericalInputFromConsole("Please input your desired height: ");

        Console.WriteLine($"There are {Calculations.GetCount(width, height)} grains of rice on a chessboard with dimensions [{width},{height}]");

        Console.Write("\nWould you like to check a differently sized chessboard? [Y/N]: ");
        response = Console.ReadLine();

        if (!string.IsNullOrWhiteSpace(response) && response.StartsWith("y", StringComparison.InvariantCultureIgnoreCase))
            repeat = true;
    } while (repeat);
}

Console.Write("\nThis concludes our display of immense skill.");
Console.ReadLine();

int GetNumericalInputFromConsole(string msg) {
    int result;
    var success = false;
    do {
        Console.Write(msg);
        response = Console.ReadLine();

        if (int.TryParse(response, out result)) success = true;
        else Console.WriteLine("Please input a valid numerical value.");
    } while (!success);

    return result;
}