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

namespace PrecastConcretePlantBusinessLogic.BusinessLogic
{
    public class ReportLogic : IReportLogic
    {
        private readonly IComponentStorage _componentStorage;
        private readonly IReinforcedStorage _reinforcedStorage;
        private readonly IOrderStorage _orderStorage;
        private readonly AbstractSaveToExcel _saveToExcel;
        private readonly AbstractSaveToWord _saveToWord;
        private readonly AbstractSaveToPdf _saveToPdf;
        public ReportLogic(IReinforcedStorage reinforcedStorage, IComponentStorage componentStorage, IOrderStorage orderStorage, AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord, AbstractSaveToPdf saveToPdf)
        {
            _reinforcedStorage = reinforcedStorage;
            _componentStorage = componentStorage;
            _orderStorage = orderStorage;
            _saveToExcel = saveToExcel;
            _saveToWord = saveToWord;
            _saveToPdf = saveToPdf;
        }

        public List<ReportReinforcedComponentViewModel> GetReinforcedComponents()
        {
            var components = _componentStorage.GetFullList();
            var reinforcedies = _reinforcedStorage.GetFullList();
            var list = new List<ReportReinforcedComponentViewModel>();
            foreach (var reinforced in reinforcedies)
            {
                var record = new ReportReinforcedComponentViewModel
                {
                    ReinforcedName = reinforced.ReinforcedName,
                    Components = new List<(string Component, int Count)>(),
                    TotalCount = 0
                };
                foreach (var component in components)
                {
                    if (reinforced.ReinforcedComponents.ContainsKey(component.Id))
                    {
                        record.Components.Add(new(component.ComponentName, reinforced.ReinforcedComponents[component.Id].Item2));
                        record.TotalCount += reinforced.ReinforcedComponents[component.Id].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,
                ReinforcedName = x.ReinforcedName,
                Sum = x.Sum,
                Status = x.Status.ToString(),
            })
           .ToList();
        }

        public void SaveReinforcediesToWordFile(ReportBindingModel model)
        {
            _saveToWord.CreateDoc(new WordInfo
            {
                FileName = model.FileName,
                Title = "Список документов",
                Reinforcedies = _reinforcedStorage.GetFullList()
            });
        }
        public void SaveReinforcedComponentToExcelFile(ReportBindingModel model)
        {
            _saveToExcel.CreateReport(new ExcelInfo
            {
                FileName = model.FileName,
                Title = "Список документов",
                ReinforcedComponents = GetReinforcedComponents()
            });
        }

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

    }
}
