using BarBusinessLogic.OfficePackage;
using BarBusinessLogic.OfficePackage.HelperModels;
using BarContracts.BindingModels;
using BarContracts.BusinessLogicContracts;
using BarContracts.SearchModels;
using BarContracts.StoragesContracts;
using BarContracts.ViewModels;

namespace BarBusinessLogic.BusinessLogics
{
    public class ReportLogic : IReportLogic
    {
        private readonly IComponentStorage _componentStorage;
        private readonly ICocktailStorage _CocktailStorage;
        private readonly IOrderStorage _orderStorage;
        private readonly AbstractSaveToExcel _saveToExcel;
        private readonly AbstractSaveToWord _saveToWord;
        private readonly AbstractSaveToPdf _saveToPdf;

        public ReportLogic(ICocktailStorage CocktailStorage, IComponentStorage ComponentStorage, IOrderStorage OrderStorage,
            AbstractSaveToExcel SaveToExcel, AbstractSaveToWord SaveToWord, AbstractSaveToPdf SaveToPdf)
        {
            _CocktailStorage = CocktailStorage;
            _componentStorage = ComponentStorage;
            _orderStorage = OrderStorage;

            _saveToExcel = SaveToExcel;
            _saveToWord = SaveToWord;
            _saveToPdf = SaveToPdf;
        }

        public List<ReportCocktailComponentViewModel> GetCocktailComponents()
        {
            return _CocktailStorage.GetFullList().
                Select(x => new ReportCocktailComponentViewModel
                {
                    CocktailName = x.CocktailName,
                    Components = x.CocktailComponents.Select(x => (x.Value.Item1.ComponentName, x.Value.Item2)).ToList(),
                    TotalCount = x.CocktailComponents.Select(x => x.Value.Item2).Sum()
                })
                .ToList();
        }

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

        public void SaveComponentsToWordFile(ReportBindingModel Model)
        {
            _saveToWord.CreateDoc(new WordInfo
            {
                FileName = Model.FileName,
                Title = "Список Коктейлей",
                Cocktails = _CocktailStorage.GetFullList()
            });
        }

        public void SaveCocktailComponentToExcelFile(ReportBindingModel Model)
        {
            _saveToExcel.CreateReport(new ExcelInfo
            {
                FileName = Model.FileName,
                Title = "Список компонентов",
                CocktailComponents = GetCocktailComponents()
            });
        }

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