using MEU.API.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;

namespace MEU.API.Services.Emails.Templates
{
    public abstract class EmailContent
    {
        public string EmailTemplatePath = Environment.CurrentDirectory + "/Resources/EmailTemplates";
        public string Subject { get; set; }
        public string Reciever { get; set; }

        public abstract string GetContent();
        public abstract bool Send();
        public abstract Task SendAsync();

        private string smtpserver = ConfigEnvironment.MAILSERVER_SMTPHOST;
        private int smtpmailport = Int32.Parse(ConfigEnvironment.MAILSERVER_SMTPPORT);
        private string username = ConfigEnvironment.MAILSERVER_USERNAME;
        private string password = ConfigEnvironment.MAILSERVER_PASSWORD;

        protected bool SendEmail(string Receiver, string Subject, string Body)
        {
            SmtpClient client = new SmtpClient(smtpserver, smtpmailport);
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(this.username, this.password);

            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(username);
            mailMessage.To.Add(Receiver);
            mailMessage.Body = Body;
            mailMessage.Subject = Subject;
            client.Send(mailMessage);
            return true;

        }


        protected async Task<bool> SendEmailAsync(string Receiver, string Subject, string Body)
        {
            SmtpClient client = new SmtpClient(smtpserver, smtpmailport);
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(this.username, this.password);

            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(username);
            mailMessage.To.Add(Receiver);
            mailMessage.Body = Body;
            mailMessage.IsBodyHtml = true;
            mailMessage.Subject = Subject;
            await client.SendMailAsync(mailMessage);
            return true;

        }

    }
}
