using SushiBarBusinessLogic.OfficePackage.HelpersModels;
using SushiBarBusinessLogic.OfficePackage;
using SushiBarContracts.BindingModels;
using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;

namespace SushiBarBusinessLogic.BusinessLogics
{
    public class ReportLogic : IReportLogic
    {
        private readonly IComponentStorage _componentStorage;
        private readonly ISushiStorage _sushiStorage;
        private readonly IOrderStorage _orderStorage;
        private readonly AbstractSaveToExcel _saveToExcel;
        private readonly AbstractSaveToWord _saveToWord;
        private readonly AbstractSaveToPdf _saveToPdf;
        public ReportLogic(
            ISushiStorage sushiStorage, 
            IComponentStorage componentStorage, 
            IOrderStorage orderStorage, 
            AbstractSaveToExcel saveToExcel, 
            AbstractSaveToWord saveToWord,
            AbstractSaveToPdf saveToPdf)
        {
            _sushiStorage = sushiStorage;
            _componentStorage = componentStorage;
            _orderStorage = orderStorage;
            _saveToExcel = saveToExcel;
            _saveToWord = saveToWord;
            _saveToPdf = saveToPdf;
        }
        public List<ReportSushiComponentViewModel> GetSushi()
        {
            var sushi = _sushiStorage.GetFullList();
            var list = new List<ReportSushiComponentViewModel>();

            foreach (var suh in sushi)
            {
                var record = new ReportSushiComponentViewModel
                {
                    SushiName = suh.SushiName,
                    Components = new List<Tuple<string, int>>(),
                    TotalCount = 0
                };
                foreach (var component in suh.SushiComponents)
                {
                    record.Components.Add(new Tuple<string, int>(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,
                SushiName = x.SushiName,
                Sum = x.Sum,
                Status = x.Status.ToString()
            })
           .ToList();
        }
        public void SaveSushiToWordFile(ReportBindingModel model)
        {
            _saveToWord.CreateDoc(new WordInfo
            {
                FileName = model.FileName,
                Title = "Sushi list",
                Sushi = _sushiStorage.GetFullList()
            });
        }
        public void SaveProductComponentToExcelFile(ReportBindingModel model)
        {
            _saveToExcel.CreateReport(new ExcelInfo
            {
                FileName = model.FileName,
                Title = "Sushi",
                Sushi = GetSushi()
            });
        }
        public void SaveOrdersToPdfFile(ReportBindingModel model)
        {
            _saveToPdf.CreateDoc(new PdfInfo
            {
                FileName = model.FileName,
                Title = "Orders list",
                DateFrom = model.DateFrom!.Value,
                DateTo = model.DateTo!.Value,
                Orders = GetOrders(model)
            });
        }
    }
}
