using UnivateProperties_API.Helpers;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Net.Mail;
using System.Text;

namespace UnivateProperties_API.Model.Communication
{
    public class Email : BaseEntity
    {
        #region Constructor
        public Email()
        {
            
        }

        public Email(int senderId)
        {
            SenderId = senderId;
        }

        public Email(int senderId, string toAddress, string toDisplay, string cc, string bcc, string subject, string body, bool isHtml = true)
        {
            SenderId = senderId;
            To = toAddress;
            ToDisplay = toDisplay;
            BCC = bcc;
            CC = cc;
            IsBodyHtml = isHtml;
            Body = body;
            Subject = subject;
        }
        #endregion Constructor

        #region Properties
        [ForeignKey("Sender")]
        public int SenderId { get; set; }
        public string Comment { get; set; }
        public string Subject { get; set; }
        public bool IsBodyHtml { get; set; }
        public string CC { get; set; }
        public string BCC { get; set; }
        public string Body { get; set; }
        public string To { get; set; }
        public string ToDisplay { get; set; }

        public virtual SMTPAccount Sender { get; set; }
        #endregion

        #region Methods
        public virtual void GetSMTPAccount()
        {
            if (SenderId > 0)
            {

            }
        }

        public virtual bool SendMail()
        {
            try
            {
                if (MyCommon.IsValidEmail(To))
                {
                    if (Sender != null && Sender.SMTPHost != null)
                    {
                        using (SmtpClient smtp = new SmtpClient(Sender.SMTPHost.Host))
                        {
                            MailMessage mail = GetMailMessage();
                            smtp.UseDefaultCredentials = Sender.SMTPHost.NeedsAuthorize;
                            smtp.Credentials = Sender.SMTPHost.GetNetworkCredential();
                            smtp.EnableSsl = Sender.SMTPHost.UseSSL;
                            smtp.Send(mail);
                            Comment = "Send";
                            return true;
                        };
                    }
                    else Comment = "NoSender";
                }
            }
            catch (SmtpFailedRecipientException ex)
            {
                SmtpStatusCode statusCode = ex.StatusCode;

                if (statusCode == SmtpStatusCode.MailboxBusy ||
                    statusCode == SmtpStatusCode.MailboxUnavailable ||
                    statusCode == SmtpStatusCode.TransactionFailed)
                {
                    Comment = $"SmtpFailedRecipientException - Msg - {ex.Message} Stack - {ex.StackTrace}";
                    //TODO: Return correct error message
                    return false;
                }
                else
                {
                    Comment = $"SmtpFailedRecipientException - Msg - {ex.Message} Stack - {ex.StackTrace}";
                    //TODO: Return correct error message
                    return false;
                }
            }
            catch (Exception ex)
            {
                Comment = $"Exception - Msg - {ex.Message} Stack - {ex.StackTrace}";
                //TODO: Return correct error message
                return false;
            }
            return true;
        }

        private MailMessage GetMailMessage()
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(new MailAddress(To, ToDisplay));
            if (!string.IsNullOrEmpty(CC))
            {
                foreach (var item in CC.Split(';'))
                {
                    mail.CC.Add(item);
                }
            }
            if (!string.IsNullOrEmpty(BCC))
            {
                foreach (var item in BCC.Split(';'))
                {
                    mail.Bcc.Add(item);
                }
            }
            mail.Subject = Subject;
            mail.Body = Body;
            mail.IsBodyHtml = IsBodyHtml;
            if (IsBodyHtml)
            {
                mail.BodyEncoding = Encoding.ASCII;
            }
            else mail.BodyEncoding = Encoding.UTF8;
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            mail.Sender = new MailAddress(Sender.Address, Sender.DisplayName);
            mail.From = mail.Sender;
            return mail;
        }
        #endregion Methods
    }
}