using SecureShopBusinessLogic.OfficePackage;
using SecureShopBusinessLogic.OfficePackage.HelperModels;
using SecureShopContracts.BindingModels;
using SecureShopContracts.BusinessLogicsContracts;
using SecureShopContracts.SearchModels;
using SecureShopContracts.StoragesContracts;
using SecureShopContracts.ViewModels;

namespace SecureShopBusinessLogic.BusinessLogics
{
    public class ReportLogic : IReportLogic
    {
        private readonly ISecureStorage _secureStorage;

        private readonly IOrderStorage _orderStorage;

        private readonly AbstractSaveToExcel _saveToExcel;

        private readonly AbstractSaveToWord _saveToWord;

        private readonly AbstractSaveToPdf _saveToPdf;

        public ReportLogic(ISecureStorage secureStorage, IOrderStorage orderStorage,
            AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord, AbstractSaveToPdf saveToPdf)
        {
            _secureStorage = secureStorage;
            _orderStorage = orderStorage;

            _saveToExcel = saveToExcel;
            _saveToWord = saveToWord;
            _saveToPdf = saveToPdf;
        }

        /// <summary>
        /// Получение списка добавок с указанием, в каких мороженых используются
        /// </summary>
        /// <returns></returns>
        public List<ReportSecureFacilitieViewModel> GetSecureFacilitie()
        {
            var secures = _secureStorage.GetFullList();

            var list = new List<ReportSecureFacilitieViewModel>();

            foreach (var secure in secures)
            {
                var record = new ReportSecureFacilitieViewModel
                {
                    SecureName = secure.SecureName,
                    Facilities = new List<(string, int)>(),
                    TotalCount = 0
                };
                foreach (var facilitie in secure.SecureFacilitiess)
                {
                    record.Facilities.Add(new(facilitie.Value.Item1.FacilitiesName, facilitie.Value.Item2));
                    record.TotalCount += facilitie.Value.Item2;
                }
                list.Add(record);
            }
            return list;
        }

        /// <summary>
        /// Получение списка заказов за определенный период
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<ReportOrdersViewModel> GetOrders(ReportBindingModel model)
        {
            return _orderStorage.GetFilteredList(new OrderSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo })
                    .Select(x => new ReportOrdersViewModel
                    {
                        Id = x.Id,
                        DateCreate = x.DateCreate,
                        SecureName = x.SecureName,
                        Sum = x.Sum,
                        OrderStatus = x.Status.ToString()
                    })
                    .ToList();
        }

        /// <summary>
        /// Сохранение мороженых в файл-Word
        /// </summary>
        /// <param name="model"></param>
        public void SaveSecuresToWordFile(ReportBindingModel model)
        {
            _saveToWord.CreateDoc(new WordInfo
            {
                FileName = model.FileName,
                Title = "Список систем безопасности",
                Secures = _secureStorage.GetFullList()
            });
        }

        /// <summary>
        /// Сохранение добавок с указаеним мороженых в файл-Excel
        /// </summary>
        /// <param name="model"></param>
        public void SaveSecureFacilitieToExcelFile(ReportBindingModel model)
        {
            _saveToExcel.CreateReport(new ExcelInfo
            {
                FileName = model.FileName,
                Title = "Список оборудования",
                SecureFacilities = GetSecureFacilitie()
            });
        }

        /// <summary>
        /// Сохранение заказов в файл-Pdf
        /// </summary>
        /// <param name="model"></param>
        public void SaveOrdersToPdfFile(ReportBindingModel model)
        {
            _saveToPdf.CreateDoc(new PdfInfo
            {
                FileName = model.FileName,
                Title = "Список заказов",
                DateFrom = model.DateFrom!.Value,
                DateTo = model.DateTo!.Value,
                Orders = GetOrders(model)
            });
        }
    }
}
