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

namespace DressAtelierBusinessLogic.BusinessLogic
{
    public class ReportLogic : IReportLogic
    {
        private readonly IDressStorage _dressStorage;
        private readonly IOrderStorage _orderStorage;
        private readonly AbstractSaveToExcel _saveToExcel;
        private readonly AbstractSaveToWord _saveToWord;
        private readonly AbstractSaveToPdf _saveToPdf;

        public ReportLogic(IDressStorage dressStorage, IOrderStorage orderStorage, AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord, AbstractSaveToPdf saveToPdf)
        {
            _dressStorage = dressStorage;
            _orderStorage = orderStorage;
            _saveToExcel = saveToExcel;
            _saveToWord = saveToWord;
            _saveToPdf = saveToPdf;
        }

        public List<ReportDressMaterialViewModel> GetDressMaterial()
        {
            var products = _dressStorage.GetFullList();
            var list = new List<ReportDressMaterialViewModel>();
            foreach (var product in products)
            {
                var record = new ReportDressMaterialViewModel
                {
                    DressName = product.DressName,
                    Materials = new List<(string Material, int count)>(),
                    TotalAmount = 0
                };
                foreach (var material in product.DressComponents)
                {
                    record.Materials.Add(new (material.Value.Item1.ComponentName, material.Value.Item2));
                    record.TotalAmount += material.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,
                CreationDate = x.DateCreate,
                DressName = x.DressName,
                Total = x.Sum,
                Status = x.Status.ToString(),                
            })
           .ToList();
        }

        public void SaveMaterialsToWordFile(ReportBindingModel model)
        {
            _saveToWord.CreateDoc(new WordInfo
            {
                FileName = model.FileName,
                Title = "Dresses list",
                Dresses = _dressStorage.GetFullList()
            });
        }

        public void SaveDressMaterialToExcelFile(ReportBindingModel model)
        {
            _saveToExcel.CreateReport(new ExcelInfo
            {
                FileName = model.FileName,
                Title = "Material list",
                DressComponents = GetDressMaterial()
            });
        }

        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)
            });
        }
    }
}
