using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CaseAccountingContracts.ViewModels;

namespace CaseAccountingBusinessLogic.OfficePackage.HelperModels
{
	public class WordBuilderCustomer
	{
        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 CreateLawyersHearingTable(List<ReportLawyerHearingViewModel> data)
        {
            List<List<string>> rows = new();
            foreach (ReportLawyerHearingViewModel lawyer in data)
            {
                List<string> lawyerCells = new() { lawyer.Lawyer, "" };
                rows.Add(lawyerCells);
                List<string> hearingCells;
                foreach (string hearing in lawyer.Hearings)
                {
                    hearingCells = new() { "", hearing };
                    rows.Add(hearingCells);
                }
            }
            WordTableData wordTable = new()
            {
                Columns = new List<(string, int)>()
                {
                    ("Юрист", 3000),
                    ("Слушание", 3000)
                },
                Rows = rows
            };
            CreateTable(wordTable);
        }
    }
}
