using SushiBarBusinessLogic.OfficePackage;
using SushiBarBusinessLogic.OfficePackage.HelperModels;
using SushiBarContracts.BindingModels;
using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SushiBarBusinessLogic.BusinessLogics
{
    public class ReportLogic : IReportLogic
    {
        private readonly IComponentStorage _componentStorage;
        private readonly ISushiStorage _SushiStorage;
        private readonly IOrderStorage _orderStorage;
        private readonly IShopStorage _shopStorage;
        private readonly AbstractSaveToExcel _saveToExcel;
        private readonly AbstractSaveToWord _saveToWord;
        private readonly AbstractSaveToPdf _saveToPdf;

        public ReportLogic(ISushiStorage SushiStorage, IComponentStorage componentStorage, IOrderStorage orderStorage, IShopStorage shopStorage,
            AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord, AbstractSaveToPdf saveToPdf)
        {
            _SushiStorage = SushiStorage;
            _componentStorage = componentStorage;
            _orderStorage = orderStorage;
            _shopStorage = shopStorage;
            _saveToExcel = saveToExcel;
            _saveToWord = saveToWord;
            _saveToPdf = saveToPdf;
        }

        public List<ReportSushiComponentViewModel> GetSushiComponents()
        {
            return _SushiStorage.GetFullList().Select(x => new ReportSushiComponentViewModel
            {
                SushiName = x.SushiName,
                Components = x.SushiComponents.Select(x => (x.Value.Item1.ComponentName, x.Value.Item2)).ToList(),
                TotalCount = x.SushiComponents.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,
                        SushiName = x.SushiName,
                        Sum = x.Sum,
                        Status = x.Status.ToString()
                    })
                    .ToList();
        }

        public void SaveSushisToWordFile(ReportBindingModel model)
        {
            _saveToWord.CreateDoc(new WordInfo
            {
                FileName = model.FileName,
                Title = "Список суши",
                Sushis = _SushiStorage.GetFullList()
            });
        }

        public void SaveSushiComponentToExcelFile(ReportBindingModel model)
        {
            _saveToExcel.CreateReport(new ExcelInfo
            {
                FileName = model.FileName,
                Title = "Список суши",
                SushiComponents = GetSushiComponents()
            });
        }

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

        public List<ReportShopsViewModel> GetShops()
        {
            return _shopStorage.GetFullList().Select(x => new ReportShopsViewModel
            {
                ShopName = x.ShopName,
                Sushis = x.ShopSushis.Select(x => (x.Value.Item1.SushiName, x.Value.Item2)).ToList(),
                TotalCount = x.ShopSushis.Select(x => x.Value.Item2).Sum()
            }).ToList();
        }

        public List<ReportGroupOrdersViewModel> GetGroupedOrders()
        {
            return _orderStorage.GetFullList().GroupBy(x => x.DateCreate.Date).Select(x => new ReportGroupOrdersViewModel
            {
                Date = x.Key,
                OrdersCount = x.Count(),
                OrdersSum = x.Select(y => y.Sum).Sum()
            }).ToList();
        }

        public void SaveShopsToWordFile(ReportBindingModel model)
        {
            _saveToWord.CreateShopsDoc(new WordShopInfo
            {
                FileName = model.FileName,
                Title = "Список магазинов",
                Shops = _shopStorage.GetFullList()
            });
        }

        public void SaveShopsToExcelFile(ReportBindingModel model)
        {
            _saveToExcel.CreateShopSushisReport(new ExcelShop
            {
                FileName = model.FileName,
                Title = "Наполненость магазинов",
                ShopSushis = GetShops()
            });
        }

        public void SaveGroupedOrdersToPdfFile(ReportBindingModel model)
        {
            _saveToPdf.CreateGroupedOrdersDoc(new PdfGroupedOrdersInfo
            {
                FileName = model.FileName,
                Title = "Список заказов сгруппированных по дате заказов",
                GroupedOrders = GetGroupedOrders()
            });
        }
    }
}
