using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.BusinessLogicContracts;
using CaseAccountingContracts.SearchModels;
using CaseAccountingContracts.StoragesContracts;
using CaseAccountingContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaseAccountingBusinessLogic.BusinessLogics
{
    public class ReportCustomerLogic : IReportCustomerLogic
    {
        private readonly ICaseStorage _caseStorage;
        private readonly IHearingStorage _hearingStorage;
        private readonly ILawyerStorage _lawyerStorage;
        private readonly ISpecializationStorage _specializationStorage;

        public ReportCustomerLogic(ICaseStorage caseStorage, IHearingStorage hearingStorage, ILawyerStorage lawyerStorage, ISpecializationStorage specializationStorage)
        {
            _caseStorage = caseStorage;
            _hearingStorage = hearingStorage;
            _lawyerStorage = lawyerStorage;
            _specializationStorage = specializationStorage;
        }

        public List<ReportHearingSpecializationViewModel> GetHearingSpecialization(ReportBindingModel model)
        {
            var result = _hearingStorage
                .GetFilteredList(new HearingSearchModel { UserId = model.UserId })
                .Where(x => model.DateFrom <= x.Date && model.DateTo >= x.Date)
                .Select(hearing => new ReportHearingSpecializationViewModel
                {
                    Information = hearing.Information,
                    Date = hearing.Date,
                    Specialization = _caseStorage.GetElement(new CaseSearchModel
                    {
                        Id = hearing.CaseId
                    })?
                    .Specialization ?? "Не удалось найти специализацию."
                })
                .ToList();
            return result;
        }

        public List<ReportLawyerHearingViewModel> GetLawyerHearing(List<LawyerViewModel> models)
        {
            var lawyers = new List<LawyerViewModel>();
            foreach (var model in models)
            {
                lawyers.Add(_lawyerStorage.GetElement(new LawyerSearchModel { Id = model.Id}));
            }
            var list = new List<ReportLawyerHearingViewModel>();

            foreach(var lawyer in lawyers)
            {
                var record = new ReportLawyerHearingViewModel
                {
                    Name = lawyer.Name,
                    Surname = lawyer.Surname,
                    Patronymic = lawyer.Patronymic,
                    Hearings = new List<(DateTime Date, string Information)>()
                };
                var cases = _caseStorage.GetFullList()
               .Where(x => x.Lawyers.ContainsKey(lawyer.Id));
                foreach (var _case in cases)
                {
                    record.Hearings.Add((_hearingStorage.GetElement(new HearingSearchModel { CaseId = _case.Id}).Date,
                        _hearingStorage.GetElement(new HearingSearchModel { CaseId = _case.Id })?.Information ?? "Не удалось найти информацию."));
                }
                list.Add(record);
            }
            return list;
        }

        public void SaveHearingSpecializationToPdfFile(ReportBindingModel model)
        {
            throw new NotImplementedException();
        }

        public void SaveLawyerHearingToExcelFile(ReportBindingModel model)
        {
            throw new NotImplementedException();
        }

        public void SaveLawyerHearingToWordFile(ReportBindingModel model)
        {
            throw new NotImplementedException();
        }
    }
}
