using DinkToPdf;
using DinkToPdf.Contracts;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using PdfService.Utility;
using PdfService.Models;
namespace PdfService.Controllers
{

    
    [ApiController]
    public class PdfController : ControllerBase
    {
        private IConverter _converter;

        public PdfController(IConverter converter)
        {
            _converter = converter;
        }

        [Route("")]
        [HttpGet]
        public ActionResult GeneratePdfDemo()
        {
            var globalSettings = new GlobalSettings
            {
                ColorMode = ColorMode.Color,
                Orientation = Orientation.Portrait,
                PaperSize = PaperKind.A4,
                Margins = new MarginSettings { Top = 0, Left = 0, Right = 0 },
                DocumentTitle = "PDF Demo Report",
            };
 
            var objectSettings = new ObjectSettings
            {
                PagesCount = true,
                HtmlContent = TemplateGenerator.GetHTMLString(new Tweet[]{
                    new Tweet("Test", "Good"),
                    new Tweet("Test2", "Bad"),
                    new Tweet("Another Tweet", "Bad"),
                    new Tweet("Another bad Tweet", "Bad")
                    }),
                WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet =  Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css") }
            };
 
            var pdf = new HtmlToPdfDocument()
            {
                GlobalSettings = globalSettings,
                Objects = { objectSettings }
            };
 
            var file = _converter.Convert(pdf);
            return File(file, "application/pdf");
        }        

        // GET /generatePDF
        [Route("generatePDF")]
        [HttpPost]
        public ActionResult GeneratePdf([FromBody] Tweet[] tweets)
        {
            // check parameters
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            // check for emptyString
            foreach(Tweet tweet in tweets) {
                if(tweet.Name.Length<1 || tweet.Sentiment.Length<1){
                    return BadRequest(ModelState);
                }
            }

            var globalSettings = new GlobalSettings
            {
                ColorMode = ColorMode.Color,
                Orientation = Orientation.Portrait,
                PaperSize = PaperKind.A4,
                Margins = new MarginSettings { Top = 0, Left = 0, Right = 0 },
                DocumentTitle = "PDF Report",
            };
 
            var objectSettings = new ObjectSettings
            {
                PagesCount = true,
                HtmlContent = TemplateGenerator.GetHTMLString(tweets),
                WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet =  Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css") }
            };
 
            var pdf = new HtmlToPdfDocument()
            {
                GlobalSettings = globalSettings,
                Objects = { objectSettings }
            };
 
            var file = _converter.Convert(pdf);
            return File(file, "application/pdf");
        }

    }
}