using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using SPWA.DataAccess.Data.MJDbContext;
using SPWA.DataAccess.Data.Models;

namespace SPWA.DataAccess.Data.EmailService
{
    public class EmailService
    {


        private readonly MJContext _context;
        public EmailService(MJContext context)
        {
            _context = context;
        }


        public Task <List<Email>> GetEmailAsync(string strCurrentUser)
        {
            List<Email> ColEmail = new List<Email>();
            ColEmail = (from email in _context.Email select email).ToList();
            return Task.FromResult(ColEmail);
        }

        public Task<Email> CreateEmailAsync(Email objEmail)
        {
            _context.Email.Add(objEmail);
            _context.SaveChanges();
            return Task.FromResult(objEmail);
        }


        public Task<bool> UpdateEmailAsync(Email objEmail)
        {
            var ExistingEmail =
                _context.Email
                .Where(x => x.EmailID == objEmail.EmailID)
                .FirstOrDefault();
            if (ExistingEmail != null)
            {
                ExistingEmail.Address =
                    objEmail.Address;
                ExistingEmail.TermID =
                    objEmail.TermID;
                ExistingEmail.UserId =
                    objEmail.UserId;
                ExistingEmail.TemplateID =
                    objEmail.TemplateID;

                _context.SaveChanges();
            }
            else
            {
                return Task.FromResult(false);
            }
            return Task.FromResult(true);
        }



    }
}
