using Aspose.Words;
using Aspose.Words.Tables;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorkFlowCheck.BL.DocumentGenerator
{
    public class DokumentumGenerator
    {
        static DokumentumGenerator()
        {
            Aspose.Words.License license = new Aspose.Words.License();

            try
            {
                license.SetLicense("Aspose.Words.lic");
                Log.Information("License set successfully.");
            }
            catch (Exception e)
            {
                Log.Error(e, "\nThere was an error setting the license: " + e.Message);
            }
        }

        public static byte[] HtmlToDocx(string html)
        {
            byte[] generated = Encoding.UTF8.GetBytes(html);
            var docHTML = new Aspose.Words.Document();

            Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(docHTML);
            builder.InsertHtml(html);

            docHTML.CompatibilityOptions.OptimizeFor(Aspose.Words.Settings.MsWordVersion.Word2016);

            MemoryStream outStream = new MemoryStream();
            docHTML.Save(outStream, Aspose.Words.SaveFormat.Docx);

            generated = outStream.ToArray();

            outStream.Dispose();

            return generated;
        }

        public static byte[] HtmlToPdf(string html)
        {
            byte[] generated = Encoding.UTF8.GetBytes(html);

            Stream inStream = new MemoryStream(generated);
            var docHTML = new Aspose.Words.Document(inStream);
            MemoryStream outStream = new MemoryStream();
            docHTML.Save(outStream, Aspose.Words.SaveFormat.Pdf);

            generated = outStream.ToArray();

            inStream.Dispose();
            outStream.Dispose();

            return generated;
        }

        public static byte[] DocxToPdf(byte[] generated)
        {
            Stream inStream = new MemoryStream(generated);
            var doc = new Aspose.Words.Document(inStream);
            MemoryStream outStream = new MemoryStream();
            doc.Save(outStream, Aspose.Words.SaveFormat.Pdf);

            generated = outStream.ToArray();

            inStream.Dispose();
            outStream.Dispose();

            return generated;
        }

        public static byte[] DocxToPdfForTopic(byte[] generated)
        {
            Stream inStream = new MemoryStream(generated);
            var doc = new Aspose.Words.Document(inStream);

            doc.Styles.DefaultFont.Name = "Times New Roman";
            doc.Styles.DefaultFont.Size = 12;

            var collection = doc.GetChildNodes(NodeType.Row, true);

            foreach (Row row in collection)
            {
                row.RowFormat.Height = 40;
                row.RowFormat.HeightRule = HeightRule.AtLeast;
            }

            MemoryStream outStream = new MemoryStream();
            doc.Save(outStream, Aspose.Words.SaveFormat.Pdf);

            generated = outStream.ToArray();

            inStream.Dispose();
            outStream.Dispose();

            return generated;
        }
    }
}
