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 AbstractSaveToPdf
    {
        public MemoryStream CreateOperatorDoc(PdfInfo info)
        {
            if (info.Transfers == null)
            {
                throw new ArgumentNullException("Данные для отчёта отсутсвуют!", nameof(info.Transfers));
            }

            CreatePdf(info);
            CreateParagraph(new PdfParagraph { Text = info.Title, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center });
            CreateParagraph(new PdfParagraph { Text = $"с {info.DateFrom.ToShortDateString()} по {info.DateTo.ToShortDateString()}", Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Center });

            CreateTable(new List<string> { "4cm", "3cm", "3cm", "2cm", "3cm", "3cm" });

            CreateRow(new PdfRowParameters
            {
                Texts = new List<string> { "Номер зачисления", "Дата зачисления", "Номер закупки", "Сумма", "Валюта", "Дата закупки" },
                Style = "NormalTitle",
                ParagraphAlignment = PdfParagraphAlignmentType.Center
            });

            foreach (var transfer in info.Transfers)
            {
                CreateRow(new PdfRowParameters
                {
                    Texts = new List<string> { "Зачисление №" + transfer.TransferId, transfer.TransferDate.ToShortDateString(), "", "", "", "" },
                    Style = "Normal",
                    ParagraphAlignment = PdfParagraphAlignmentType.Left
                });
                foreach(var purchase in transfer.Purchases)
                {
                    CreateRow(new PdfRowParameters
                    {
                        Texts = new List<string> { "", "", "Закупка №" + purchase.Id, purchase.Amount.ToString(), purchase.CurrencyName.ToString(), purchase.PurchaseDate.ToShortDateString()},
                        Style = "Normal",
                        ParagraphAlignment = PdfParagraphAlignmentType.Left
                    });
                }
            }

            return SavePdf();
        }


        public MemoryStream CreateBankOperatorDoc(PdfInfo info)
        {
            if (info.CurrencyPurchases == null)
            {
                throw new ArgumentNullException("Данные для отчёта не найдены!", nameof(info.CurrencyPurchases));
            }

            CreatePdf(info);
            CreateParagraph(new PdfParagraph { Text = info.Title, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center });
            CreateParagraph(new PdfParagraph { Text = $"с {info.DateFrom.ToShortDateString()} по {info.DateTo.ToShortDateString()}", Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Center });

            CreateTable(new List<string> { "4cm", "3cm", "2cm", "4cm", "3cm"});

            CreateRow(new PdfRowParameters
            {
                Texts = new List<string> { "Номер закупки", "Дата закупки", "Валюта", "Номер выплаты", "Дата выплаты" },
                Style = "NormalTitle",
                ParagraphAlignment = PdfParagraphAlignmentType.Center
            });

            foreach (var purchase in info.CurrencyPurchases)
            {
                CreateRow(new PdfRowParameters
                {
                    Texts = new List<string> { "Закупка №" + purchase.CurrencyPurchaseId, 
                        purchase.PurchaseDate.ToShortDateString(), purchase.CurrencyName,
                        "",""},
                    Style = "Normal",
                    ParagraphAlignment = PdfParagraphAlignmentType.Left
                });
                foreach (var payment in purchase.Payments)
                {
                    CreateRow(new PdfRowParameters
                    {
                        Texts = new List<string> { "", "", "", "Выплата № " + payment.PaymentId,
                            payment.PaymentDate.ToShortDateString() },
                        Style = "Normal",
                        ParagraphAlignment = PdfParagraphAlignmentType.Left
                    });
                }
            }

            return SavePdf();
        }

        /// <summary>
		/// Создание doc-файла
		/// </summary>
		/// <param name="info"></param>
        protected abstract void CreatePdf(PdfInfo info);

        /// <summary>
        /// Создание параграфа с текстом
        /// </summary>
        /// <param name="title"></param>
        /// <param name="style"></param>
        protected abstract void CreateParagraph(PdfParagraph paragraph);

        /// <summary>
        /// Создание таблицы
        /// </summary>
        /// <param name="title"></param>
        /// <param name="style"></param>
        protected abstract void CreateTable(List<string> columns);

        /// <summary>
        /// Создание и заполнение строки
        /// </summary>
        /// <param name="rowParameters"></param>
        protected abstract void CreateRow(PdfRowParameters rowParameters);

        /// <summary>
		/// Сохранение файла
		/// </summary>
		/// <param name="info"></param>
        protected abstract MemoryStream SavePdf();
    }
}
