using CoviDok.Api.Objects;
using CoviDok.BLL;
using CoviDok.BLL.User.Managers;
using CoviDok.Data.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoviDok.Data.MySQL
{
    public class MySqlDoctorHandler : IdoctorHandler
    {
        private readonly MySqlContext context = new MySqlContext();

        public bool DoctorExists(int id)
        {
            return context.Doctors.Any(e => e.Id == id);
        }

        public List<Assistant> GetAssistants(int id)
        {
            return (from a in context.Assistants where a.DoctorId == id select a).ToList();
        }

        public List<Child> GetChildren(int id)
        {
            return (from a in context.Children where a.DoctorId == id select a).ToList();
        }

        public async Task<Doctor> GetDoctor(int id)
        {
            return await context.Doctors.FindAsync(id);
        }

        public List<Doctor> GetDoctors(string firstName = null, string lastName = null)
        {
            if (firstName != null && lastName != null)
            {
                return (from d in context.Doctors where d.FirstName.ToLower().Contains(firstName.ToLower()) && d.LastName.ToLower().Contains(lastName.ToLower()) select d).ToList();
            }
            if (firstName != null)
            {
                return (from d in context.Doctors where d.FirstName.ToLower().Contains(firstName.ToLower()) select d).ToList();
            }
            if (lastName != null)
            {
                return (from d in context.Doctors where d.LastName.ToLower().Contains(lastName.ToLower()) select d).ToList();
            }
            return context.Doctors.ToList();
        }

        public async Task UpdateDoctor(int id, Doctor value)
        {
            Doctor doctor = await context.Doctors.FindAsync(id);
            context.Entry(doctor).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            PropertyCopier<Doctor>.Copy(value, doctor);
            await context.SaveChangesAsync();
        }
    }
}