using CaseAccountingBusinessLogic.OfficePackage;
using CaseAccountingBusinessLogic.OfficePackage.HelperModels;
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;
        private readonly WordBuilderCustomer _wordBuilder;
        private readonly ExcelBuilderCustomer _excelBuilder;

        public ReportCustomerLogic(ICaseStorage caseStorage, IHearingStorage hearingStorage, ILawyerStorage lawyerStorage, ISpecializationStorage specializationStorage, 
            WordBuilderCustomer wordBuilder, ExcelBuilderCustomer excelBuilder)
        {
            _caseStorage = caseStorage;
            _hearingStorage = hearingStorage;
            _lawyerStorage = lawyerStorage;
            _specializationStorage = specializationStorage;
            _wordBuilder = wordBuilder;
            _excelBuilder = excelBuilder;
        }

        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> GetLawyersHearing(List<LawyerViewModel> lawyers)
        {
            var reportRecords = new List<ReportLawyerHearingViewModel>();
            foreach (var lawyer in lawyers)
            {
                var hearings = _lawyerStorage.GetLawyerCases(new() { Id = lawyer.Id })
                    .SelectMany(_case => _caseStorage.GetCaseHearings(new() { Id = _case.Id }))
                    .Select(hearing => hearing.Information)
                    .ToList();
                ReportLawyerHearingViewModel reportRecord = new()
                {
                    Lawyer = lawyer.Name + " " + lawyer.Surname + ", " + lawyer.Patronymic,
                    Hearings = hearings
                };
                reportRecords.Add(reportRecord);
            }
            return reportRecords;
        }

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

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

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

        public byte[] SaveListFile(LawyerHearingListBindingModel model)
        {
            byte[] file = Array.Empty<byte>();

            string title = "Список слушаний по выбранным юристам";

            if (model.FileType == "docx")
            {
                _wordBuilder.CreateDocument();
                _wordBuilder.CreateTitle(title);
                _wordBuilder.CreateLawyersHearingTable(GetLawyersHearing(model.Lawyers));
                file = _wordBuilder.GetFile();
            }
            else if (model.FileType == "xlsx")
            {
                _excelBuilder.CreateDocument();
                _excelBuilder.CreateTitle(title);
                _excelBuilder.CreateLawyersHearingTable(GetLawyersHearing(model.Lawyers));
                file = _excelBuilder.GetFile();
            }
            return file;
        }
    }
}
