using PlumbingRepairBusinessLogic.OfficePackage.HelperModels;
using PlumbingRepairBusinessLogic.OfficePackage;
using PlumbingRepairContracts.BindingModels;
using PlumbingRepairContracts.BusinessLogicsContracts;
using PlumbingRepairContracts.SearchModels;
using PlumbingRepairContracts.StoragesContracts;
using PlumbingRepairContracts.ViewModels;

namespace PlumbingRepairBusinessLogic.BusinessLogic
{
    public class ReportLogic : IReportLogic
    {
        private readonly IComponentStorage _componentStorage;
        private readonly IWorkStorage _workStorage;
        private readonly IOrderStorage _orderStorage;
        private readonly AbstractSaveToExcel _saveToExcel;
        private readonly AbstractSaveToWord _saveToWord;
        private readonly AbstractSaveToPdf _saveToPdf;
        public ReportLogic(
            IWorkStorage workStorage,
            IComponentStorage componentStorage,
            IOrderStorage orderStorage,
            AbstractSaveToExcel saveToExcel,
            AbstractSaveToWord saveToWord,
            AbstractSaveToPdf saveToPdf)
        {
            _workStorage = workStorage;
            _componentStorage = componentStorage;
            _orderStorage = orderStorage;
            _saveToExcel = saveToExcel;
            _saveToWord = saveToWord;
            _saveToPdf = saveToPdf;
        }
        public List<ReportWorkComponentViewModel> GetWork()
        {
            var work = _workStorage.GetFullList();
            var list = new List<ReportWorkComponentViewModel>();

            foreach (var wor in work)
            {
                var record = new ReportWorkComponentViewModel
                {
                    WorkName = wor.WorkName,
                    Components = new List<Tuple<string, int>>(),
                    TotalCount = 0
                };
                foreach (var component in wor.WorkComponents)
                {
                    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,
                WorkName = x.WorkName,
                Sum = x.Sum,
                Status = x.Status.ToString()
            })
           .ToList();
        }
        public void SaveWorksToWordFile(ReportBindingModel model)
        {
            _saveToWord.CreateDoc(new WordInfo
            {
                FileName = model.FileName,
                Title = "Список Работ",
                Works = _workStorage.GetFullList()
            });
        }
        public void SaveWorkComponentToExcelFile(ReportBindingModel model)
        {
            _saveToExcel.CreateReport(new ExcelInfo
            {
                FileName = model.FileName,
                Title = "Список Работ",
                Works = GetWork()
            });
        }
        public void SaveOrdersToPdfFile(ReportBindingModel model)
        {
            _saveToPdf.CreateDoc(new PdfInfo
            {
                FileName = model.FileName,
                Title = "Список заказов",
                DateFrom = DateTime.SpecifyKind(model.DateFrom!.Value, DateTimeKind.Utc),
                DateTo = DateTime.SpecifyKind(model.DateTo!.Value, DateTimeKind.Utc),
                Orders = GetOrders(model)
            });
        }
    }
}
