using Telerik.Documents.Core.Fonts;

namespace ConsoleNetStandard
{
    public class FontsProvider : Telerik.Windows.Documents.Extensibility.FontsProviderBase
    {
        public override byte[] GetFontData(Telerik.Windows.Documents.Core.Fonts.FontProperties fontProperties)
        {
            string fontFileName = fontProperties.FontFamilyName + ".ttf";
            string fontFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);

            if (fontProperties.FontFamilyName == "Times New Roman")
            {
                if (fontProperties.FontStyle == FontStyles.Italic && fontProperties.FontWeight == FontWeights.Bold)
                {
                    fontFileName = $"timesbi.ttf";
                }
                else if (fontProperties.FontStyle == FontStyles.Italic)
                {
                    fontFileName = $"timesi.ttf";
                }
                else if (fontProperties.FontWeight == FontWeights.Normal)
                {
                    fontFileName = "times.ttf";
                }
                else if (fontProperties.FontWeight == FontWeights.Bold)
                {
                    fontFileName = $"timesbd.ttf";
                }
            }

            //...add more fonts if needed... 

            DirectoryInfo directory = new DirectoryInfo(fontFolder);
            FileInfo[] fontFiles = directory.GetFiles();

            var fontFile = fontFiles.FirstOrDefault(f => f.Name.Equals(fontFileName, StringComparison.InvariantCultureIgnoreCase));
            if (fontFile != null)
            {
                var targetPath = fontFile.FullName;
                using (FileStream fileStream = File.OpenRead(targetPath))
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        fileStream.CopyTo(memoryStream);
                        return memoryStream.ToArray();
                    }
                }
            }

            return null;
        }
    }
}
