using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using IceCreamBusinessLogic.OfficePackage.HelperEnums;
using IceCreamBusinessLogic.OfficePackage.HelperModels;

namespace IceCreamBusinessLogic.OfficePackage.Implements
{
    public class SaveToWord : AbstractSaveToWord
    {
        private WordprocessingDocument? _wordDocument;

        private Body? _docBody;

        /// <summary>
        /// Получение типа выравнивания
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private static JustificationValues GetJustificationValues(WordJustificationType type)
        {
            return type switch
            {
                WordJustificationType.Both => JustificationValues.Both,
                WordJustificationType.Center => JustificationValues.Center,
                _ => JustificationValues.Left,
            };
        }

        /// <summary>
        /// Настройки страницы
        /// </summary>
        /// <returns></returns>
        private static SectionProperties CreateSectionProperties()
        {
            var properties = new SectionProperties();

            var pageSize = new PageSize
            {
                Orient = PageOrientationValues.Portrait
            };

            properties.AppendChild(pageSize);

            return properties;
        }

        /// <summary>
        /// Задание форматирования для абзаца
        /// </summary>
        /// <param name="paragraphProperties"></param>
        /// <returns></returns>
        private static ParagraphProperties? CreateParagraphProperties(WordTextProperties? paragraphProperties)
        {
            if (paragraphProperties == null)
            {
                return null;
            }

            var properties = new ParagraphProperties();

            properties.AppendChild(new Justification()
            {
                Val = GetJustificationValues(paragraphProperties.JustificationType)
            });

            properties.AppendChild(new SpacingBetweenLines
            {
                LineRule = LineSpacingRuleValues.Auto
            });

            properties.AppendChild(new Indentation());

            var paragraphMarkRunProperties = new ParagraphMarkRunProperties();
            if (!string.IsNullOrEmpty(paragraphProperties.Size))
            {
                paragraphMarkRunProperties.AppendChild(new FontSize { Val = paragraphProperties.Size });
            }
            properties.AppendChild(paragraphMarkRunProperties);

            return properties;
        }

        protected override void CreateWord(WordInfo info)
        {
            _wordDocument = WordprocessingDocument.Create(info.FileName, WordprocessingDocumentType.Document);
            MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart();
            mainPart.Document = new Document();
            _docBody = mainPart.Document.AppendChild(new Body());
        }

        protected override void CreateParagraph(WordParagraph paragraph)
        {
            if (_docBody == null || paragraph == null)
            {
                return;
            }
            var docParagraph = new Paragraph();

            docParagraph.AppendChild(CreateParagraphProperties(paragraph.TextProperties));

            foreach (var run in paragraph.Texts)
            {
                var docRun = new Run();

                var properties = new RunProperties();
                properties.AppendChild(new FontSize { Val = run.Item2.Size });
                if (run.Item2.Bold)
                {
                    properties.AppendChild(new Bold());
                }
                docRun.AppendChild(properties);

                docRun.AppendChild(new Text { Text = run.Item1, Space = SpaceProcessingModeValues.Preserve });

                docParagraph.AppendChild(docRun);
            }

            _docBody.AppendChild(docParagraph);
        }

        protected override void SaveWord(WordInfo info)
        {
            if (_docBody == null || _wordDocument == null)
            {
                return;
            }
            _docBody.AppendChild(CreateSectionProperties());

            _wordDocument.MainDocumentPart!.Document.Save();

            _wordDocument.Close();
        }

        protected override void CreateTable(WordTable table)
        {
            if (_docBody == null || table == null)
            {
                return;
            }
            Table tab = new Table();
            TableProperties props = new TableProperties(
               new TableBorders(
                   new TopBorder
                   {
                       Val = new EnumValue<BorderValues>(BorderValues.Single),
                       Size = 12
                   },
                   new BottomBorder
                   {
                       Val = new EnumValue<BorderValues>(BorderValues.Single),
                       Size = 12
                   },
                   new LeftBorder
                   {
                       Val = new EnumValue<BorderValues>(BorderValues.Single),
                       Size = 12
                   },
                   new RightBorder
                   {
                       Val = new EnumValue<BorderValues>(BorderValues.Single),
                       Size = 12
                   },
                   new InsideHorizontalBorder
                   {
                       Val = new EnumValue<BorderValues>(BorderValues.Single),
                       Size = 12
                   },
                   new InsideVerticalBorder
                   {
                       Val = new EnumValue<BorderValues>(BorderValues.Single),
                       Size = 12
                   }
                   )
            );
            tab.AppendChild<TableProperties>(props);
            TableGrid tableGrid = new TableGrid();
            for (int i = 0; i < table.Headers.Count; i++)
            {
                tableGrid.AppendChild(new GridColumn());
            }
            tab.AppendChild(tableGrid);
            TableRow tableRow = new TableRow();
            foreach (var text in table.Headers)
            {
                tableRow.AppendChild(CreateTableCell(text));
            }
            tab.AppendChild(tableRow);
            int height = table.Texts.Count / table.Headers.Count;
            int width = table.Headers.Count;
            for (int i = 0; i < height; i++)
            {
                tableRow = new TableRow();
                for (int j = 0; j < width; j++)
                {
                    var element = table.Texts[i * table.Headers.Count + j];
                    tableRow.AppendChild(CreateTableCell(element));
                }
                tab.AppendChild(tableRow);
            }

            _docBody.AppendChild(tab);

        }

        private TableCell CreateTableCell(string element)
        {
            var tableParagraph = new Paragraph();
            var run = new Run();
            run.AppendChild(new Text { Text = element });
            tableParagraph.AppendChild(run);
            var tableCell = new TableCell();
            tableCell.AppendChild(tableParagraph);
            return tableCell;
        }
    }
}
