using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicContracts;
using UniversityContracts.ViewModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityBusinessLogic.OfficePackage;
using System.Reflection;
using System.Net;
using System.Reflection.Metadata;

namespace UniversityBusinessLogic.BusinessLogics
{
    public class ReportCustomerLogic : IReportCustomerLogic
    {
        private readonly IDocumentStorage _documentStorage;
        private readonly IStudentStorage _studentStorage;
        private readonly IEducationStatusStorage _educationStatusStorage;
        private readonly IEducationGroupStorage _educationGroupStorage;
        private readonly IDisciplineStorage _disciplineStorage;
        private readonly IStreamStorage _streamStorage;
        private readonly AbstractSaveToExcelProvider _saveToExcel;
        private readonly AbstractSaveToWordProvider _saveToWord;
        private readonly AbstractSaveToPdfProvider _saveToPdf;

        public ReportCustomerLogic(IDocumentStorage documentStorage,
            IStudentStorage studentStorage,
            IEducationStatusStorage educationStatusStorage,
            IEducationGroupStorage educationGroupStorage,
            IDisciplineStorage disciplineStorage,
            IStreamStorage streamStorage,
            AbstractSaveToExcelProvider saveToExcel,
            AbstractSaveToWordProvider saveToWord,
            AbstractSaveToPdfProvider saveToPdf)
        {
            _documentStorage = documentStorage;
            _studentStorage = studentStorage;
            _educationStatusStorage = educationStatusStorage;
            _educationGroupStorage = educationGroupStorage;
            _disciplineStorage = disciplineStorage;
            _streamStorage = streamStorage;
            _saveToExcel = saveToExcel;
            _saveToWord = saveToWord;
            _saveToPdf = saveToPdf;
        }

        public List<ReportDisciplineViewModel> GetDiscipline(ReportBindingModel model)
        {
            var result = _streamStorage.GetFilteredList(new StreamSearchModel { Id = _disciplineStorage.GetElement(new DisciplineSearchModel { Name = model.DisciplineName })?.StreamId })
                .Select(stream => new ReportDisciplineViewModel
                {
                DisciplineName = model.DisciplineName,
                StudentEdStatus = stream.StudentStream
                        .Where(student => _documentStorage.GetFilteredList(new DocumentSearchModel
                        {
                            UserId = model.UserId,
                            DateFrom = model.DateFrom,
                            DateTo = model.DateTo,
                        })
                        .Any(document => document.StudentDocument.ContainsKey(student.Value.Id)))//Выбираем студентов, которые есть в приказах за выбранный промежуток времени
                        .Join(_documentStorage.GetFullList(),
                            t1 => t1.Value.Id,
                            t2 => t2.UserId,
                            (t1, t2) => new { student = t1, document = t2 })
                        .Select(res => (
                            StudentFIO: $"{res.student.Value.Name} {res.student.Value.Surname}",
                            Document: $"{res.document.Date}",
                            EdStatus: _educationStatusStorage.GetElement(new EducationStatusSearchModel { Id = res.student.Value.EducationStatusId })?.Name ?? "не удалось получить"))
                        .ToList()
                })
                .ToList();
            return result;
        }
        public List<ReportStreamEducationStatusViewModel> StreamEducationStatus(List<StreamViewModel> streams)
        {
            var result = streams
                .Select(stream => new ReportStreamEducationStatusViewModel
                {
                    StreamName = stream.Name,
                    StudentEdStatus = stream.StudentStream
                        .Select(student => (
                            StudentFIO: $"{student.Value.Name} {student.Value.Surname}",
                            EdStatus: _educationStatusStorage.GetElement(new EducationStatusSearchModel { Id = student.Value.EducationStatusId })?.Name ?? "не удалось получить"))
                        .ToList()
                })
                .ToList();
            return result;
        }


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

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

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