using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<double> izdevumi = new List<double>();
        string[,] noliktava =
        {
            {"A101", "A102", "", "A104", null},
            {"B201", "", "B203", "B204", "B205"},
            {"", "", "", "C204", "C205"},
            {"D301", "D302", "D303", "", ""}
        };

        bool turpinat = true;
        while (turpinat)
        {
            Console.Clear();
            Console.WriteLine("1 - Budžeta analīze");
            Console.WriteLine("2 - Preču meklēšana");
            Console.WriteLine("3 - Attāluma kalkulators");
            Console.WriteLine("4 - Iziet");
            Console.Write("Ievadiet izvēli: ");
            string ievade = Console.ReadLine();

            switch (ievade)
            {
                case "1":
                    BudzetaAnalize(izdevumi);
                    break;
                case "2":
                    PrecuMeklesana(noliktava);
                    break;
                case "3":
                    AttalumaKalkulators();
                    break;
                case "4":
                    turpinat = false;
                    break;
                default:
                    Console.WriteLine("Nederīga izvēle!");
                    Console.ReadKey();
                    break;
            }
        }
    }

    static void BudzetaAnalize(List<double> izdevumi)
    {
        bool ievadeTurpinat = true;
        while (ievadeTurpinat)
        {
            Console.Clear();
            Console.WriteLine("Ievadiet izdevumu (vai beigt, lai beigtu):");
            string rinda = Console.ReadLine();
            if (double.TryParse(rinda, out double sk))
                izdevumi.Add(sk);
            else if (string.IsNullOrEmpty(rinda) || rinda.ToUpper() == "beigt")
                ievadeTurpinat = false;
        }

        Console.Clear();
        Console.WriteLine("Visi ievadītie izdevumi:");
        izdevumi.ForEach(Console.WriteLine);

        Console.WriteLine("Nospiediet Enter, lai filtrētu izmaksas...");
        while (Console.ReadKey().Key != ConsoleKey.Enter) { }

        List<double> filtr = izdevumi.Where(x => x > 35).ToList();
        Console.Clear();
        Console.WriteLine("Filtrētie izdevumi:");
        filtr.ForEach(Console.WriteLine);

        double diapazons = (filtr.Count == 0) ? 0 : filtr.Max() - filtr.Min();
        Console.WriteLine($"Izdevumu diapazons: {diapazons}");
        Console.WriteLine("Nospiediet Enter, lai atgrieztos izvēlnē.");
        while (Console.ReadKey().Key != ConsoleKey.Enter) { }
    }

    static void PrecuMeklesana(string[,] noliktava)
    {
        Console.Clear();
        Console.Write("Ievadiet preces kodu 1: ");
        string kods1 = Console.ReadLine();
        Console.Write("Ievadiet preces kodu 2: ");
        string kods2 = Console.ReadLine();

        Console.Clear();
        Console.WriteLine(AtrastPreci(noliktava, kods1));
        Console.WriteLine(AtrastPreci(noliktava, kods2));

        int tuksi = SaskaititTuksosPlauktus(noliktava);
        Console.WriteLine($"Tukšo plauktu skaits: {tuksi}");
        Console.WriteLine("Nospiediet Enter, lai turpinātu...");
        while (Console.ReadKey().Key != ConsoleKey.Enter) { }
    }

    static string AtrastPreci(string[,] noliktava, string kods)
    {
        for (int i = 0; i < noliktava.GetLength(0); i++)
            for (int j = 0; j < noliktava.GetLength(1); j++)
                if (noliktava[i, j] == kods)
                    return $"Prece atrasta: Eja {i + 1}, Plaukts {j + 1}";
        return "Prece nav atrasta";
    }

    static int SaskaititTuksosPlauktus(string[,] noliktava)
    {
        int sk = 0;
        foreach (var v in noliktava)
            if (string.IsNullOrEmpty(v)) sk++;
        return sk;
    }

    static void AttalumaKalkulators()
    {
        Console.Clear();
        Console.WriteLine("Ievadiet pirmā punkta x un y (atdaliet ar atstarpi):");
        string[] p1 = Console.ReadLine().Split(' ');
        double x1 = Convert.ToDouble(p1[0]);
        double y1 = Convert.ToDouble(p1[1]);

        Console.WriteLine("Ievadiet otrā punkta x un y (atdaliet ar atstarpi):");
        string[] p2 = Console.ReadLine().Split(' ');
        double x2 = Convert.ToDouble(p2[0]);
        double y2 = Convert.ToDouble(p2[1]);

        double distance = Math.Round(Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2)), 2);
        Console.Clear();
        Console.WriteLine($"Attālums starp punktiem: {distance}");
        Console.WriteLine("Nospiediet Enter, lai turpinātu...");
        while (Console.ReadKey().Key != ConsoleKey.Enter) { }
    }
}

