using CaseAccountingBusinessLogic.OfficePackage.HelperModels;
using CaseAccountingContracts.ViewModels;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace CaseAccountingBusinessLogic.OfficePackage
{
    public class WordBuilderProvider
    {
        private readonly string tempFileName = "temp.docx";
        private WordprocessingDocument? wordDocument;
        private Body? documentBody;

        public void CreateDocument()
        {
            wordDocument = WordprocessingDocument.Create(tempFileName,
                WordprocessingDocumentType.Document);
            MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
            mainPart.Document = new Document();
            documentBody = mainPart.Document.AppendChild(new Body());
        }

        public void CreateParagraph(string text)
        {
            if (documentBody == null)
            {
                return;
            }
            Paragraph paragraph = new();
            Run run = new();
            run.AppendChild(new Text
            {
                Text = text
            });
            paragraph.AppendChild(run);
            documentBody.AppendChild(paragraph);
        }

        public void CreateTitle(string text)
        {
            if (documentBody == null)
            {
                return;
            }
            Paragraph paragraph = new();
            Run run = new();
            RunProperties properties = new();
            properties.AppendChild(new Bold());
            run.AppendChild(properties);
            run.AppendChild(new Text
            {
                Text = text
            });
            paragraph.AppendChild(run);
            documentBody.AppendChild(paragraph);
        }

        private TableCell CreateTableCell(string text, bool inHead = false, int? cellWidth = null)
        {
            Run run = new();
            TableCell tableCell = new();
            TableCellProperties cellProperties = new()
            {
                TableCellWidth = new()
                {
                    Width = cellWidth.ToString()
                },
                TableCellMargin = new()
                {
                    LeftMargin = new()
                    {
                        Width = "100"
                    }
                }
            };
            if (inHead)
            {
                Shading shading = new()
                {
                    Color = "auto",
                    Fill = "e0e8ff",
                    Val = ShadingPatternValues.Clear
                };
                cellProperties.Append(shading);
                RunProperties properties = new();
                properties.AppendChild(new Bold());
                run.AppendChild(properties);
            }
            run.AppendChild(new Text
            {
                Text = text
            });
            Paragraph paragraph = new(run);
            tableCell.AppendChild(paragraph);
            tableCell.Append(cellProperties);
            return tableCell;
        }

        protected void CreateTable(WordTableData tableData)
        {
            if (documentBody == null || tableData == null)
            {
                return;
            }
            var table = new Table();

            TableProperties tableProperties = new(
                new TableBorders(
                    new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 3 },
                    new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 3 },
                    new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 3 },
                    new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 3 },
                    new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 3 },
                    new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 3 }
                )
            );
            table.AppendChild(tableProperties);

            table.Append(new TableRow(tableData.Columns.Select(x => CreateTableCell(x.Item1, true, x.Item2))));
            table.Append(tableData.Rows.Select(x => new TableRow(x.Select(y => CreateTableCell(y)))));

            documentBody.AppendChild(table);
        }

        private void Save()
        {
            if (documentBody == null || wordDocument == null)
            {
                return;
            }
            wordDocument.MainDocumentPart!.Document.Save();
            wordDocument.Dispose();
        }

        public byte[] GetFile()
        {
            Save();
            byte[] file = File.ReadAllBytes(tempFileName);
            File.Delete(tempFileName);
            return file;
        }

        public void CreateCaseSpecializationTable(List<ReportCaseSpecializationViewModel> data)
        {
            List<List<string>> rows = new();
            foreach (ReportCaseSpecializationViewModel specializationl in data)
            {
                List<string> specializationlCells = new() { specializationl.Specialization, "" };
                rows.Add(specializationlCells);
                List<string> caseCells;
                foreach (string caseString in specializationl.Cases)
                {
                    caseCells = new() { "", caseString };
                    rows.Add(caseCells);
                }
            }
            WordTableData wordTable = new()
            {
                Columns = new List<(string, int)>()
                {
                    ("Специализация", 3000),
                    ("Дела", 3000)
                },
                Rows = rows
            };
            CreateTable(wordTable);
        }
    }
}
