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 ReportProviderLogic : IReportProviderLogic
    {
        private readonly ICaseStorage _caseStorage;
        private readonly ISpecializationStorage _specializationStorage;
        private readonly IHearingStorage _hearingStorage;
        private readonly ILawyerStorage _lawyerStorage;

        public ReportProviderLogic(ICaseStorage caseStorage, ISpecializationStorage specializationStorage, IHearingStorage hearingStorage, ILawyerStorage lawyerStorage)
        {
            _caseStorage = caseStorage ?? throw new ArgumentNullException(nameof(caseStorage));
            _specializationStorage = specializationStorage ?? throw new ArgumentNullException(nameof(specializationStorage));
            _hearingStorage = hearingStorage ?? throw new ArgumentNullException(nameof(hearingStorage));
            _lawyerStorage = lawyerStorage ?? throw new ArgumentNullException(nameof(lawyerStorage));
        }

        public List<ReportCaseSpecializationViewModel> GetCaseSpecialization(List<CaseViewModel> models)
        {
            var сases = new List<CaseViewModel>();
            foreach (var model in models)
                сases.Add(_caseStorage.GetElement(new CaseSearchModel { Id = model.Id }));
            return сases.Select(x => new ReportCaseSpecializationViewModel {
                CaseName = x.Name,
                Applicant = x.Applicant,
                Defendant = x.Defendant,
                Date = x.Date,
                Specialization = x.Specialization,
            }).ToList();
        }

        public List<ReportHearingLawyerViewModel> GetHearingLawyer(ReportBindingModel model)
        {
            var hearings = _hearingStorage
                .GetFilteredList(new HearingSearchModel { UserId = model.UserId})
                .Where(x => model.DateFrom <= x.Date && model.DateTo >= x.Date);
            var list = new List<ReportHearingLawyerViewModel>();
            foreach (var hearing in hearings)
            {
                var record = new ReportHearingLawyerViewModel
                {
                    Information = hearing.Information,
                    Date = hearing.Date,
                    Lawyers = new List<(string Surname, string Name, string Patronymic)>()
                };
                foreach (var lawyer in _caseStorage.GetElement(new CaseSearchModel { Id = hearing.CaseId }).Lawyers.Values)
                {
                    record.Lawyers.Add((lawyer.Surname, lawyer.Name, lawyer.Patronymic));
                }
                list.Add(record);
            }
            return list;
        }

        public void SaveCaseSpecializationToExcelFile(ReportBindingModel model)
        {
            throw new NotImplementedException();
            //TODO
        }

        public void SaveCaseSpecializationToWordFile(ReportBindingModel model)
        {
            throw new NotImplementedException();
            //TODO
        }

        public void SaveHearingLawyerToPdfFile(ReportBindingModel model)
        {
            throw new NotImplementedException();
            //TODO
        }
    }
}
