using PizzeriaBusinessLogic.OfficePackage;
using PizzeriaBusinessLogic.OfficePackage.HelperModels;
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.BusinessLogicsContracts;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StorageContracts;
using PizzeriaContracts.ViewModels;

namespace PizzeriaBusinessLogic.BusinessLogic
{
    public class ReportLogic : IReportLogic
    {
        private readonly IPizzaStorage _PizzaStorage;

        private readonly IOrderStorage _orderStorage;

        private readonly AbstractSaveToExcel _saveToExcel;

        private readonly AbstractSaveToWord _saveToWord;

        private readonly AbstractSaveToPdf _saveToPdf;

        public ReportLogic(IPizzaStorage PizzaStorage, IComponentStorage componentStorage, IOrderStorage orderStorage, AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord, AbstractSaveToPdf saveToPdf)
        {
            _PizzaStorage = PizzaStorage;
            _orderStorage = orderStorage;

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

        public List<ReportPizzaComponentViewModel> GetPizzaComponents()
        {

            var Pizzas = _PizzaStorage.GetFullList();

            var list = new List<ReportPizzaComponentViewModel>();

            foreach (var Pizza in Pizzas)
            {
                var record = new ReportPizzaComponentViewModel
                {
                    PizzaName = Pizza.PizzaName,
                    Components = new List<(string Component, int Count)>(),
                    TotalCount = 0,
                };
                foreach (var component in Pizza.PizzaComponents)
                {
                    record.Components.Add(new(component.Value.Item1.ComponentName, component.Value.Item2));
                    record.TotalCount += component.Value.Item2;
                }

                list.Add(record);
            }

            return list;
        }
        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,
                    PizzaName = x.PizzaName,
                    OrderStatus = x.Status.ToString(),
                    Sum = x.Sum
                })
                .ToList();
}

        public void SavePizzasToWordFile(ReportBindingModel model)
        {
            _saveToWord.CreateDoc(new WordInfo
            {
                FileName = model.FileName,
                Title = "Список изделий",
                Pizzas = _PizzaStorage.GetFullList()
            });
        }

        public void SavePizzaComponentsToExcelFile(ReportBindingModel model)
        {
            _saveToExcel.CreateReport(new ExcelInfo
            {
                FileName = model.FileName,
                Title = "Список изделий",
                PizzaComponents = GetPizzaComponents()
            });
        }

        public void SaveOrdersToPdfFile(ReportBindingModel model)
        {
            _saveToPdf.CreateDoc(new PdfInfo
            {
                FileName = model.FileName,
                Title = "Список заказов",
                DateFrom = model.DateFrom!.Value,
                DateTo = model.DateTo!.Value,
                Orders = GetOrders(model)
            });
        }

       
    }
}
