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

namespace BarBusinessLogic.OfficePackage.Implements
{
    public class SaveToWord : AbstractSaveToWord
    {
        private WordprocessingDocument? _wordDocument;
        private Body? _docBody;

        private static JustificationValues GetJustificationValues(WordJustificationType Type)
        {
            return Type switch
            {
                WordJustificationType.Both => JustificationValues.Both,
                WordJustificationType.Center => JustificationValues.Center,
                _ => JustificationValues.Left,
            };
        }
        
        private static SectionProperties CreateSectionProperties()
        {
            var Properties = new SectionProperties();

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

            Properties.AppendChild(PageSize);
            return Properties;
        }
        
        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();
        }
    }
}
