using Microsoft.Exchange.WebServices.Data;

namespace InABox.Mail
{
    public class ExchangeMailSummary : ICoreMailSummary
    {
        public string ID => throw new NotImplementedException();
        public DateTime Date => throw new NotImplementedException();

        public string From
        {
            get => throw new NotImplementedException();
            set => throw new NotImplementedException();
        }

        public string Subject
        {
            get => throw new NotImplementedException();
            set => throw new NotImplementedException();
        }

        public IEnumerable<string> To
        {
            get => throw new NotImplementedException();
            set => throw new NotImplementedException();
        }
    }

    public class ExchangeMailMessage : ICoreMailMessage
    {
        private Tuple<string, byte[]>[] _attachments = { };

        private ExchangeMailMessage()
        {
        }

        public ExchangeMailMessage(EmailMessage message)
        {
            Message = message;
        }

        public EmailMessage Message { get; }

        public void Save()
        {
            Message.Save();
        }

        public string ID
        {
            get => Message.Id.UniqueId;
            set => throw new NotImplementedException();
        }

        public DateTime Date
        {
            get => Message.DateTimeReceived;
            set => throw new NotImplementedException();
        }

        public string From
        {
            get => Message.From.Address;
            set => Message.From.Address = value;
        }

        public IEnumerable<string> To
        {
            get { return Message.ToRecipients.Select(x => x.Address); }
            set
            {
                Message.ToRecipients.Clear();
                Message.ToRecipients.AddRange(value);
            }
        }

        public IEnumerable<string> CC
        {
            get { return Message.CcRecipients.Select(x => x.Address); }
            set
            {
                Message.CcRecipients.Clear();
                Message.CcRecipients.AddRange(value);
            }
        }

        public IEnumerable<string> BCC
        {
            get { return Message.BccRecipients.Select(x => x.Address); }
            set
            {
                Message.BccRecipients.Clear();
                Message.BccRecipients.AddRange(value);
            }
        }

        public string Subject
        {
            get => Message.Subject;
            set => Message.Subject = value;
        }

        public string Body
        {
            get => Message.Body;
            set => Message.Body = value;
        }

        public IEnumerable<Tuple<string, byte[]>> Attachments
        {
            get => _attachments;
            set
            {
                _attachments = value.ToArray();
                Message.Attachments.Clear();
                foreach (var attach in value)
                    Message.Attachments.AddFileAttachment(attach.Item1, attach.Item2);
            }
        }

        public bool IsHTML
        {
            get => Message.Body.BodyType == BodyType.HTML;
            set => Message.Body.BodyType = value ? BodyType.HTML : BodyType.Text;
        }
    }
}