using BankBusinessLogic.OfficePackage.HelperEnums;
using BankBusinessLogic.OfficePackage.HelperModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BankBusinessLogic.OfficePackage
{
    public abstract class AbstractSaveToWord
    {
        public MemoryStream CreateOperatorDoc(WordInfo info)
        {
            if (info.Payments == null)
            {
                throw new ArgumentNullException("Данные для отчёта отсутсвуют!", nameof(info.Payments));
            }
            CreateWord();
            CreateParagraph(new WordParagraph
            {
                Texts = new List<(string, WordTextProperties)> { (info.Title, new WordTextProperties { Bold = true, Size = "24", }) },
                TextProperties = new WordTextProperties
                {
                    Size = "24",
                    JustificationType = WordJustificationType.Center
                }
            });
            foreach (var payment in info.Payments)
            {
                CreateParagraph(new WordParagraph
                {
                    Texts = new List<(string, WordTextProperties)> { ("Выплата №" + payment.PaymentId + ", ", new WordTextProperties { Size = "24", Bold=true}), ("список закупок, связанных с данной выплатой:", new WordTextProperties { Size = "24"})},
                    TextProperties = new WordTextProperties
                    {
                        Size = "18",
                        JustificationType = WordJustificationType.Both
                    }
                });
                foreach (var purchase in payment.Purchases)
                {
                    CreateParagraph(new WordParagraph
                    {
                        Texts = new List<(string, WordTextProperties)> { ("Закупка №" + purchase.PurchaseId + " ", new WordTextProperties { Size = "18", Bold = true }), (purchase.Amount.ToString(), new WordTextProperties { Size = "18", }) },
                        TextProperties = new WordTextProperties
                        {
                            Size = "18",
                            JustificationType = WordJustificationType.Both
                        }
                    });
                }
            }
            return SaveWord(info);
        }

        public MemoryStream CreateBankOperatorDoc(WordInfo info)
        {
            if (info.Currencies == null)
            {
                throw new ArgumentNullException("Данные для отчета не найдены!",
                    nameof(info.Currencies));
            }
            CreateWord();
            CreateParagraph(new WordParagraph
            {
                Texts = new List<(string, WordTextProperties)> 
                { (info.Title, new WordTextProperties { Bold = true, Size = "24", }) },
                TextProperties = new WordTextProperties
                {
                    Size = "24",
                    JustificationType = WordJustificationType.Center
                }
            });
            foreach (var currency in info.Currencies)
            {
                CreateParagraph(new WordParagraph
                {
                    Texts = new List<(string, WordTextProperties)>
                    { ("Зачисление по валюте: " + currency.CurrencyName+ ". ",
                    new WordTextProperties { Size = "24", Bold = true })}, 
                    TextProperties = new WordTextProperties
                    {
                        Size = "18",
                        JustificationType = WordJustificationType.Both
                    }
                });
                foreach (var transfer in currency.Transfers)
                {
                    CreateParagraph(new WordParagraph
                    {
                        Texts = new List<(string, WordTextProperties)> 
                        { ("Зачисление №" + transfer.Id + ". ",
                        new WordTextProperties { Size = "18", Bold = true }),
                            ("Дата: " + transfer.TransferDateTime.ToString(),
                            new WordTextProperties { Size = "18", }),
                            ("Сумма: " + transfer.Amount.ToString(),
                        new WordTextProperties 
                            { Size = "18", }) },
                        TextProperties = new WordTextProperties
                        {
                            Size = "18",
                            JustificationType = WordJustificationType.Both
                        }
                    });
                }
            }
            return SaveWord(info);
        }
        /// <summary>
        /// Создание doc-файла
        /// </summary>
        /// <param name="info"></param>
        protected abstract void CreateWord();
        /// <summary>
        /// Создание абзаца с текстом
        /// </summary>
        /// <param name="paragraph"></param>
        /// <returns></returns>
        protected abstract void CreateParagraph(WordParagraph paragraph);
        /// <summary>
        /// Сохранение файла
        /// </summary>
        /// <param name="info"></param>
        protected abstract MemoryStream SaveWord(WordInfo info);
    }
}
