using IWS5.Server.Utils.Converter.PdfConverter;
using IWS5.Server.Utils.Converter.TiffConverter;

namespace IWS5.Server.Utils.Converter;

internal static class ConverterFactory
{
    private static readonly Dictionary<Type, Lazy<IFileConverter>> LazyConverters = new()
    {
        { typeof(HtmlFileToPdfConverter), new Lazy<IFileConverter>(() => new HtmlFileToPdfConverter()) },
        { typeof(WordFileToPdfConverter), new Lazy<IFileConverter>(() => new WordFileToPdfConverter()) },
        { typeof(ImageFileToPdfConverter), new Lazy<IFileConverter>(() => new ImageFileToPdfConverter()) },
        { typeof(TiffFileToPdfConverter), new Lazy<IFileConverter>(() => new TiffFileToPdfConverter()) },
        { typeof(XmlFileToPdfConverter), new Lazy<IFileConverter>(() => new XmlFileToPdfConverter()) },
        { typeof(HtmlFileToTiffConverter), new Lazy<IFileConverter>(() => new HtmlFileToTiffConverter()) },
        { typeof(WordFileToTiffConverter), new Lazy<IFileConverter>(() => new WordFileToTiffConverter()) },
        { typeof(ImageFileToTiffConverter), new Lazy<IFileConverter>(() => new ImageFileToTiffConverter()) },
        { typeof(PdfFileToTiffConverter), new Lazy<IFileConverter>(() => new PdfFileToTiffConverter()) },
        { typeof(XmlFileToTiffConverter), new Lazy<IFileConverter>(() => new XmlFileToTiffConverter()) }
    };
    
    public static IFileConverter CreateConverter(string sourceFileExtension, string targetFileExtension)
    {
        foreach (var lazyConverter in LazyConverters.Values)
        {
            var converter = lazyConverter.Value;
            if (converter.CanConvert(sourceFileExtension, targetFileExtension))
            {
                return converter;
            }
        }
        
        return null;
    }
}