using System.Text.RegularExpressions;
using DiunaBI.Domain.Entities;

namespace DiunaBI.Infrastructure.Services;

public static class ProcessHelper
{
    public static void SetValue(Record record, int number, double? value)
    {
        value = (double)Math.Round((decimal)(value ?? 0), 2);
        switch (number)
        {
            case 1:
                record.Value1 = value;
                break;
            case 2:
                record.Value2 = value;
                break;
            case 3:
                record.Value3 = value;
                break;
            case 4:
                record.Value4 = value;
                break;
            case 5:
                record.Value5 = value;
                break;
            case 6:
                record.Value6 = value;
                break;
            case 7:
                record.Value7 = value;
                break;
            case 8:
                record.Value8 = value;
                break;
            case 9:
                record.Value9 = value;
                break;
            case 10:
                record.Value10 = value;
                break;
            case 11:
                record.Value11 = value;
                break;
            case 12:
                record.Value12 = value;
                break;
            case 13:
                record.Value13 = value;
                break;
            case 14:
                record.Value14 = value;
                break;
            case 15:
                record.Value15 = value;
                break;
            case 16:
                record.Value16 = value;
                break;
            case 17:
                record.Value17 = value;
                break;
            case 18:
                record.Value18 = value;
                break;
            case 19:
                record.Value19 = value;
                break;
            case 20:
                record.Value20 = value;
                break;
            case 21:
                record.Value21 = value;
                break;
            case 22:
                record.Value22 = value;
                break;
            case 23:
                record.Value23 = value;
                break;
            case 24:
                record.Value24 = value;
                break;
            case 25:
                record.Value25 = value;
                break;
            case 26:
                record.Value26 = value;
                break;
            case 27:
                record.Value27 = value;
                break;
            case 28:
                record.Value28 = value;
                break;
            case 29:
                record.Value29 = value;
                break;
            case 30:
                record.Value30 = value;
                break;
            case 31:
                record.Value31 = value;
                break;
            case 32:
                record.Value32 = value;
                break;
        }
    }
    public static double? GetValue(Record record, int number)
    {
        return number switch
        {
            1 => record.Value1,
            2 => record.Value2,
            3 => record.Value3,
            4 => record.Value4,
            5 => record.Value5,
            6 => record.Value6,
            7 => record.Value7,
            8 => record.Value8,
            9 => record.Value9,
            10 => record.Value10,
            11 => record.Value11,
            12 => record.Value12,
            13 => record.Value13,
            14 => record.Value14,
            15 => record.Value15,
            16 => record.Value16,
            17 => record.Value17,
            18 => record.Value18,
            19 => record.Value19,
            20 => record.Value20,
            21 => record.Value21,
            22 => record.Value22,
            23 => record.Value23,
            24 => record.Value24,
            25 => record.Value25,
            26 => record.Value26,
            27 => record.Value27,
            28 => record.Value28,
            29 => record.Value29,
            30 => record.Value30,
            31 => record.Value31,
            32 => record.Value32,
            _ => null
        };
    }
    public static List<int> ParseCodes(string codes)
    {
        var codesList = new List<int>();
        foreach (var code in codes.Split(';'))
        {
            var range = code.Split('-');
            switch (range.Length)
            {
                case 1:
                    codesList.Add(int.Parse(range[0]));
                    break;
                case 2:
                    {
                        for (var i = int.Parse(range[0]); i <= int.Parse(range[1]); i++)
                        {
                            codesList.Add(i);
                        }

                        break;
                    }
                default:
                    throw new Exception($"Invalid code range: {code}");
            }
        }
        return codesList;
    }
    public static string? ExtractMonthFromLayerName(string layerName)
    {
        string pattern = @"L\d+-P-\d{4}/(\d{2})-";
        var match = Regex.Match(layerName, pattern);
        if (match.Success && match.Groups.Count > 1)
        {
            return match.Groups[1].Value;
        }
        return null;
    }
    public static string GetSheetName(int month, int year)
    {
        if (month < 1 || month > 12)
        {
            throw new ArgumentOutOfRangeException(nameof(month), "Month must be between 1 and 12.");
        }

        var polishMonths = new[]
        {
            "Styczen",
            "Luty",
            "Marzec",
            "Kwiecien",
            "Maj",
            "Czerwiec",
            "Lipiec",
            "Sierpien",
            "Wrzesien",
            "Pazdziernik",
            "Listopad",
            "Grudzien"
        };
        var monthName = polishMonths[month - 1];
        return $"{monthName}_{year}";
    }
}