
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels;

namespace HardwareShopContracts.BusinessLogicsContracts
{
    public class WorkerReportLogic : IWorkerReportLogic
    {

        private readonly IPurchaseStorage _purchaseStorage;

        public WorkerReportLogic(IPurchaseStorage purchaseStorage)
        {

            _purchaseStorage = purchaseStorage;
        }

        /// <summary>
        /// Получение списка компонент с указанием, в каких покупках используются
        /// </summary>
        /// <returns></returns>
        public List<ReportPurchaseComponentViewModel> GetPurchaseComponent(List<PurchaseViewModel> purchaseList)
        {
            var list = new List<ReportPurchaseComponentViewModel>();

            foreach (var purchase in purchaseList)
            {
                var record = new ReportPurchaseComponentViewModel
                {
                    Id = purchase.Id,
                    Builds = new List<(string Build, int count, List<(string Component, int count)>)>(),
                    TotalCount = 0,
                    TotalCost = purchase.Sum,
                };
                foreach (var build in purchase.PurchaseBuilds)
                {
                    List<(string Component, int count)> componentList = new List<(string Component, int count)>();
                    int buildTotalCount = 0;
                    foreach (var component in build.Value.Item1.BuildComponents)
                    {
                        componentList.Add(new(component.Value.Item1.ComponentName, component.Value.Item2));
                        buildTotalCount += component.Value.Item2;
                    }
                    record.Builds.Add(new(build.Value.Item1.BuildName, build.Value.Item2, componentList));
                    record.TotalCount += buildTotalCount * build.Value.Item2;
                }

                list.Add(record);
            }
            return list;
        }

        /// <summary>
        /// Получение списка покупок за определенный период
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<ReportPurchaseViewModel> GetPurchase(ReportBindingModel model, UserBindingModel userModel)
        {
            var list = new List<ReportPurchaseViewModel>();
            var purchases = _purchaseStorage.GetFilteredList(new PurchaseSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo, UserId = userModel.Id });

            foreach (var purchase in purchases)
            {
                var record = new ReportPurchaseViewModel
                {
                    Id = purchase.Id,
                    Builds = new List<(string Build, int count, List<string>, List<(string Component, int count)>)>(),
                };
                foreach (var build in purchase.PurchaseBuilds)
                {
                    List<string> commentList = new List<string>();
                    foreach (var comment in build.Value.Item1.BuildComments)
                    {
                        commentList.Add(new(comment.Value.Text));
                    }
                    List<(string Component, int count)> componentList = new List<(string Component, int count)>();
                    foreach (var component in build.Value.Item1.BuildComponents)
                    {
                        componentList.Add(new(component.Value.Item1.ComponentName, component.Value.Item2));
                    }
                    record.Builds.Add(new(build.Value.Item1.BuildName, build.Value.Item2, commentList, componentList));
                }
                list.Add(record);
            }
            return list;
        }


        /// <summary>
        /// Сохранение компонент с указаеним покупок в файл-Word
        /// </summary>
        /// <param name="model"></param>
        public void SaveComponentsToWordFile(ReportBindingModel model)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Сохранение компонент с указаеним покупок в файл-Excel
        /// </summary>
        /// <param name="model"></param>
        public void SaveDishComponentToExcelFile(ReportBindingModel model)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Сохранение отчёта по покупкам в файл-Pdf
        /// </summary>
        /// <param name="model"></param>
        public void SaveOrdersToPdfFile(ReportBindingModel model)
        {
            throw new NotImplementedException();
        }
    }
}