namespace ConsoleApp1;

class Program
{
    /**
     * Kristers Saltais
     * 31.03.2025 - 14:47
     */
    static void Main(string[] args)
    {
        string vards = "Kristers";
        PrintTable(vards, GenerateRandomArray(vards.Length, vards.Length, 1, 23));

        Console.WriteLine("\n\n");

        string uzvards = "Saltais";
        int[,] initialArray = GenerateRandomArray(vards.Length, vards.Length, 1, 23); // Pārgenerē skaidrības labad vai izmanto citu masīvu
        PrintTable(uzvards, TransposeArray(initialArray));
    }

    // izdrukā līniju tabulai
    static void PrintLine(int columnWidth, int numColumns)
    {
        for (int i = 0; i < numColumns; i++)
        {
            Console.Write("*");
            for (int j = 0; j < columnWidth; j++)
            {
                Console.Write("_");
            }
        }
        Console.Write("*\n");
    }

    // izdrukā tabulas rindu ar datiem, maksimālo, minimālo un summu
    static void PrintRow(int[] rowData, int max, int min, int sum)
    {
        foreach (int data in rowData)
        {
            Console.Write($"| {data,-2} "); // -2 nodrošina līdzinājumu pa kreisi un minimālo platumu
        }
        Console.WriteLine($"| {max,-2} | {min,-2} | {sum,-3} |");
    }

    // attēlo tabulas saturu
    static void PrintTable(string headerText, int[,] data)
    {
        int columns = data.GetLength(1);
        int columnWidth = 4; // Standarta kolonnas platums

        PrintLine(columnWidth, headerText.Length + 2); // Galvenes līnija
        Console.Write("|");
        foreach (char c in headerText)
        {
            Console.Write($" {c,-2} |");
        }
        Console.WriteLine($" min | max | sum |");
        PrintLine(columnWidth, headerText.Length + 2); // Apakšējā līnija zem galvenes

        for (int i = 0; i < data.GetLength(0); i++)
        {
            int min = int.MaxValue;
            int max = int.MinValue;
            int sum = 0;

            int[] currentRowData = new int[columns];
            for (int j = 0; j < columns; j++)
            {
                int value = data[i, j];
                sum += value;
                if (value > max) max = value;
                if (value < min) min = value;
                currentRowData[j] = value;
            }

            PrintRow(currentRowData, max, min, sum);
            PrintLine(columnWidth, columns + 2); // Līnija pēc katras rindas
        }
    }

    // ģenerē divdimensiju masīvu ar nejaušiem skaitļiem
    static int[,] GenerateRandomArray(int rows, int columns, int min, int max)
    {
        Random rand = new Random();
        int[,] array = new int[rows, columns];
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                array[i, j] = rand.Next(min, max + 1);
            }
        }
        return array;
    }

    // transponē divdimensiju masīvu
    static int[,] TransposeArray(int[,] array)
    {
        int rows = array.GetLength(0);
        int columns = array.GetLength(1);
        int[,] transposedArray = new int[columns, rows];

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                transposedArray[j, i] = array[i, j];
            }
        }
        return transposedArray;
    }
}
