/*
    Ingus Zemturis 110.
    PR 7.
    compilātors: 1. uzd https://www.onlinegdb.com/online_csharp_compiler
    2. & 3. uzd Visual Studio
*/
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;

class HelloWorld
{
    static List<double> IevadietIzdevumus(List<double> izejasDati) // 1. uzd
    {

        bool Expensive = true;
        double Expenses;
        string Confirmation;

        Console.WriteLine("ievadi izdevumus, nospied Enter taustiņu lai apstiprinātu, neieraksti ciparu un nospied Enter lai beigtu");
        while (Expensive == true)
        {
            Confirmation = Console.ReadLine();
            if (Double.TryParse(Confirmation, out Expenses))
            {
                izejasDati.Add(Expenses);
            }
            else
            {
                break;
            }

        }
        Console.Clear();
        return izejasDati;
    }

    static List<double> FiltretIzdevumus(List<double> izejasDati, double slieksnis) // 1. uzd
    {
        List<double> dati = new List<double>();
        for (int i = 0; i < izejasDati.Count; i++)
        {
            if (izejasDati[i] < slieksnis)
            {
                continue;
            }
            else
            {
                dati.Add(izejasDati[i]);
            }
        }
        return dati;
    }

    static double AprekinatMainibu(List<double> dati) // 1. uzd
    {
        double minimalistical = 999999999;
        double maximalical = -999999999;
        double YourMathematicalValue;
        if (dati.Count > 0)
        {
            for (int i = 0; i < dati.Count; i++)
            {
                if (dati[i] > maximalical)
                {
                    maximalical = dati[i];
                }
                if (dati[i] < minimalistical)
                {
                    minimalistical = dati[i];
                }
            }
            YourMathematicalValue = maximalical - minimalistical;
            return YourMathematicalValue;
        }
        return 4631;
    }

    static string AtklatPreci(string[,] noliktav, string kods) // 2. uzd
    {
        string ProducInfo = "prece nav atrasta";
        bool FoundProduct = false;
        if (string.IsNullOrEmpty(kods))
        {
            ProducInfo = "nav dots kods";
        }
        else
        {
            for (int row = 0; row < noliktav.GetLength(0); row++)
            {
                for (int shelf = 0; shelf < noliktav.GetLength(1); shelf++)
                {
                    if (noliktav[row, shelf] == kods)
                    {
                        ProducInfo = $"Prece atrasta Eja {row + 1} plaukts {shelf + 1}";
                        FoundProduct = true;
                        break;
                    }
                }
                if (FoundProduct == true)
                {
                    break;
                }
            }
        }

        return ProducInfo;
    }

    static int AprekinatNeaizpilditas(string[,] nolikta) // 2. uzd
    {
        int empty = 0;
        for (int Ydirectional = 0; Ydirectional < nolikta.GetLength(0); Ydirectional++)
        {
            for (int Xdirectional = 0; Xdirectional < nolikta.GetLength(1); Xdirectional++)
            {
                if (string.IsNullOrEmpty(nolikta[Ydirectional, Xdirectional]))
                {
                    empty += 1;
                }
            }
        }
        return empty;
    }
    static bool IsThatAchar(string? check)
    {
        if (string.IsNullOrEmpty(check))
        {
            return false;
        } else if (check.Length != 1)
        {
            return false;
        } else
        {
            return true;
        }
    }

    static double AprekinatPlaknesAttalumu()
    {
        (double x, double y) PointOne = (0, 0);
        (double x, double y) PointTwo = (0, 0);
        bool input = true;
        bool inputOne = true;
        bool inputTwo = true;
        bool inputExtra = true;
        bool inputExtraterrestrial = true;

        string? SafetyCheck = "";
        char? Confirmatinator = 'n';
        // spaghetti code begins vvvvv
        while (input)
        {
            Console.Clear();
            if (inputOne) // first point coordinate inputs are handled here
            {
                Console.WriteLine("ievadi pirmaa punkta x vertibu ");
                SafetyCheck = Console.ReadLine();
                double.TryParse(SafetyCheck, out PointOne.x);

                Console.WriteLine("ievadi pirmaa punkta y vertibu ");
                SafetyCheck = Console.ReadLine();
                double.TryParse(SafetyCheck, out PointOne.y);
                inputOne = false;
            }

            if (inputTwo) // the other point
            {
                Console.WriteLine("ievadi otraa punkta x vertibu ");
                SafetyCheck = Console.ReadLine();
                double.TryParse(SafetyCheck, out PointTwo.x);

                Console.WriteLine("ievadi otraa punkta y vertibu ");
                SafetyCheck = Console.ReadLine();
                double.TryParse(SafetyCheck, out PointTwo.y);
                inputTwo = false;
            }
            inputExtra = true; // to allow multiple rewrites
            Console.WriteLine($"\npirmaa punkta koordinaates ({PointOne.x};{PointOne.y})");
            Console.WriteLine($"otraa punkta koordinaates ({PointTwo.x};{PointTwo.y})");
            Console.WriteLine("vai tu gribi citas koordinaates?  y/n");
            SafetyCheck = Console.ReadLine();
            if (IsThatAchar(SafetyCheck)) // checks if the input was a character, from what i saw in chat gpt's rewrite of this mess there is a read char function but too late for that
            {
                Confirmatinator = Convert.ToChar(SafetyCheck);
            }
            if (Confirmatinator == 'n' || Confirmatinator == 'y')
            {
                inputExtraterrestrial = false;
            }
            while (inputExtraterrestrial) // more clear description if the input wasn't y or n
            {
                Console.Clear();
                Console.WriteLine($"\npirmaa punkta koordinaates ({PointOne.x};{PointOne.y})");
                Console.WriteLine($"otraa punkta koordinaates ({PointTwo.x};{PointTwo.y})");
                Console.WriteLine("ievadi y ja gribi citas koordinaates, n ja negribi citas koordinaates");
                SafetyCheck = Console.ReadLine();
                if (IsThatAchar(SafetyCheck))
                {
                    Confirmatinator = Convert.ToChar(SafetyCheck); // this loop only ends if y or n is inputted, with the check for that happening here
                    if (Confirmatinator == 'n' || Confirmatinator == 'y')
                    {
                        inputExtraterrestrial = false;
                    }
                }
            }
            if (Confirmatinator == 'n') // exit the input loop
            {
                input = false;
                Console.Clear();
            }
            if (Confirmatinator == 'y')
            {
                while (inputExtra) // goes on until a valid input is received
                {
                    Console.Clear();
                    Console.WriteLine("vai tu gribi citas punkta 1 punkta 2 vai abu punktu koordinaates? 1/2/3");
                    SafetyCheck = Console.ReadLine();
                    if (IsThatAchar(SafetyCheck))
                    {
                        Confirmatinator = Convert.ToChar(SafetyCheck);
                    }
                    if (Confirmatinator == '1')
                    {
                        inputOne = true;
                        inputExtra = false; // breaks out of the rewrite loop 
                        inputExtraterrestrial = true; // resets the secondary y/n confirmation part
                    }
                    else if (Confirmatinator == '2')
                    {
                        inputTwo = true;
                        inputExtra = false;
                        inputExtraterrestrial = true;
                    }
                    else if (Confirmatinator == '3')
                    {
                        inputOne = true;
                        inputTwo = true;
                        inputExtra = false;
                        inputExtraterrestrial = true;
                    }
                }
            }
        }
        // spaghetti code ends ^^^^^
        Console.Clear();
        double distance = 0;
        distance = Math.Sqrt(Math.Pow((PointTwo.x - PointOne.x), 2) + Math.Pow((PointTwo.y - PointOne.y), 2));
        return distance;
    }
    static double KonvertetRadianos(double gradi)
    {
        double pi = Math.PI;
        double radiani = gradi * (pi / 180);
        return radiani;
    }
    static void Main()
    {
        Thread.Sleep(250);
        // 1. uzd
        List<double> izejasDati = new List<double>();
        izejasDati = IevadietIzdevumus(izejasDati);

        string OtherConfirmation;
        double minimalizm;
        Console.WriteLine("ievadi minimālo izdevumu slieksni");
        OtherConfirmation = Console.ReadLine();
        if (double.TryParse(OtherConfirmation, out minimalizm))
        {
            Console.WriteLine($"diapazons: {AprekinatMainibu(FiltretIzdevumus(izejasDati, minimalizm))}");
        }
        Console.WriteLine("\n piespied taustinu enter lai turpinaatu");
        Console.ReadKey();
        Console.Clear();

        // 2. uzd
        string[,] noliktava =
        {
            {"aboli", "bumbieri", "", "apelsini", "zemenes", ""},
            {"galdi", "kresli", "pamatnes", "", "naglas", ""},
            {"krasaini zimuli", "", "kosas krasas", "", "", "flomasteri"}
        };
        string code;
        Console.WriteLine("ievadi precu kodu");
        code = Console.ReadLine();
        Console.WriteLine(AtklatPreci(noliktava, code));
        Console.WriteLine($"noliktavaa ir {AprekinatNeaizpilditas(noliktava)} tuksi plaukti");
        Console.WriteLine("\n piespied taustinu enter lai turpinaatu");
        Console.ReadKey();
        Console.Clear();

        // 3. uzd
        double distance = AprekinatPlaknesAttalumu();
        Console.WriteLine($"attalums starp abiem punktiem: {distance}");
        Console.Write("ievadi gradus ");
        int gradi = 0;
        bool loop = true;
        if (int.TryParse(Console.ReadLine(), out gradi) == false)
        {
            while (loop)
            {
                Console.Clear();
                Console.WriteLine("ievadi skaitli");
                if (int.TryParse(Console.ReadLine(), out gradi))
                {
                    loop = false;
                }
            }
        }
        double radians = KonvertetRadianos(gradi);
        Console.WriteLine($"{gradi} graados ir {radians} radiani");
        Console.WriteLine("\n piespied taustinu enter lai beigtu programmu");
        Console.ReadKey();
        Console.Clear();
    }
}