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 DistribucionCanillasTotalesDocument : IDocument
    {
        public DistribucionCanillasViewModel Model { get; }
        private static readonly CultureInfo CultureAr = new CultureInfo("es-AR");

        public DistribucionCanillasTotalesDocument(DistribucionCanillasViewModel model)
        {
            Model = model;
        }

        public DocumentMetadata GetMetadata() => DocumentMetadata.Default;

        public void Compose(IDocumentContainer container)
        {
            container.Page(page =>
            {
                page.Margin(1, Unit.Centimetre);
                page.DefaultTextStyle(x => x.FontFamily("Arial").FontSize(10));
                
                page.Header().Element(ComposeHeader);
                page.Content().Element(ComposeContent);
                page.Footer().AlignCenter().Text(x => { x.Span("Página "); x.CurrentPageNumber(); });
            });
        }

        void ComposeHeader(IContainer container)
        {
            container.Column(column =>
            {
                column.Item().Row(row => {
                    row.RelativeItem().Text("Listado de Distribución: Canillas / Accionistas (Totales)").FontSize(12);
                    row.RelativeItem().AlignRight().Text(text => {
                        text.Span("Fecha Consultada ").SemiBold().FontSize(12);
                        text.Span(Model.FechaConsultada).FontSize(12);
                    });
                });
                column.Item().PaddingTop(5).Row(row => {
                    row.RelativeItem().Text(text => {
                         text.Span("Fecha de Generación del Reporte ").SemiBold().FontSize(12);
                         text.Span(Model.FechaReporte).FontSize(12);
                    });
                     row.RelativeItem().AlignRight().Text(text => {
                         text.Span("Empresa ").SemiBold().FontSize(12);
                         text.Span(Model.Empresa).FontSize(12);
                    });
                });
            });
        }
        
        void ComposeContent(IContainer container)
        {
            container.PaddingTop(5, Unit.Millimetre).Column(column =>
            {
                column.Spacing(15);
                
                if(Model.CanillasTodos.Any())
                    column.Item().Element(ComposeTablaTotales);
                
                if(Model.RemitoIngresado > 0)
                    column.Item().Element(ComposeResumenDevoluciones);
            });
        }
        
        void ComposeTablaTotales(IContainer container)
        {
             container.Column(column =>
            {
                column.Item().PaddingBottom(4).Text("Recuento de Publicaciones").SemiBold().FontSize(11);
                column.Item().Table(table =>
                {
                    table.ColumnsDefinition(columns =>
                    {
                        columns.RelativeColumn(2); columns.RelativeColumn(3);
                        columns.RelativeColumn(1.2f); columns.RelativeColumn(1.2f);
                        columns.RelativeColumn(1.2f); columns.RelativeColumn(1.5f);
                    });
                    table.Header(header =>
                    {
                        header.Cell().Border(1).Background(Colors.Grey.Lighten3).Padding(2).Text("Tipo Vendedor");
                        header.Cell().Border(1).Background(Colors.Grey.Lighten3).Padding(2).Text("Publicación");
                        header.Cell().Border(1).Background(Colors.Grey.Lighten3).Padding(2).AlignRight().Text("Llevados");
                        header.Cell().Border(1).Background(Colors.Grey.Lighten3).Padding(2).AlignRight().Text("Devueltos");
                        header.Cell().Border(1).Background(Colors.Grey.Lighten3).Padding(2).AlignRight().Text("Vendidos");
                        header.Cell().Border(1).Background(Colors.Grey.Lighten3).Padding(2).AlignRight().Text("A Rendir");
                    });
                    foreach(var item in Model.CanillasTodos.OrderBy(d => d.TipoVendedor).ThenBy(d => d.Publicacion))
                    {
                        table.Cell().Border(1).Padding(2).Text(item.TipoVendedor);
                        table.Cell().Border(1).Padding(2).Text(item.Publicacion);
                        table.Cell().Border(1).Padding(2).AlignRight().Text(item.TotalCantSalida.ToString("N0"));
                        table.Cell().Border(1).Padding(2).AlignRight().Text(item.TotalCantEntrada.ToString("N0"));
                        table.Cell().Border(1).Padding(2).AlignRight().Text((item.TotalCantSalida-item.TotalCantEntrada).ToString("N0"));
                        table.Cell().Border(1).Padding(2).AlignRight().Text(item.TotalRendir.ToString("C", CultureAr));
                    }
                    var boldStyle = TextStyle.Default.ExtraBold();
                    table.Cell().ColumnSpan(2).BorderTop(2).BorderColor(Colors.Black).Padding(2).AlignRight().Text(t => t.Span("Total General").Style(boldStyle));
                    table.Cell().BorderTop(2).BorderColor(Colors.Black).Padding(2).AlignRight().Text(t => t.Span(Model.CanillasTodos.Sum(i => i.TotalCantSalida).ToString("N0")).Style(boldStyle));
                    table.Cell().BorderTop(2).BorderColor(Colors.Black).Padding(2).AlignRight().Text(t => t.Span(Model.CanillasTodos.Sum(i => i.TotalCantEntrada).ToString("N0")).Style(boldStyle));
                    table.Cell().BorderTop(2).BorderColor(Colors.Black).Padding(2).AlignRight().Text(t => t.Span(Model.CanillasTodos.Sum(i => i.TotalCantSalida - i.TotalCantEntrada).ToString("N0")).Style(boldStyle));
                    table.Cell().BorderTop(2).BorderColor(Colors.Black).Padding(2).AlignRight().Text(t => t.Span(Model.CanillasTodos.Sum(i => i.TotalRendir).ToString("C", CultureAr)).Style(boldStyle));
                });
            });
        }

        void ComposeResumenDevoluciones(IContainer container)
        {
            container.PaddingTop(10).Column(column => {
                column.Item().Row(row => {
                    row.Spacing(15);
                    row.AutoItem().Text(text => { text.Span("Remito: ").SemiBold(); text.Span(Model.RemitoIngresado.ToString("N0")); });
                    row.AutoItem().Text(text => { text.Span("Devolución: ").SemiBold(); text.Span(Model.DevolucionTotal.ToString("N0")); });
                    row.AutoItem().Text(text => { text.Span("Venta: ").SemiBold(); text.Span(Model.VentaTotal.ToString("N0")); });
                });
            });
        }
    }
}