/*
    Veidoja: Roberts Onzuls
    Grupa: 110
    Stundas darbs 11/12/2025
    Izmantotais compiler: www.onlinegdb.com
*/
using System;
using System.Collections.Generic;
using System.Linq;
class program
{
    static int[,] matrix;
    static void Main()
    {
        matrix = CreateMatrix();
        
        bool turpini = true;
        do
        {
            Console.Clear();
            Console.WriteLine("1. Izdrukāt matricu");
            Console.WriteLine("2. Atrast lielāko un mazāko elementu");
            Console.WriteLine("3. Aprēķināt katras kolonnas summu");
            Console.WriteLine("4. Atrest galvenās diognāles summu");
            Console.WriteLine("5. Atrast blakusdiognāles summu");
            Console.WriteLine("6. Transponēt matricu");
            Console.WriteLine("7. Meklēt skaitli matricā (lietotāja ievadītu)");
            Console.WriteLine("0. Iziet");
            Console.WriteLine("Lūdzu ievadiet savu izvēli: ");
            
            int x = Positive();
            
            switch (x)
            {
                case 1: PrintMatrix(matrix);
                        break;
                case 2: int max = FindMax(matrix);
                        int min = FindMin(matrix);
                        Console.Clear();
                        Console.WriteLine($"Lielākais elements: {max}");
                        Console.WriteLine($"Mazākais elements: {min}");
                        Console.WriteLine("Nospiediet Enter, lai turpinātu!");
                        while (Console.ReadKey().Key != ConsoleKey.Enter) {}
                        break;
                case 3: int[] sums = ColumnSums(matrix);
                        Console.Clear();
                        Console.WriteLine("Kolonnu summas:");
                        for (int i = 0; i < sums.Length; i++)
                        {
                            Console.WriteLine($"Kolonna {i + 1}: {sums[i]}");
                        }
                    
                        Console.WriteLine("Nospiediet Enter lai turpinātu!");
                        while (Console.ReadKey().Key != ConsoleKey.Enter) { }
                        break;
                case 4: int mainDiag = MainDiagonalSum(matrix);
                        Console.Clear();
                        Console.WriteLine($"Galvenās diagonāles summa: {mainDiag}");
                        Console.WriteLine("Nospiediet Enter lai turpinātu!");
                        while (Console.ReadKey().Key != ConsoleKey.Enter) {}
                        break;
                case 5: int secDiag = SecondaryDiagonalSum(matrix);
                        Console.Clear();
                        Console.WriteLine($"Blakusdiagonāles summa: {secDiag}");
                        Console.WriteLine("Nospiediet Enter lai turpinātu!");
                        while (Console.ReadKey().Key != ConsoleKey.Enter) {}
                        break;
                case 6: int[,] transposed = Transpose(matrix);
                        Console.Clear();
                        Console.WriteLine("Transponētā matrica:");
                        for (int i = 0; i < transposed.GetLength(0); i++)
                        {
                            for (int j = 0; j < transposed.GetLength(1); j++)
                                Console.Write($"\t{transposed[i,j]}\t");
                            Console.WriteLine();
                        }
                        Console.WriteLine("Nospiediet Enter lai turpinātu!");
                        while (Console.ReadKey().Key != ConsoleKey.Enter) {}
                        break;
                case 7: Console.Clear();
                        Console.Write("Ievadiet skaitli, ko meklēt: ");
                        int nr;
                        while (!int.TryParse(Console.ReadLine(), out nr)) {}
                    
                        var result = FindNumber(matrix, nr);
                    
                        if (result.found)
                            Console.WriteLine($"Skaitlis {nr} atrasts {result.row + 1}. rindā {result.col + 1}. kolonnā");
                        else
                            Console.WriteLine($"Skaitlis {nr} netika atrasts matricā.");
                    
                        Console.WriteLine("Nospiediet Enter lai turpinātu!");
                        while (Console.ReadKey().Key != ConsoleKey.Enter) {}
                        break;
                case 0: turpini = false;
                        break;
                default:Console.WriteLine("Ievadīta nederīga izvēle!");
                        break;
            }
        } while (turpini);
    }
    static void PrintMatrix(int[,] matrix)
    {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
    
        int maxLength = 0;
        foreach (int n in matrix)
            maxLength = Math.Max(maxLength, n.ToString().Length);
    
        Console.Clear();
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                Console.BackgroundColor = GetBackgroundColorOfSquare(i, j);
                Console.ForegroundColor = Console.BackgroundColor == ConsoleColor.White 
                    ? ConsoleColor.Black 
                    : ConsoleColor.White;
                Console.Write(matrix[i, j].ToString().PadLeft(maxLength + 2));
                Console.ResetColor();
            }
            Console.WriteLine();
        }
        Console.WriteLine("Nospiediet taustiņu Enter lai atgrieztos uzdevumu izvēlnē!");
        while (Console.ReadKey().Key != ConsoleKey.Enter) {}
    }
    static int FindMax(int[,] matrix)
    {
        int max = matrix.Cast<int>().Max();
        return max;
    }
    static int FindMin(int[,] matrix)
    {
        int min = matrix.Cast<int>().Min();
        return min;
    }
    static int[] ColumnSums(int[,] matrix)
    {
        int columns = matrix.GetLength(1);
        int rows = matrix.GetLength(0);
    
        int[] sums = new int[columns];
    
        for (int col = 0; col < columns; col++)
        {
            int sum = 0;
            for (int row = 0; row < rows; row++)
            {
                sum += matrix[row, col];
            }
            sums[col] = sum;
        }
    
        return sums;
    }
    static int MainDiagonalSum(int[,] matrix)
    {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
        int limit = Math.Min(rows, cols);
    
        int sum = 0;
        for (int i = 0; i < limit; i++)
            sum += matrix[i, i];
    
        return sum;
    }
    static int SecondaryDiagonalSum(int[,] matrix)
    {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
        int limit = Math.Min(rows, cols);
    
        int sum = 0;
        for (int i = 0; i < limit; i++)
            sum += matrix[i, cols - 1 - i];
    
        return sum;
    }
    static int[,] Transpose(int[,] matrix)
    {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
    
        int[,] transposed = new int[cols, rows];
    
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                transposed[j, i] = matrix[i, j];
    
        return transposed;
    }
    static (bool found, int row, int col) FindNumber(int[,] matrix, int number)
    {
        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                if (matrix[i, j] == number)
                    return (true, i, j);
            }
        }
    
        return (false, -1, -1);
    }
    static int Positive()
    {
        int sk;
            bool cikls = true;
            do
            {
                string nr = Console.ReadLine();
                if (int.TryParse(nr, out sk) && sk>=0)
                {
                    cikls = false;
                }
            } while (cikls);
            return sk;
    }
    static int[,] CreateMatrix()
    {
        int row = ReadPositiveNumber("Ievadiet rindu skaitu: ");
        int col = ReadPositiveNumber("Ievadiet kolonnu skaitu: ");
    
        Random rand = new Random();
        int[,] matrix = new int[row, col];
    
        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++)
                matrix[i, j] = rand.Next(-100, 101);
    
        return matrix;
    }
    static int ReadPositiveNumber(string msg)
    {
        int number;
        while (true)
        {
            Console.Write(msg);
            if (int.TryParse(Console.ReadLine(), out number) && number > 0)
                return number;
    
            Console.WriteLine("Lūdzu ievadiet pozitīvu veselu skaitli!");
        }
    }
    public static ConsoleColor GetBackgroundColorOfSquare(int row, int col)
    {
        return (row + col) % 2 == 0 ? ConsoleColor.Red : ConsoleColor.Blue;
    }
}