using FishFactoryBusinessLogic.OfficePackage.HelperModels;
using FishFactoryBusinessLogic.OfficePackage;
using FishFactoryContracts.BindingModels;
using FishFactoryContracts.BusinessLogicsContracts;
using FishFactoryContracts.SearchModels;
using FishFactoryContracts.StoragesContracts;
using FishFactoryContracts.ViewModels;

namespace FishFactoryBusinessLogic.BusinessLogics
{
    public class ReportLogic : IReportLogic
    {
        private readonly IIngredientStorage _ingredientStorage;

        private readonly ICannedStorage _cannedStorage;

        private readonly IOrderStorage _orderStorage;

        private readonly AbstractSaveToExcel _saveToExcel;

        private readonly AbstractSaveToWord _saveToWord;

        private readonly AbstractSaveToPdf _saveToPdf;

        public ReportLogic(ICannedStorage productStorage, IIngredientStorage ingredientStorage, IOrderStorage orderStorage,
            AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord, AbstractSaveToPdf saveToPdf)
        {
            _cannedStorage = productStorage;
            _ingredientStorage = ingredientStorage;
            _orderStorage = orderStorage;

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

        /// <summary>
        /// Получение списка ингредиентов с указанием, в каких суши используются
        /// </summary>
        /// <returns></returns>
        public List<ReportCannedIngredientViewModel> GetCannedIngredient()
        {
            var ingredients = _ingredientStorage.GetFullList();

            var cannedList = _cannedStorage.GetFullList();

            var list = new List<ReportCannedIngredientViewModel>();

            foreach (var canned in cannedList)
            {
                var record = new ReportCannedIngredientViewModel
                {
                    CannedName = canned.CannedName,
                    Ingredients = new(),
                    TotalCount = 0
                };
                foreach (var ingredient in ingredients)
                {
                    if (canned.CannedIngredients.ContainsKey(ingredient.Id))
                    {
                        record.Ingredients.Add(new(ingredient.IngredientName,
                            canned.CannedIngredients[ingredient.Id].Item2));
                        record.TotalCount += canned.CannedIngredients[ingredient.Id].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,
                        CannedName = x.CannedName,
                        OrderStatus = x.Status.ToString(),
                        Sum = x.Sum
                    })
                    .ToList();
        }

        /// <summary>
        /// Сохранение консерв в файл-Word
        /// </summary>
        /// <param name="model"></param>
        public void SaveListCannedToWordFile(ReportBindingModel model)
        {
            _saveToWord.CreateDoc(new WordInfo
            {
                FileName = model.FileName,
                Title = "Список консерв",
                ListCanned = _cannedStorage.GetFullList()
            });
        }

        /// <summary>
        /// Сохранение ингредиентов с указаеним консерв в файл-Excel
        /// </summary>
        /// <param name="model"></param>
        public void SaveCannedIngredientToExcelFile(ReportBindingModel model)
        {
            _saveToExcel.CreateReport(new ExcelInfo
            {
                FileName = model.FileName,
                Title = "Список консерв",
                CannedIngredients = GetCannedIngredient()
            });
        }

        /// <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)
            });
        }
    }
}
