using BarBusinessLogic.OfficePackage.HelperEnums;
using BarBusinessLogic.OfficePackage.HelperModels;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;

namespace BarBusinessLogic.OfficePackage.Implements
{
    public class SaveToPdf : AbstractSaveToPdf
    {
        private Document? _document;
        private Section? _section;
        private Table? _table;

        private static ParagraphAlignment GetParagraphAlignment(PdfParagraphAlignmentType Type)
        {
            return Type switch
            {
                PdfParagraphAlignmentType.Center => ParagraphAlignment.Center,
                PdfParagraphAlignmentType.Left => ParagraphAlignment.Left,
                PdfParagraphAlignmentType.Rigth => ParagraphAlignment.Right,
                _ => ParagraphAlignment.Justify,
            };
        }

        private static void DefineStyles(Document Document)
        {
            var Style = Document.Styles["Normal"];
            Style.Font.Name = "Times New Roman";
            Style.Font.Size = 14;

            Style = Document.Styles.AddStyle("NormalTitle", "Normal");
            Style.Font.Bold = true;
        }

        protected override void CreatePdf(PdfInfo Info)
        {
            _document = new Document();
            DefineStyles(_document);

            _section = _document.AddSection();
        }

        protected override void CreateParagraph(PdfParagraph PdfParagraph)
        {
            if (_section == null)
                return;

            var Paragraph = _section.AddParagraph(PdfParagraph.Text);
            
            Paragraph.Format.SpaceAfter = "1cm";
            Paragraph.Format.Alignment = GetParagraphAlignment(PdfParagraph.ParagraphAlignment);
            Paragraph.Style = PdfParagraph.Style;
        }

        protected override void CreateTable(List<string> Columns)
        {
            if (_document == null)
                return;

            _table = _document.LastSection.AddTable();

            foreach (var Elem in Columns)
            {
                _table.AddColumn(Elem);
            }
        }

        protected override void CreateRow(PdfRowParameters RowParameters)
        {
            if (_table == null)
                return;

            var Row = _table.AddRow();
            
            for (int i = 0; i < RowParameters.Texts.Count; ++i)
            {
                Row.Cells[i].AddParagraph(RowParameters.Texts[i]);

                if (!string.IsNullOrEmpty(RowParameters.Style))
                {
                    Row.Cells[i].Style = RowParameters.Style;
                }

                Unit borderWidth = 0.5;

                Row.Cells[i].Borders.Left.Width = borderWidth;
                Row.Cells[i].Borders.Right.Width = borderWidth;
                Row.Cells[i].Borders.Top.Width = borderWidth;
                Row.Cells[i].Borders.Bottom.Width = borderWidth;

                Row.Cells[i].Format.Alignment = GetParagraphAlignment(RowParameters.ParagraphAlignment);
                Row.Cells[i].VerticalAlignment = VerticalAlignment.Center;
            }
        }

        protected override void SavePdf(PdfInfo Info)
        {
            var Renderer = new PdfDocumentRenderer(true)
            {
                Document = _document
            };
            
            Renderer.RenderDocument();
            Renderer.PdfDocument.Save(Info.FileName);
        }
    }
}
