/* 
Veidoja : Roberts Dorbe
Grupa : 110
Praktiskais darbs 7
Izmantotais compiler : onlinegdb.com
*/

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // 1 uzd
        Console.WriteLine("1. uzd: Ievadiet izdevumus (vai gals, lai beigtu.)");
        var izdevumi = IevadietIzdevumus();

        Console.Write("Ievadiet minimālo slieksni: ");
        double slieksnis;
        while (!double.TryParse(Console.ReadLine(), out slieksnis))
            Console.Write("Nederīga ievade.");

        var filtrētie = FiltretIzdevumus(izdevumi, slieksnis);
        Console.WriteLine("Diapazons: " + AprekinatMainibu(filtrētie));

        // 2 uzd
        Console.WriteLine("\n2. uzd: Ievadiet preces kodu (piem. A1, C3):");

        string[,] noliktava =
        {
            { "A1","A2","","A4","A5" },
            { "B1","B2","B3","","B5" },
            { "C1","","C3","C4","C5" },
            { "D1","D2","D3","D4","" }
        };

        Console.Write("Kods: ");
        string kods = Console.ReadLine();

        Console.WriteLine(AtklatPreci(noliktava, kods));
        Console.WriteLine("Tukšo plauktu skaits: " + AprekinatNeaizpilditas(noliktava));
    }

    // 1 uzd metodes
    static List<double> IevadietIzdevumus()
    {
        var sar = new List<double>();

        while (true)
        {
            Console.Write("Ievade: ");
            string i = Console.ReadLine();

            if (i == "gals") break;

            if (double.TryParse(i, out double x))
                sar.Add(x);
            else
                Console.WriteLine("Nederīgs skaitlis!");
        }
        return sar;
    }

    static List<double> FiltretIzdevumus(List<double> dati, double slieksnis)
    {
        var jauns = new List<double>();
        foreach (double x in dati)
        {
            if (x < slieksnis) continue;
            jauns.Add(x);
        }
        return jauns;
    }

    static double AprekinatMainibu(List<double> d)
    {
        if (d.Count < 2) return 0;

        double max = double.MinValue, min = double.MaxValue;

        foreach (double x in d)
        {
            if (x > max) max = x;
            if (x < min) min = x;
        }

        return Math.Abs(max - min);
    }

    // 2 uzd metodes
    static string AtklatPreci(string[,] m, string kods)
    {
        for (int e = 0; e < m.GetLength(0); e++)
            for (int p = 0; p < m.GetLength(1); p++)
                if (m[e, p] == kods)
                    return $"Prece atrasta: Eja {e + 1}, Plaukts {p + 1}";

        return "Prece nav atrasta.";
    }

    static int AprekinatNeaizpilditas(string[,] m)
    {
        int sk = 0;
        for (int e = 0; e < m.GetLength(0); e++)
            for (int p = 0; p < m.GetLength(1); p++)
                if (string.IsNullOrEmpty(m[e, p])) sk++;
        return sk;
    }
}
