using System;
using System.Drawing;
using System.IO;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using GrapeCity.Documents.Drawing;



// Aliases
using GcImage = GrapeCity.Documents.Drawing.Image;
using GcFont = GrapeCity.Documents.Text.Font;
namespace CreatePdf_DocSol.Pages
{
    public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;
        private readonly IWebHostEnvironment _environment;

        public IndexModel(ILogger<IndexModel> logger, IWebHostEnvironment environment)
        {
            _logger = logger;
            _environment = environment;

            // Create PDF when the page model is constructed
            CreatePdf();
        }

        public void OnGet()
        {
        }

        private void CreatePdf()
        {
            try
            {
                // 1. Create DsPdf document
                var doc = new GcPdfDocument();

                // 2. Add page and get graphics
                var page = doc.Pages.Add();
                var g = page.Graphics;

                
                var imagePath = Path.Combine("Resources", "Images", "myheader.jpg");
                var rc = page.Bounds;

                float textTop;

                if (System.IO.File.Exists(imagePath))
                {
                    // Load header image
                    GcImage img = GcImage.FromFile(imagePath);

                    rc.Height *= 0.35f;
                    g.DrawImage(img, rc, null, ImageAlign.StretchImage);

                  
                    textTop = rc.Bottom + 12;
                }
                else
                {
                    textTop = 72;
                }

                
                var fontPath = Path.Combine("Resources", "Fonts", "OpenSans-Regular.ttf");
                GcFont bodyFont;

                if (System.IO.File.Exists(fontPath))
                {
                    bodyFont = GcFont.FromFile(fontPath);
                }
                else
                {
                    bodyFont = StandardFonts.Helvetica;
                }

                var headingFormat = new TextFormat
                {
                    Font = bodyFont,
                    FontSize = 20,
                    ForeColor = Color.Black
                };

                var textFormat = new TextFormat
                {
                    Font = bodyFont,
                    FontSize = 12,
                    ForeColor = Color.Black
                };

               
                var tl = g.CreateTextLayout();
                tl.MaxWidth = page.Size.Width - 72 * 2; 

                tl.AppendLine("Document Solutions for PDF (DsPdf)", headingFormat);
                tl.AppendLine("");
                tl.AppendLine(
                    "This PDF includes a header image and a custom font loaded from Resources. " +
                    "It follows the same layout style as the original marketing blog sample.",
                    textFormat);
                tl.AppendLine("");
                tl.AppendLine("Features:", textFormat);
                tl.AppendLine("• Load header branding images", textFormat);
                tl.AppendLine("• Use custom TTF fonts", textFormat);
                tl.AppendLine("• Create rich PDF layouts in ASP.NET Core", textFormat);

                tl.PerformLayout(true);

              
                g.DrawTextLayout(tl, new PointF(72, textTop));

                
                Directory.CreateDirectory(_environment.WebRootPath);
                var outputPath = Path.Combine(_environment.WebRootPath, "sample.pdf");

                using var fs = new FileStream(outputPath, FileMode.Create, FileAccess.Write);
                doc.Save(fs);

                _logger.LogInformation("PDF created at {Path}", outputPath);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error creating PDF.");
            }
        }
    }
}
