using GestionIntegral.Api.Dtos.Reportes.ViewModels;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using System.Globalization;
using System.Linq;

namespace GestionIntegral.Api.Controllers.Reportes.PdfTemplates
{
    public class LiquidacionCanillaDocument : IDocument
    {
        public LiquidacionCanillaViewModel Model { get; }
        private static readonly CultureInfo CultureAr = new CultureInfo("es-AR");

        public LiquidacionCanillaDocument(LiquidacionCanillaViewModel model)
        {
            Model = model;
        }

        public DocumentMetadata GetMetadata() => DocumentMetadata.Default;

        public void Compose(IDocumentContainer container)
{
    container.Page(page =>
    {
        page.Size(PageSizes.A4);
        page.Margin(5, Unit.Millimetre);
        page.DefaultTextStyle(x => x.FontFamily("Arial").FontSize(9));

        page.Content().Column(mainColumn =>
        {
            mainColumn.Item()
                .AlignCenter()
                .Width(PageSizes.A6.Width)   
                .Height(PageSizes.A6.Height)
                .Column(a6ContentColumn =>
                {
                    a6ContentColumn.Item().PaddingRight(10, Unit.Millimetre).PaddingLeft(10, Unit.Millimetre).Column(content =>
                    {
                        content.Item().Element(ComposeHeader);
                        content.Item().Element(ComposeContent);
                    });
                });
        });
    });
}

        void ComposeHeader(IContainer container)
        {
            container.Column(column =>
            {
                column.Item().Text("EL DIA S.A.I.C. y F.");
                column.Item().Text(text =>
                {
                    text.Span("Liquidación venta de diarios del: ");
                    text.Span(Model.FechaLiquidacion);
                });
                column.Item().PaddingTop(5).Text($"Vendedor: {Model.NombreVendedor}").SemiBold();
                column.Item().PaddingTop(10);
            });
        }

        void ComposeContent(IContainer container)
        {
            container.Column(column =>
            {
                column.Spacing(15);

                column.Item().Table(table =>
                {
                    table.ColumnsDefinition(columns =>
                    {
                        columns.RelativeColumn(2);
                        columns.RelativeColumn(2);
                        columns.RelativeColumn(1);
                    });

                    foreach (var item in Model.Detalles.OrderBy(d => d.Publicacion))
                    {
                        var vendidos = item.TotalCantSalida - item.TotalCantEntrada;

                        table.Cell().ColumnSpan(3).Text(item.Publicacion).SemiBold();

                        table.Cell();
                        table.Cell().Text("Retirados");
                        table.Cell().AlignRight().Text(item.TotalCantSalida.ToString("N0"));

                        table.Cell();
                        table.Cell().Text("Devueltos");
                        table.Cell().AlignRight().Text(item.TotalCantEntrada.ToString("N0"));

                        table.Cell();
                        table.Cell().Text("Vendidos");
                        table.Cell().AlignRight().Text(vendidos.ToString("N0"));

                        table.Cell();
                        table.Cell().Text("Precio Unitario");
                        table.Cell().AlignRight().Text(item.PrecioEjemplar.ToString("C", CultureAr));

                        table.Cell();
                        table.Cell().BorderTop(1.5f).BorderColor(Colors.Black).PaddingTop(2).Text(t => t.Span("Importe Vendido").SemiBold());
                        table.Cell().BorderTop(1.5f).BorderColor(Colors.Black).PaddingTop(2).AlignRight().Text(t => t.Span(item.TotalRendir.ToString("C", CultureAr)).SemiBold());
                    }
                });

                column.Item().BorderTop(2).BorderColor(Colors.Black).PaddingTop(4).Row(row =>
                {
                    row.RelativeItem().Text("Total A Rendir").SemiBold().FontSize(10);
                    row.RelativeItem().AlignRight().Text(text => text.Span(Model.TotalARendir.ToString("C", CultureAr)).SemiBold().FontSize(10));
                });

                if (!Model.EsAccionista && Model.Ganancias.Any())
                {
                    column.Item().PaddingTop(10).Element(ComposeGananciasTable);
                }
            });
        }

        void ComposeGananciasTable(IContainer container)
        {
            container.Column(column =>
            {
                column.Item().Text("Comisiones Acreditadas").SemiBold().FontSize(11);

                column.Item().PaddingTop(5).Table(table =>
                {
                    table.ColumnsDefinition(columns =>
                    {
                        columns.RelativeColumn(2);
                        columns.RelativeColumn(1);
                    });

                    foreach (var item in Model.Ganancias)
                    {
                        table.Cell().Border(1).BorderColor(Colors.Grey.Lighten2).Padding(3).Text(item.Publicacion);
                        table.Cell().Border(1).BorderColor(Colors.Grey.Lighten2).Padding(3).AlignRight().Text(item.TotalRendir.ToString("C", CultureAr));
                    }

                    table.Cell().BorderTop(1.5f).BorderColor(Colors.Black).Padding(3).Text("Total Comisiones").SemiBold();
                    table.Cell().BorderTop(1.5f).BorderColor(Colors.Black).Padding(3).AlignRight().Text(t => t.Span(Model.TotalComisiones.ToString("C", CultureAr)).SemiBold());
                });
            });
        }
    }
}