using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;
using Baza_Reklam.Classes.Helpers;
using Baza_Reklam.Classes.Interfaces;

namespace Baza_Reklam.Classes
{
    public class EmailSender : IEmailSender
    {
        private readonly ISmtpConfiguration _smtpConfiguration;
        private string _from;
        private List<string> _to = new List<string>();
        private string _subject;
        private string _body;

        public EmailSender(ISmtpConfiguration smtpConfiguration)
        {
            _smtpConfiguration = smtpConfiguration;
        }

        public EmailSender(string to, string subject, string body)
        {
            _from = ConfigurationHelper.GetSettingByKey("Application.Email.From");
            _to.Add(to);
            _subject = subject;
            _body = body;
        }

        public static void SendExceptonNotification(Exception loggingException, string exceptionMessage, string exceptionDetails, string userName, DateTime dateTime)
        {
            try
            {
                MailMessage message = new MailMessage();
                message.From = new MailAddress(ConfigurationHelper.GetSettingByKey("Application.Email.From") ?? "baza_reklam@ct.com.pl");
                message.To.Add(new MailAddress(ConfigurationHelper.GetSettingByKey("Application.Email.To") ?? "marek.stachura@ct.com.pl"));
                message.Subject = "Error Logging Exception - " + dateTime;

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Data i czas: " + dateTime);
                sb.AppendLine("Użytkownik: " + userName);
                sb.AppendLine("Exception.Message: " + exceptionMessage);
                sb.AppendLine(exceptionDetails);
                sb.AppendLine("***");
                sb.AppendLine("Logging.Exception.Message: " + loggingException.Message);
                sb.AppendLine(loggingException.ToString());

                message.Body = sb.ToString();
                SmtpClient client = new SmtpClient(ConfigurationHelper.GetSettingByKey("Application.Email.Smtp") ?? "poczta.ct.com.pl");
                client.SendAsync(message, null);
            }
            catch
            {
                //TODO: nothing what we can do now...
            }

        }

        public string From
        {
            get { return _from; }
            set { _from = value; }
        }

        public List<string> To
        {
            get { return _to; }
            set { _to = value; }
        }

        public List<string> Cc
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public List<string> Bcc
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public string Subject
        {
            get { return _subject; }
            set { _subject = value; }
        }

        public string Body
        {
            get { return _body; }
            set { _body = value; }
        }

        public ISmtpConfiguration SmtpConfiguration
        {
            get { return _smtpConfiguration; }
        }

        public void Send()
        {
            try
            {
                MailMessage message = new MailMessage();
                message.From = new MailAddress(From);
                foreach (string to in To)
                {
                    message.To.Add(new MailAddress(to));
                }
                
                message.Subject = Subject;
                message.Body = Body;
                SmtpClient client = new SmtpClient(SmtpConfiguration.Server);
                client.SendAsync(message, null);
            }
            catch
            {
                //TODO: nothing what we can do now...
            }
        }
    }
}