using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Vrh.OneMessage;

namespace Vrh.OneMessage
{
    /// <summary>
    /// Ebben az osztályban kell tárolni a küldő szerverhez kapcsolódás adatait.
    /// </summary>
    /// <remarks>
    /// A későbbiekben ezt kéne bővíteni, ha az SMTP protokollon kívűl mást is akarunk alkalmazni.
    /// </remarks>
    public class EmailServerConnection
    {
        private const string SC_HOST = "HOST";
        private const string SC_USER = "USER";
        private const string SC_PSSW = "PSW";
        private const string SC_PORT = "PORT";
        private const string SC_ESSL = "SSL";       //EnableSsl (true or false)
        private const string SC_DECR = "DEFCRE";    //UseDefaultCredentials (true or false)

        public string Host { get; set; }
        public int Port { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public bool EnableSsl { get; set; }
        public bool UseDefaultCredentials { get; set; }

        public EmailServerConnection(string connectionString)
        {
            Port = 25;
            EnableSsl = false;

            string[] connectionStringMemberList = connectionString.Split(';');
            foreach (string connectionStringMember in connectionStringMemberList.Where(x => !string.IsNullOrEmpty(x)))
            {
                if (connectionStringMember.IndexOf("=") <= 0)
                {
                    throw new ApplicationException($"The emailserverconnection string contains invalid member '{connectionStringMember}'!");
                }

                string[] connectionStringMemberNameValuePair = connectionStringMember.Split('=');

                string sckey = connectionStringMemberNameValuePair[0];
                string scvalue = connectionStringMemberNameValuePair[1];
                switch (sckey)
                {
                    case SC_HOST: Host = scvalue; break;
                    case SC_PORT: Port = Convert.ToInt32(scvalue); break;
                    case SC_USER: UserName = scvalue; break;
                    case SC_PSSW: Password = scvalue; break;
                    case SC_ESSL: EnableSsl = BooleanParse(scvalue); break;
                    case SC_DECR: UseDefaultCredentials = BooleanParse(scvalue); break;
                    default: throw new ApplicationException($"The emailserverconnection string contains invalid member '{connectionStringMember}'!");
                }
            }
            if (String.IsNullOrEmpty(Host)) throw new ApplicationException("The emailserverconnection string is invalid, member 'HOST' is missing!");
        }

        #region BooleanParse
        private const string BOOL_ON = "on";
        private const string BOOL_OFF = "off";
        private const string BOOL_TRUE = "true";
        private const string BOOL_FALSE = "false";

        private static bool BooleanParse(string value)
        {
            bool result = false;
            if (!String.IsNullOrEmpty(value))
            {
                value = value.ToLower();
                if (value != BOOL_ON && value != BOOL_OFF && value != BOOL_TRUE && value != BOOL_FALSE)
                {
                    throw new ApplicationException(String.Format("The possible values for a boolean attribute '{0}', '{1}', '{2}' or '{3}' !", BOOL_ON, BOOL_OFF, BOOL_TRUE, BOOL_FALSE));
                }
                result = value == BOOL_ON || value == BOOL_TRUE;
            }
            return result;
        }

        #endregion BooleanParse
    }
}
