using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PhotoTkutimes.Models;
using System.Globalization;
using iText.IO.Image;
using iText.Kernel.Colors;
using iText.Kernel.Font;

using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using Document = iText.Layout.Document;
using Paragraph = iText.Layout.Element.Paragraph;
using DocumentFormat.OpenXml.Wordprocessing;
using iText.IO.Font;

namespace PhotoTkutimes.Controllers
{
    [Route("fpdf/")]
    [ApiController]
    public class PdfController : ControllerBase
    {
        private readonly IConfiguration _configuration;
        public PdfController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        [HttpGet("{no}.pdf")]
        public IActionResult Get(int no)
        {
            if (_configuration.GetValue<int>("PdfControllerEnabled") != 1)
            {
                return NotFound();
            }

            DtlPageData pageData = new DtlPageData();
            pageData = DtlPageData.getDtlPageData(_configuration, no, "");

            string _title = "";
            string _paperno = "";
            string _type = "";
            string _dt = "";
            string _content = "";
            char[] c = { '\n' };
            if (pageData.dtlNews.Count > 0)
            {
                _title = pageData.dtlNews[0].Title1;
                _paperno = string.Format("淡江時報 第 {0} 期", pageData.dtlNews[0].News_no);
                _type = pageData.dtlNews[0].PgTyp;
                //_dt = DateTime.Now.ToString("yyyyMMddHHmmss");
                _dt = pageData.dtlNews[0].DMdf.ToString("yyyyMMddHHmmss");
                if (pageData.dtlNews[0].news_content != null)
                {
                    _content = pageData.dtlNews[0].news_content;
                    //_content = content;
                }

            }

            string pdfpath = string.Format("{0}_{1}.pdf", no, _dt);
            string filepath = string.Format("{1}{0}", pdfpath, Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/pdf/"));
            string fontApath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/font/", "edukai-4.0.ttf"); // 教育部標楷體

            if (System.IO.File.Exists(filepath))
            {
                return File(Path.Combine("pdf", pdfpath), "application/pdf");
            }
            try
            {
                using (PdfWriter writer = new PdfWriter(filepath))
                {
                    PdfDocument pdf = new PdfDocument(writer);
                    Document document = new iText.Layout.Document(pdf);
                    PdfFont fontA = PdfFontFactory.CreateFont(fontApath, PdfEncodings.IDENTITY_H);
                    // PdfFont fontB = PdfFontFactory.CreateFont(fontBpath, PdfEncodings.IDENTITY_H);
                    //PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
                    document.Add(new Paragraph(_paperno).SetFontSize(14).SetFont(fontA).SetFontColor(ColorConstants.RED).SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT));

                    document.Add(new Paragraph(_title).SetFont(fontA).SetFontSize(16));
                    document.Add(new Paragraph(_type).SetFont(fontA).SetFontSize(14).SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT).SetFontColor(ColorConstants.GRAY));
                    document.Add(new Paragraph(_content).SetFont(fontA).SetFontSize(14));
                    if (pageData.dtlImg != null && pageData.dtlImg.Count > 0)
                    {
                        foreach (DtlImg img in pageData.dtlImg)
                        {
                            string fileName = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Photos/", img.ImgPath, "m", img.ImgNme);
                            if (!System.IO.File.Exists(fileName))
                            {
                                continue;
                            }
                            Image image = new Image(ImageDataFactory.Create(fileName)).SetHorizontalAlignment(HorizontalAlignment.CENTER);
                            document.Add(image);
                        }
                    }
                    document.Close();
                }
                return File(Path.Combine("pdf", pdfpath), "application/pdf");

            }
            catch (Exception ex)
            {
                return BadRequest(ex.ToString());
            }
            return Ok();
        }
    }
}
