using Demo.Data.Repository;
using Demo.Data.RemoteData.RemoteDataBase.DAO;
using Demo.Domain.Models;
using System;
using System.Linq;

namespace Demo.Domain.UseCase
{
    public class UseCaseGeneratePresence
    {
        private readonly IPresenceRepository _presenceRepository;
        private readonly IUserRepository _userRepository;

        public UseCaseGeneratePresence(IPresenceRepository presenceRepository, IUserRepository userRepository)
        {
            _presenceRepository = presenceRepository ?? throw new ArgumentNullException(nameof(presenceRepository));
            _userRepository = userRepository ?? throw new ArgumentNullException(nameof(userRepository));
        }

        public void GeneratePresenceForToday(int firstLesson, int lastLesson, int groupId, DateTime date)
        {
            date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
            var users = _userRepository.GetAllUsers().Where(u => u.GroupID == groupId);
            foreach (var user in users)
            {
                for (int lesson = firstLesson; lesson <= lastLesson; lesson++)
                {
                    var daoPresence = new Demo.Data.RemoteData.RemoteDataBase.DAO.Presence
                    {
                        Id = Guid.NewGuid(),
                        Date = date,
                        LessonNumber = lesson,
                        IsAttendance = true,
                        UserId = user.Id
                    };

                    var domainPresence = new Demo.Domain.Models.Presence
                    {
                        Id = daoPresence.Id,
                        Date = daoPresence.Date,
                        LessonNumber = daoPresence.LessonNumber,
                        IsAttendance = daoPresence.IsAttendance,
                        UserId = daoPresence.UserId
                    };

                    _presenceRepository.AddPresence(domainPresence);
                }
            }
        }

        public void GeneratePresenceForWeek(int firstLesson, int lastLesson, int groupId, DateTime startDate)
        {
            startDate = DateTime.SpecifyKind(startDate, DateTimeKind.Utc);
            for (int i = 0; i < 7; i++)
            {
                GeneratePresenceForToday(firstLesson, lastLesson, groupId, startDate.AddDays(i));
            }
        }
    }
}