using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program
{
    class Program
    {
        //public static List<List<char>> Boxes = new List<List<char>>();
        public static List<List<(char, bool)>> Boxes = new List<List<(char, bool)>>();
        static void Main(string[] args)
        {
            string path = "../../../forrasok/dobozok.txt";
            var AllBoxes = File.ReadAllText(path).ToCharArray();

            //TaskA(AllBoxes);
            TaskBWithDefectiveBoxes(AllBoxes);

        }
        /*
        static void TaskA(char[] AllBoxes)
        {
            Boxes.Clear();

            foreach (char character in AllBoxes)
            {
                bool shouldIAddIt = true;

                foreach (var location in Boxes)
                {
                    if (CanContain(location.Last(), character))
                    {
                        location.Add(character);
                        shouldIAddIt = false;
                        break;
                    }
                }

                if (shouldIAddIt)
                {
                    Boxes.Add(new List<char> { character });
                }
            }

            Console.WriteLine($"Task A: Minimum number of wrapped packages: {Boxes.Count}");
        }

        */

        static void TaskBWithDefectiveBoxes(char[] AllBoxes)
        {
            Boxes.Clear();
            int maxActiveLocations = 0;
            int aCount = 0;
            int bCount = 0;

            foreach (char character in AllBoxes)
            {
                bool shouldIAddIt = true;
                bool isDefective = false;

                if (character == 'A')
                {
                    aCount++;
                    if (aCount % 25 == 0)
                    {
                        isDefective = true;
                    }
                }
                else if (character == 'B')
                {
                    bCount++;
                    if (bCount % 25 == 0)
                    {
                        isDefective = true;
                    }
                }

                foreach (var location in Boxes)
                {
                    if (CanContain(location.Last(), (character, isDefective)))
                    {
                        location.Add((character, isDefective));
                        shouldIAddIt = false;
                        break;
                    }
                }

                if (shouldIAddIt)
                {
                    Boxes.Add(new List<(char, bool)> { (character, isDefective) });
                }

                // Remove wrapped locations (those containing 'C')
                Boxes.RemoveAll(location => location.Any(box => box.Item1 == 'C'));
                Boxes.RemoveAll(location => location.Any(box => box.Item1 == 'B' && box.Item2 == true));

                maxActiveLocations = Math.Max(maxActiveLocations, Boxes.Count);
            }

            Console.WriteLine($"Task B with Defective Boxes: Maximum number of active packing locations: {maxActiveLocations}");
        }

        static bool CanContain((char, bool) outer, (char, bool) inner)
        {
            char outerType = outer.Item1;
            bool outerDefective = outer.Item2;
            char innerType = inner.Item1;
            bool innerDefective = inner.Item2;

            if (outerType == 'A')
            {
                if (outerDefective)
                {
                    return innerType == 'C';
                }
                else
                {
                    return innerType == 'B' || innerType == 'C';
                }
            }
            if (outerType == 'B')
            {
                if (outerDefective)
                {
                    return false;
                }
                else
                {
                    return innerType == 'C';
                }
            }
            return false;
        }

        /*
        static void TaskB(char[] AllBoxes)
        {
            Boxes.Clear();
            int maxActiveLocations = 0;

            foreach (char character in AllBoxes)
            {
                bool shouldIAddIt = true;

                foreach (var location in Boxes)
                {
                    if (CanContain(location.Last(), character))
                    {
                        location.Add(character);
                        shouldIAddIt = false;
                        break;
                    }
                }

                if (shouldIAddIt)
                {
                    Boxes.Add(new List<char> { character });
                }

                // Remove wrapped locations (those containing 'C')
                Boxes.RemoveAll(location => location.Contains('C'));

                maxActiveLocations = Math.Max(maxActiveLocations, Boxes.Count);
            }

            Console.WriteLine($"Task B: Maximum number of active packing locations: {maxActiveLocations}");
        }

        static bool CanContain(char outer, char inner)
        {
            if (outer == 'A' && (inner == 'B' || inner == 'C'))
                return true;
            if (outer == 'B' && inner == 'C')
                return true;
            return false;
        }

        */

        static void Feladat2(string path, char[] AllBoxes)
        {
            foreach (var character in AllBoxes)
            {

            }
        }

        /*
        static void Feladat1(string path, char[] AllBoxes)
        {
            
            foreach (char character in AllBoxes)
            {
                if (character == 'A')
                {
                    Boxes.Add(new List<char> { 'A' });
                    continue;
                }

                bool shouldIAddIt = false;
                int counter = 0;
                foreach (var hely in Boxes)
                {
                    if (character == 'B')
                    {
                        if (!hely.Contains('B') && !hely.Contains('C'))
                        {
                            hely.Add('B');
                            break;
                        }

                    }

                    if (character == 'C')
                    {
                        if (!hely.Contains('C'))
                        {
                            hely.Add('C');
                            break;
                        }
                    }
                    counter++;
                }

                if (counter == Boxes.Count)
                {
                    shouldIAddIt = true;
                }

                if (shouldIAddIt)
                {
                    Boxes.Add(new List<char> { character });
                }
            }



            foreach (var item in Boxes)
            {
                foreach (char character in item)
                {
                    Console.Write($"{character}");
                }
                Console.WriteLine();
            }

            Console.WriteLine($"1. feladat a: {Boxes.Count}");
        }

        */

    }
}