using System.IO;
using System.Collections.Generic;
using RTCommunicatorBase;

namespace RTCommunicatorTLV;

internal class HeaterTypeAnalizer
{
    public static bool Analize(string filePath, out Dictionary<byte, Dictionary<byte, HeaterType>> types)
    {
        types = default;
        if (string.IsNullOrEmpty(filePath))
            return false;

        if (!File.Exists(filePath))
            return false;

        try
        {
            using var reader = new StreamReader(filePath);
            string line;
            types = [];
            while ((line = reader.ReadLine()) is not null)
            {
                string[] cache = line.Split(',');
                if (cache.Length != 3)
                    continue;

                if (!byte.TryParse(cache[0], out byte mini8Index))
                    continue;
                if (!byte.TryParse(cache[1], out byte channelIndex))
                    continue;
                HeaterType heaterType = cache[2] switch
                {
                    "GaseLine" => HeaterType.GaseLine,
                    "ForeLine" => HeaterType.ForeLine,
                    _ => HeaterType.Undefined,
                };

                if (!types.TryGetValue(mini8Index, out Dictionary<byte, HeaterType> heaters) || heaters is null)
                {
                    heaters = [];
                    types[mini8Index] = heaters;
                }

                heaters[channelIndex] = heaterType;
            }
        }
        catch
        {
            return false;
        }


        return true;
    }


}