using BusinessLogic.OfficePackage.HelperModels;
using BusinessLogic.OfficePackage;
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using Contracts.SearchModels;

namespace BusinessLogic.BusinessLogic
{
    public class ReportLogic : IReportLogic
    {
        private readonly IProductStorage _productStorage;
        private readonly ISupplyStorage _supplyStorage;
        private readonly IMediaFileStorage _mediaFileStorage;
        private readonly AbstractSaveToPdf _saveToPdf;
        private readonly AbstractSaveToPdfCheque _saveToCheque;
        public ReportLogic(IProductStorage productStorage, ISupplyStorage supplyStorage, AbstractSaveToPdf saveToPdf, AbstractSaveToPdfCheque saveToPdfCheque, IMediaFileStorage mediaFileStorage)
        {
            _productStorage = productStorage;
            _supplyStorage = supplyStorage;
            _saveToPdf = saveToPdf;
            _saveToCheque = saveToPdfCheque;
            _mediaFileStorage = mediaFileStorage;
        }
        /// <summary>
        /// Получение списка компонент с указанием, в каких изделиях используются
        /// </summary>
        /// <returns></returns>
        public List<ReportSupplyProductViewModel> GetSupplyProduct()
        {
            var products = _productStorage.GetFullList();
            var supplies = _supplyStorage.GetFullList();
            var list = new List<ReportSupplyProductViewModel>();
            foreach (var supply in supplies)
            {
                var record = new ReportSupplyProductViewModel
                {
                    SupplyName = supply.Name,
                    Products = new List<Tuple<string, int>>(),
                    TotalCount = 0
                };
                foreach (var product in products)
                {
                    if (supply.Products.ContainsKey(product.Id))
                    {
                        record.Products.Add(new Tuple<string, int>(product.Name, supply.Products[product.Id].Item2));
                        record.TotalCount += supply.Products[product.Id].Item2;
                    }
                }
                list.Add(record);
            }
            return list;
        }
        /// <summary>
        /// Сохранение заказов в файл-Pdf
        /// </summary>
        /// <param name="model"></param>
        public void SaveSuppliesToPdfFile(ReportBindingModel model)
        {
            _saveToPdf.CreateDoc(new PdfInfo
            {
                FileName = model.FileName,
                Title = "Накладная",
                Date = model.Date!.Value,
                Supply = _supplyStorage.GetElement(new SupplySearchModel()
                {
                    Id = model.SupplyId,
                })
            });
        }

        public void SaveProductToPdfFile(ReportBindingModel model)
        {
            _saveToCheque.CreateDoc(new PdfInfo
            {
                FileName = model.FileName,
                Title = "Чек на товар",
                Product = _productStorage.GetElement(new ProductSearchModel()
                {
                    Id = model.ProductId
                }),
                //MediaFiles = _mediaFileStorage.GetFilteredList(new MediaFileSearchModel() { ProductId = model.ProductId })
            });
        }
    }
}
