using bdHerramientaCACOA.dbcontext;
using BlazorBootstrap;
using DinkToPdf;
using DinkToPdf.Contracts;
using DocumentFormat.OpenXml.Packaging;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.Text;
namespace HerramientaCASA
{
    public class ServicioPDF
    {
        private readonly IConverter _converter;
        private readonly IWebHostEnvironment _env;
        private readonly HttpClient _http;


        public ServicioPDF(IConverter converter, IWebHostEnvironment env, HttpClient http)
        {
            _converter = converter;
            _env = env;
            _http = http;
        }

        public  byte[] GenerarPdf(string html)
        {
            try
            {

                var bd = tsHerramientasCACOA.NuevoContexto(SoloLectura: false);

                string nombreColegio = bd.enumeraciones.First(x=> x.Codigo == "CW.NOMBRECOLEGIO").ValorAlfabetico1;

                string headerNombre = "Header" + nombreColegio + ".html";

                var doc = new HtmlToPdfDocument()
                {
                    GlobalSettings = {
                    PaperSize = PaperKind.A4,
                    Orientation = DinkToPdf.Orientation.Portrait,
                    Margins= new MarginSettings
                    {
                        Top = 11.8,
                        Left = 0,
                        Right = 0,
                        Bottom = 10
                    },
                },
                    Objects = {
                new ObjectSettings() {
                     HtmlContent = html,
                     HeaderSettings = new HeaderSettings
                     {
                         HtmUrl = Path.Combine(Environment.CurrentDirectory, "wwwroot", "Imprimir", headerNombre)
                         //HtmUrl = "https://casa3coacyle.cacoa.es/HeaderPdf"
                     },
                }
            }
                };

                return _converter.Convert(doc);
            }
            catch (Exception ex)
            {
                    throw new Exception(ex.Message);
            }
        }

        //public string obtenerURL()
        //{
        //    string url = "";
        //    var bd = tsHerramientasCACOA.NuevoContexto(SoloLectura: false);

         
        //        url = bd.enumeraciones.First(x => x.Codigo.Contains("CW.DOMINIO")).ValorAlfabetico1 + "/HeaderPdf";

        //    return url;
        //}
    }
}