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;

namespace UniversityBusinessLogic.BusinessLogics
{
    public class ReportProviderLogic : IReportProviderLogic
    {
        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 ReportProviderLogic(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<ReportStudentsDisciplineViewModel> GetStudentsDiscipline(List<StudentViewModel> students)
        {
            var result = students
                .Select(student => new ReportStudentsDisciplineViewModel
                {
                    StudentFIO = $"{student.Name} {student.Surname}",
                    Disciplines = _streamStorage.GetFullList()
                        .Where(stream => stream.StudentStream.ContainsKey(student.Id))
                        .Join(_disciplineStorage.GetFullList(),
                            stream => stream.Id,
                            discipline => discipline.StreamId,
                            (stream, discipline) => discipline.Name)
                        .ToList()
                })
                .ToList();

            return result;
        }
        public List<ReportStreamStudentEdStatPeriodViewModel> StreamStudentEdStatPeriod(ReportBindingModel model)
        {
            var result = _streamStorage
                .GetFilteredList(new StreamSearchModel { UserId = model.UserId })
                .Select(stream => new ReportStreamStudentEdStatPeriodViewModel
                {
                    StreamName = stream.Name,
                    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)))//Выбираем студентов, которые есть в приказах за выбранный промежуток времени
                        .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();
            //TODO
        }

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

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