/*
 Veidoja: Mikus Tolkačevs
Grupa: 110
Darbs: Atkartojums
Compiler: Visual Studio 2022
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp13
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            int rindas = ReadPositiveInt("Ievadiet rindu skaitu: ");
            int kolonnas = ReadPositiveInt("Ievadiet kolonnu skaitu: ");
            int[,] matrix = GenerateMatrix(rindas, kolonnas);
            Menu(matrix);
            PrintMatrix(matrix);
            FindMax(matrix);
            ColumnSums(matrix);
        }
        static int ReadPositiveInt(string zina)
        {
            int sk;
            bool cikls = true;
            do
            {
                Console.Write(zina);
                string nr = Console.ReadLine();
                if (int.TryParse(nr, out sk) && sk > 0)
                {
                    cikls = false;
                }
            } while (cikls);
            return sk;
        }
        static int[,] GenerateMatrix(int rindas, int kolonnas)
        {
            int[,] matrix = new int[rindas, kolonnas];
            Random matrica = new Random();
            for (int i = 0; i < rindas; i++)
            {
                for (int j = 0; j < kolonnas; j++)
                {
                    matrix[i, j] = matrica.Next(-100, 101);
                }
            }

            return matrix;
        }

        public static void Menu(int[,] matrix)
        {
            do
            {
                Console.WriteLine("Izvelieties darbibu:");
                Console.WriteLine("1-Izdrukat matricu");
                Console.WriteLine("2-Atrast lielako un mazako elementu");
                Console.WriteLine("3-Aprekinat katras kolonnas summu");
                Console.WriteLine("4-Atrast galvenas diognales summu");
                Console.WriteLine("5-Atrast blakusdiognales summu");
                Console.WriteLine("6-Transportet matricu");
                Console.WriteLine("7-Meklet skaitli matrica(lietotajs ievada skaitli)");
                Console.WriteLine("0-Iziet");
                Console.WriteLine("Uzrakstiet ciparu kuru darbibu velaties pildit:");
                int izvele = Convert.ToInt32(Console.ReadLine());

                switch (izvele)
                {
                    case 1:
                        PrintMatrix(matrix);
                        Console.WriteLine("Nospieziet Enter taustinu");
                        Console.ReadLine();
                        break;
                    case 2:
                        FindMax(matrix);
                        FindMin(matrix);
                        Console.WriteLine("Nospieziet Enter taustinu");
                        Console.ReadLine();
                        break;
                    case 3:
                        ColumnSums(matrix);
                        Console.WriteLine("Nospieziet Enter taustinu");
                        Console.ReadLine();
                        break;
                    case 4:
                        MainDiagonalSum(matrix);
                        Console.WriteLine("Nospieziet Enter taustinu");
                        Console.ReadLine();
                        break;
                    case 5:
                        SecondaryDiagonalSum(matrix);
                        Console.WriteLine("Nospieziet Enter taustinu");
                        Console.ReadLine();
                        break;
                    case 6:
                        Transpose(matrix);
                        Console.WriteLine("Nospieziet Enter taustinu");
                        Console.ReadLine();
                        break;
                    case 7:
                        Console.Write("Ievadiet skaitli: ");
                        int sk = Convert.ToInt32(Console.ReadLine());

                        var result = FindNumber(matrix, sk);

                        if (result.found)
                        {
                            Console.WriteLine($"Skaitlis {sk} atrasts [{result.row + 1}, {result.col + 1}].");
                        }
                        else
                        {
                            Console.WriteLine("Skaitlis nav matrica");
                        }

                        Console.WriteLine("Nospieziet Enter taustinu");
                        Console.ReadLine();
                        break;
                    case 0:
                        Console.WriteLine();
                        System.Environment.Exit(1);
                        Console.Clear();
                        break;

                }

            } while (true);

        }
        static void PrintMatrix(int[,] matrix)
        {
            int rindas = matrix.GetLength(0);
            int kolonnas = matrix.GetLength(1);
            for (int i = 0; i < rindas; i++)
            {
                for (int j = 0; j < kolonnas; j++)
                {
                    Console.Write("+----");
                }
                Console.WriteLine("+");
                for (int j = 0; j < kolonnas; j++)
                {
                    Console.Write($"|{matrix[i, j].ToString().PadLeft(4)}");

                }
                Console.WriteLine("|");
            }
            for (int j = 0; j < kolonnas; j++)
            {
                Console.Write("+----");
            }
            Console.WriteLine("+");

        }
        static void FindMax(int[,] matrix)
        {
            int rindas = matrix.GetLength(0);
            int kolonnas = matrix.GetLength(1);
            int sk = -101;
            for (int i = 0; i < rindas; i++)
            {
                for (int j = 0; j < kolonnas; j++)
                {
                    if (matrix[i, j] > sk)
                    {
                        sk = matrix[i, j];
                    }
                }

            }
            Console.WriteLine("Lielakais elements: " + sk);


        }
        static void FindMin(int[,] matrix)
        {
            int rindas = matrix.GetLength(0);
            int kolonnas = matrix.GetLength(1);
            int sk = 101;
            for (int i = 0; i < rindas; i++)
            {
                for (int j = 0; j < kolonnas; j++)
                {
                    if (matrix[i, j] < sk)
                    {
                        sk = matrix[i, j];
                    }
                }

            }
            Console.WriteLine("Lielakais elements: " + sk);


        }
        static void ColumnSums(int[,] matrix)
        {
            int rindas = matrix.GetLength(0);
            int kolonnas = matrix.GetLength(1);
            int[] rez = new int[kolonnas];
            for (int j = 0; j < kolonnas; j++)
            {
                int summa = 0;
                for (int i = 0; i < rindas; i++)
                {
                    summa += matrix[i, j];
                }
                rez[j] = summa;
                summa = 0;
            }
            Console.WriteLine("Kolonnu summas:");
            for (int j = 0; j < kolonnas; j++)
            {
                Console.WriteLine($"Kolonna {j + 1}: {rez[j]}");
            }

        }
        static void MainDiagonalSum(int[,] matrix)
        {
            int rindas = matrix.GetLength(0);
            int kolonnas = matrix.GetLength(1);
            int sk = 0;
            int summa = 0;
            if (rindas < kolonnas)
            {
                sk = rindas;
            }
            else
            {
                sk = kolonnas;
            }
            for (int i = 0; i < sk; i++)
            {
                summa += matrix[i, i];
            }
            Console.WriteLine("Diognales summa:" + summa);
        }
        static void SecondaryDiagonalSum(int[,] matrix)
        {
            int rindas = matrix.GetLength(0);
            int kolonnas = matrix.GetLength(1);
            int sk = 0;
            int summa = 0;
            if (rindas < kolonnas)
            {
                sk = rindas;
            }
            else
            {
                sk = kolonnas;
            }
            for (int i = 0; i < sk; i++)
            {
                summa += matrix[i, kolonnas - 1 - i];
            }
            Console.WriteLine("Blakusdiognales summa:" + summa);
        }
        static void Transpose(int[,] matrix)
        {
            int rindas = matrix.GetLength(1);
            int kolonnas = matrix.GetLength(0);
            for (int i = 0; i < rindas; i++)
            {
                for (int j = 0; j < kolonnas; j++)
                {
                    Console.Write("+----");
                }
                Console.WriteLine("+");
                for (int j = 0; j < kolonnas; j++)
                {
                    Console.Write($"|{matrix[j, i].ToString().PadLeft(4)}");

                }
                Console.WriteLine("|");
            }
            for (int j = 0; j < kolonnas; j++)
            {
                Console.Write("+----");
            }
            Console.WriteLine("+");
        }
        static (bool found, int row, int col)  FindNumber(int[,] matrix, int number)
        {
            int rindas = matrix.GetLength(0);
            int kolonnas = matrix.GetLength(1);
            for (int i = 0; i < rindas; i++)
            {
                for (int j = 0; j < kolonnas; j++)
                {
                    if (matrix[i, j] == number)
                    {
                        return (true, i, j);
                    }
                }
            }
            return (false, -1, -1);
        }
    }
}
    
