using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace SelectPdf.Samples.Pages
{
    public class PdfResizeModel : PageModel
    {
        public void OnGet()
        {
            DdlPageSize = "A4";
            DdlHorizontalAlign = "Center";
            DdlVerticalAlign = "Middle";
        }

        [BindProperty]
        public string DdlPageSize { get; set; }

        public List<SelectListItem> PageSizes { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "A1", Text = "A1" },
            new SelectListItem { Value = "A2", Text = "A2" },
            new SelectListItem { Value = "A3", Text = "A3" },
            new SelectListItem { Value = "A4", Text = "A4" },
            new SelectListItem { Value = "A5", Text = "A5" },
            new SelectListItem { Value = "Letter", Text = "Letter" },
            new SelectListItem { Value = "HalfLetter", Text = "HalfLetter" },
            new SelectListItem { Value = "Ledger", Text = "Ledger" },
            new SelectListItem { Value = "Legal", Text = "Legal" },
        };

        [BindProperty]
        public string DdlPageOrientation { get; set; }

        public List<SelectListItem> PageOrientations { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Portrait", Text = "Portrait" },
            new SelectListItem { Value = "Landscape", Text = "Landscape" },
        };

        [BindProperty]
        public int TxtTopMargin { get; set; }

        [BindProperty]
        public int TxtRightMargin { get; set; }

        [BindProperty]
        public int TxtBottomMargin { get; set; }

        [BindProperty]
        public int TxtLeftMargin { get; set; }

        [BindProperty]
        public bool ChkAllowScaling { get; set; }

        [BindProperty]
        public bool ChkKeepAspectRatio { get; set; }

        [BindProperty]
        public string DdlHorizontalAlign { get; set; }

        public List<SelectListItem> HorizontalAligns { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Left", Text = "Left" },
            new SelectListItem { Value = "Center", Text = "Center" },
            new SelectListItem { Value = "Right", Text = "Right" },
        };

        [BindProperty]
        public string DdlVerticalAlign { get; set; }

        public List<SelectListItem> VerticalAligns { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Top", Text = "Top" },
            new SelectListItem { Value = "Middle", Text = "Middle" },
            new SelectListItem { Value = "Bottom", Text = "Bottom" },
        };


        public IActionResult OnPost()
        {
            // the test file
            string filePdf = Startup.ServerPath + @"\wwwroot\files\selectpdf.pdf";

            // settings
            PdfPageSize pdfPageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize),
                DdlPageSize, true);

            PdfCustomPageSize pageSize = PdfCustomPageSize.A4;
            switch (pdfPageSize)
            {
                case PdfPageSize.A1:
                    pageSize = PdfCustomPageSize.A1;
                    break;
                case PdfPageSize.A2:
                    pageSize = PdfCustomPageSize.A2;
                    break;
                case PdfPageSize.A3:
                    pageSize = PdfCustomPageSize.A3;
                    break;
                case PdfPageSize.A5:
                    pageSize = PdfCustomPageSize.A5;
                    break;
                case PdfPageSize.Letter:
                    pageSize = PdfCustomPageSize.Letter;
                    break;
                case PdfPageSize.HalfLetter:
                    pageSize = PdfCustomPageSize.HalfLetter;
                    break;
                case PdfPageSize.Ledger:
                    pageSize = PdfCustomPageSize.Ledger;
                    break;
                case PdfPageSize.Legal:
                    pageSize = PdfCustomPageSize.Legal;
                    break;
            }

            PdfPageOrientation pdfOrientation =
                (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation),
                DdlPageOrientation, true);

            PdfHorizontalAlign pdfHorizontalAlign =
                (PdfHorizontalAlign)Enum.Parse(typeof(PdfHorizontalAlign),
                DdlHorizontalAlign, true);

            PdfVerticalAlign pdfVerticalAlign =
                (PdfVerticalAlign)Enum.Parse(typeof(PdfVerticalAlign),
                DdlVerticalAlign, true);

            // resize the content
            PdfResizeManager resizer = new PdfResizeManager();
            resizer.Load(filePdf);

            resizer.PageMargins = new PdfMargins(
                TxtLeftMargin, TxtRightMargin, TxtTopMargin, TxtBottomMargin);
            resizer.PageSize = pageSize;
            resizer.PageOrientation = pdfOrientation;
            resizer.AllowScale = ChkAllowScaling;
            resizer.KeepAspectRatio = ChkKeepAspectRatio;
            resizer.HorizontalAlign = pdfHorizontalAlign;
            resizer.VerticalAlign = pdfVerticalAlign;

            // save pdf document
            byte[] pdf = resizer.Save();

            // close pdf document
            resizer.Close();

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Document.pdf";
            return fileResult;

        }
    }
}