using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Web;

namespace Fab2ApprovalSystem.Misc
{
    public class EmailNotification
    {
        #region Variabls
        protected string _subject = null;
        protected string _TemplatesPath = null;
        #endregion

        /// <summary>
        /// Email subject
        /// </summary>
        public string EmailSubject
        {
            set { _subject = value; }

        }

        /// <summary>
        /// This function will read the content of a file name
        /// </summary>
        /// <param name="FileName">File Name</param>
        /// <returns>String: Containing the Entire content of the file</returns>
        protected string ReadEmailFile(string FileName)
        {
            string retVal = null;
            try
            {
                //setting the file name path
                string path = _TemplatesPath + FileName;
                FileInfo TheFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(path));

                //check if the file exists in the location.
                if (!TheFile.Exists)
                    throw new Exception("Could Not Find the file : " + FileName + " in the location " + _TemplatesPath); // throw an exception here.

                //start reading the file. i have used Encoding 1256 to support arabic text also.
                StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(@path), System.Text.Encoding.GetEncoding(1256));
                retVal = sr.ReadToEnd(); // getting the entire text from the file.
                sr.Close();
            }


            catch (Exception ex)
            {
                throw new Exception("Error Reading File." + ex.Message);


            }
            return retVal;
        }
        /// <summary>
        /// this function will send email. it will read the mail setting from the web.config
        /// </summary>
        /// <param name="SenderEmail">Sender Email ID</param>
        /// <param name="SenderName">Sender Name</param>
        /// <param name="Recep">Recepient Email ID</param>
        /// <param name="cc">CC ids</param>
        /// <param name="email_title">Email Subject</param>
        /// <param name="email_body">Email Body</param>
        protected void SendEmail(string SenderEmail, string SenderName, string Recep, string cc, string email_title, string email_body)
        {
            // creating email message
            MailMessage msg = new MailMessage();
            msg.IsBodyHtml = true;// email body will allow html elements

            // setting the Sender Email ID
            //msg.From = new MailAddress(SenderEmail, SenderName);
            msg.From = new MailAddress("MesaFabApproval@infineon.com", "Mesa Fab Approval");
            msg.Sender = new MailAddress("MesaFabApproval@infineon.com", "Mesa Fab Approval");
            // adding the Recepient Email ID
            msg.To.Add(Recep);
            //msg.To.Add("Jonathan.Ouellette@infineon.com");
            // add CC email ids if supplied.
            if (!string.IsNullOrEmpty(cc))
                msg.CC.Add(cc);

            //setting email subject and body
            msg.Subject = email_title;
            msg.Body = email_body;

           

                //create a Smtp Mail which will automatically get the smtp server details from web.config mailSettings section
                SmtpClient SmtpMail = new SmtpClient("mailrelay-external.infineon.com");

            // sending the message.
            try
            {
                SmtpMail.Send(msg);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
                    ex.ToString());
            }




        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="SenderEmail"></param>
        /// <param name="SenderName"></param>
        /// <param name="Recep"></param>
        /// <param name="cc"></param>
        /// <param name="email_title"></param>
        /// <param name="email_body"></param>
        /// <param name="attachmentPath"></param>
        protected void SendEmailWithAttachment(string SenderEmail, string SenderName, string Recep, string cc, string email_title, string email_body, string attachmentPath)
        {
            // creating email message
            MailMessage msg = new MailMessage();
            msg.IsBodyHtml = true;// email body will allow html elements

            // setting the Sender Email ID
            msg.From = new MailAddress(SenderEmail, SenderName);

            // adding the Recepient Email ID
            msg.To.Add(Recep);

            // add CC email ids if supplied.
            if (!string.IsNullOrEmpty(cc))
                msg.CC.Add(cc);

            //setting email subject and body
            msg.Subject = email_title;
            msg.Body = email_body;
            msg.Attachments.Add(new Attachment(attachmentPath));


            //create a Smtp Mail which will automatically get the smtp server details from web.config mailSettings section
            SmtpClient SmtpMail = new SmtpClient();
#if(!DEBUG)
            // sending the message.
            SmtpMail.Send(msg);
#endif



        }


       


        /// <summary>
        /// 
        /// </summary>
        /// <param name="SenderEmail"></param>
        /// <param name="SenderName"></param>
        /// <param name="RecepientList"></param>
        /// <param name="cc"></param>
        /// <param name="email_title"></param>
        /// <param name="email_body"></param>
        protected void SendEmail(string SenderEmail, string SenderName, List<string> RecepientList, string cc, string email_title, string email_body)
        {
            // creating email message
            MailMessage msg = new MailMessage();
            msg.IsBodyHtml = true;// email body will allow html elements

            // setting the Sender Email ID
            //msg.From = new MailAddress(SenderEmail, SenderName);
            msg.From = new MailAddress("MesaFabApproval@infineon.com", "Mesa Fab Approval");
            // adding the Recepient Email ID
            foreach (string recepient in RecepientList)
            {
                msg.To.Add(recepient);
            }

            // add CC email ids if supplied.
            if (!string.IsNullOrEmpty(cc))
                msg.CC.Add(cc);

            //setting email subject and body
            msg.Subject = email_title;
            msg.Body = email_body;

            
                //create a Smtp Mail which will automatically get the smtp server details from web.config mailSettings section
                SmtpClient SmtpMail = new SmtpClient();

            // sending the message.
            try
            {
                SmtpMail.Send(msg);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
                    ex.ToString());
            }
                
           



        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="SenderEmail"></param>
        /// <param name="SenderName"></param>
        /// <param name="RecepientList"></param>
        /// <param name="cc"></param>
        /// <param name="email_title"></param>
        /// <param name="email_body"></param>
        /// <param name="attachmentPath"></param>
        protected void SendEmailWithAttachment(string SenderEmail, string SenderName, List<string> RecepientList, string cc, string email_title, string email_body, string attachmentPath)
        {
            // creating email message
            MailMessage msg = new MailMessage();
            msg.IsBodyHtml = true;// email body will allow html elements

            // setting the Sender Email ID
            msg.From = new MailAddress(SenderEmail, SenderName);

            // adding the Recepient Email ID
            foreach (string recepient in RecepientList)
            {
                if (recepient != null)
                    msg.To.Add(recepient);
            }

            // add CC email ids if supplied.
            if (!string.IsNullOrEmpty(cc))
                msg.CC.Add(cc);

            //setting email subject and body
            msg.Subject = email_title;
            msg.Body = email_body;
            msg.Attachments.Add(new Attachment(attachmentPath));


            //create a Smtp Mail which will automatically get the smtp server details from web.config mailSettings section
            SmtpClient SmtpMail = new SmtpClient();

            // sending the message.
            SmtpMail.Send(msg);




        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="SenderEmail"></param>
        /// <param name="SenderName"></param>
        /// <param name="Recep"></param>
        /// <param name="cc"></param>
        /// <param name="email_title"></param>
        /// <param name="email_body"></param>
        /// <param name="attachments"></param>
        protected void SendEmailWithAttachments(string SenderEmail, string SenderName, string Recep, string cc, string email_title, string email_body, List<string> attachments)
        {
            // creating email message
            MailMessage msg = new MailMessage();
            msg.IsBodyHtml = true;// email body will allow html elements

            // setting the Sender Email ID
            msg.From = new MailAddress(SenderEmail, SenderName);

            // adding the Recepient Email ID
            msg.To.Add(Recep);

            // add CC email ids if supplied.
            if (!string.IsNullOrEmpty(cc))
                msg.CC.Add(cc);

            //setting email subject and body
            msg.Subject = email_title;
            msg.Body = email_body;
            foreach (string attachment in attachments)
            {
                msg.Attachments.Add(new Attachment(attachment));
            }


            //create a Smtp Mail which will automatically get the smtp server details from web.config mailSettings section
            SmtpClient SmtpMail = new SmtpClient();
#if(!DEBUG)
            // sending the message.
            SmtpMail.Send(msg);
#endif



        }

       /// <summary>
       /// 
       /// </summary>
       /// <param name="SenderEmail"></param>
       /// <param name="SenderName"></param>
       /// <param name="RecepientList"></param>
       /// <param name="cc"></param>
       /// <param name="email_title"></param>
       /// <param name="email_body"></param>
       /// <param name="attachments"></param>
        protected void SendEmailWithAttachments(string SenderEmail, string SenderName, List<string> RecepientList, string cc, string email_title, string email_body, List<string> attachments)
        {
            // creating email message
            MailMessage msg = new MailMessage();
            msg.IsBodyHtml = true;// email body will allow html elements

            // setting the Sender Email ID
            msg.From = new MailAddress(SenderEmail, SenderName);

            // adding the Recepient Email ID
            foreach (string recepient in RecepientList)
            {
                if (recepient != null)
                    msg.To.Add(recepient);
            }

            // add CC email ids if supplied.
            if (!string.IsNullOrEmpty(cc))
                msg.CC.Add(cc);

            //setting email subject and body
            msg.Subject = email_title;
            msg.Body = email_body;

            foreach (string attachment in attachments)
            {
                msg.Attachments.Add(new Attachment(attachment));
            }


            //create a Smtp Mail which will automatically get the smtp server details from web.config mailSettings section
            SmtpClient SmtpMail = new SmtpClient();

            // sending the message.
            SmtpMail.Send(msg);




        }


        public EmailNotification()
        {

        }


        /// <summary>
        /// The Constructor Function
        /// </summary>
        /// <param name="EmailHeaderSubject">Email Header Subject</param>
        /// <param name="TemplatesPath">Emails Files Templates</param>
        public EmailNotification(string EmailHeaderSubject, string TemplatesPath)
        {
            _subject = EmailHeaderSubject;
            _TemplatesPath = TemplatesPath;

        }



        /// <summary>
        /// This function will send the email notification by reading the email template and substitute the arguments
        /// </summary>
        /// <param name="EmailTemplateFile">Email Template File</param>
        /// <param name="SenderEmail">Sender Email</param>
        /// <param name="SenderName">Sender Name</param>
        /// <param name="RecepientEmail">Recepient Email ID</param>
        /// <param name="CC">CC IDs</param>
        /// <param name="Subject">EMail Subject</param>
        /// <param name="Args">Arguments</param>
        /// <returns>String: Return the body of the email to be send</returns>
        public string SendNotificationEmail(string EmailTemplateFile, string SenderEmail, string SenderName, string RecepientEmail, string CC, string Subject, params string[] Args)
        {
            string retVal = null;

            //reading the file
            string FileContents = ReadEmailFile(EmailTemplateFile);
      
            string emailBody = FileContents;

            //setting formatting the string
            retVal = string.Format(emailBody, Args);

            try
            {
                //check if we are in debug mode or not. to send email
                if (GlobalVars.DBConnection.ToUpper() == "TEST" || GlobalVars.DBConnection.ToUpper() == "QUALITY")
                {
                    SendEmail(SenderEmail, SenderName, GetTestRecipientsList(), CC, (!string.IsNullOrEmpty(Subject) ? Subject + " for: " + RecepientEmail : _subject), retVal);
                }
                else
                {
#if(!DEBUG)
                    SendEmail(SenderEmail, SenderName, RecepientEmail, CC, (!string.IsNullOrEmpty(Subject) ? Subject  : _subject), retVal);
#endif
                }
            }
            catch (Exception ex)
            {
                 throw ex;
            }

            return retVal;


        }


        /// <summary>
        /// This function will send the email notification by reading the email template and substitute the arguments along with the attachments
        /// </summary>
        /// <param name="EmailTemplateFile"></param>
        /// <param name="SenderEmail"></param>
        /// <param name="SenderName"></param>
        /// <param name="RecepientEmail"></param>
        /// <param name="CC"></param>
        /// <param name="Subject"></param>
        /// <param name="attachmentPath"></param>
        /// <param name="Args"></param>
        /// <returns></returns>
        public string SendNotificationEmailWithAttachment(string EmailTemplateFile, string SenderEmail, string SenderName, string RecepientEmail, string CC, string Subject, string attachmentPath,  params string[] Args)
        {
            string retVal = null;

            //reading the file
            string FileContents = ReadEmailFile(EmailTemplateFile);

            string emailBody = FileContents;

            //setting formatting the string
            retVal = string.Format(emailBody, Args);

            try
            {
                //check if we are in debug mode or not. to send email
                if (GlobalVars.DBConnection.ToUpper() == "TEST" || GlobalVars.DBConnection.ToUpper() == "QUALITY")
                {
                    SendEmailWithAttachment(SenderEmail, SenderName, GetTestRecipientsList(), CC, (!string.IsNullOrEmpty(Subject) ? " TESTING ONLY -IGNORE : " + Subject + RecepientEmail : _subject), retVal, attachmentPath);
                }
                else
                {
#if(!DEBUG)
                    SendEmailWithAttachment(SenderEmail, SenderName, RecepientEmail, CC, (!string.IsNullOrEmpty(Subject) ? Subject : _subject), retVal, attachmentPath);
#endif
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return retVal;


        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="EmailTemplateFile"></param>
        /// <param name="SenderEmail"></param>
        /// <param name="SenderName"></param>
        /// <param name="RecepientEmail"></param>
        /// <param name="CC"></param>
        /// <param name="Subject"></param>
        /// <param name="attachmentPath"></param>
        /// <param name="Args"></param>
        /// <returns></returns>
        public string SendNotificationEmailWithAttachment(string EmailTemplateFile, string SenderEmail, string SenderName, List<string> RecepientEmail, string CC, string Subject, string attachmentPath, params string[] Args)
        {
            string retVal = null;

            //reading the file
            string FileContents = ReadEmailFile(EmailTemplateFile);

            string emailBody = FileContents;

            //setting formatting the string
            retVal = string.Format(emailBody, Args);

            try
            {
                //check if we are in debug mode or not. to send email
                if (GlobalVars.DBConnection.ToUpper() == "TEST" || GlobalVars.DBConnection.ToUpper() == "QUALITY")
                {
                    foreach(string email in RecepientEmail)
                    {
                        Subject += email + ";";
                    }

                    RecepientEmail.Clear();
                    SendEmailWithAttachment(SenderEmail, SenderName, GetTestRecipientsList(), CC, (!string.IsNullOrEmpty(Subject) ? " TESTING ONLY -IGNORE : " + Subject : _subject), retVal, attachmentPath);
                }
                else
                {
#if (!DEBUG)
                    SendEmailWithAttachment(SenderEmail, SenderName, RecepientEmail, CC, (!string.IsNullOrEmpty(Subject) ? Subject : _subject), retVal, attachmentPath);                    
#endif
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return retVal;


        }
        

        /////////============================================================

        public string SendNotificationEmailWithAttachments(string EmailTemplateFile, string SenderEmail, string SenderName, string RecepientEmail, string CC, string Subject, List<string> attachments, params string[] Args)
        {
            string retVal = null;

            //reading the file
            string FileContents = ReadEmailFile(EmailTemplateFile);

            string emailBody = FileContents;

            //setting formatting the string
            retVal = string.Format(emailBody, Args);

            try
            {
                //check if we are in debug mode or not. to send email
                if (GlobalVars.DBConnection.ToUpper() == "TEST" || GlobalVars.DBConnection.ToUpper() == "QUALITY")
                {
                    SendEmailWithAttachments(SenderEmail, SenderName, GetTestRecipientsList(), CC, (!string.IsNullOrEmpty(Subject) ? " TESTING ONLY -IGNORE : " + Subject + RecepientEmail : _subject), retVal, attachments);
                }
                else
                {
#if(!DEBUG)
                    SendEmailWithAttachments(SenderEmail, SenderName, RecepientEmail, CC, (!string.IsNullOrEmpty(Subject) ? Subject : _subject), retVal, attachments);
#endif
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return retVal;


        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="EmailTemplateFile"></param>
        /// <param name="SenderEmail"></param>
        /// <param name="SenderName"></param>
        /// <param name="RecepientEmail"></param>
        /// <param name="CC"></param>
        /// <param name="Subject"></param>
        /// <param name="attachmentPath"></param>
        /// <param name="Args"></param>
        /// <returns></returns>
        public string SendNotificationEmailWithAttachments(string EmailTemplateFile, string SenderEmail, string SenderName, List<string> RecepientEmail, string CC, string Subject, List<string> attachments, params string[] Args)
        {
            string retVal = null;

            //reading the file
            string FileContents = ReadEmailFile(EmailTemplateFile);

            string emailBody = FileContents;

            //setting formatting the string
            retVal = string.Format(emailBody, Args);

            try
            {
                //check if we are in debug mode or not. to send email
                if (GlobalVars.DBConnection.ToUpper() == "TEST" || GlobalVars.DBConnection.ToUpper() == "QUALITY")
                {
                    foreach(string email in RecepientEmail)
                    {
                        Subject += email + ";";
                    }

                    RecepientEmail.Clear();
                    SendEmailWithAttachments(SenderEmail, SenderName, GetTestRecipientsList(), CC, (!string.IsNullOrEmpty(Subject) ? " TESTING ONLY -IGNORE : " + Subject : _subject), retVal, attachments);
                }
                else
                {
#if (!DEBUG)
                    SendEmailWithAttachments(SenderEmail, SenderName, RecepientEmail, CC, (!string.IsNullOrEmpty(Subject) ? Subject : _subject), retVal, attachments);                    
#endif
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return retVal;


        }




        /// <summary>
        /// 
        /// </summary>
        /// <param name="EmailTemplateFile"></param>
        /// <param name="SenderEmail"></param>
        /// <param name="SenderName"></param>
        /// <param name="RecepientEmail"></param>
        /// <param name="CC"></param>
        /// <param name="Subject"></param>
        /// <param name="Args"></param>
        /// <returns></returns>
        public string SendNotificationEmail(string EmailTemplateFile, string SenderEmail, string SenderName, List<string> RecepientEmail, string CC, string Subject, params string[] Args)
        {
            string retVal = null;

            //reading the file
            string FileContents = ReadEmailFile(EmailTemplateFile);

            string emailBody = FileContents;

            //setting formatting the string
            retVal = string.Format(emailBody, Args);

            try
            {
                //check if we are in debug mode or not. to send email
                if (GlobalVars.DBConnection.ToUpper() == "TEST" || GlobalVars.DBConnection.ToUpper() == "QUALITY")
                {
                    foreach (string email in RecepientEmail)
                    {
                        Subject += email + ";";
                    }
                    RecepientEmail.Clear();
                    SendEmail(SenderEmail, SenderName, GetTestRecipientsList(), CC, (!string.IsNullOrEmpty(Subject) ? Subject : _subject), retVal);
                }
                else
                {
#if (!DEBUG)
                        SendEmail(SenderEmail, SenderName, RecepientEmail, CC, (!string.IsNullOrEmpty(Subject) ? Subject : _subject), retVal);
#endif
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return retVal;


        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="importance"></param>
        public void SendNotificationEmailToAdmin(string subject, string body, MailPriority importance)
        {
            try
            {
                System.Configuration.ConfigurationManager.RefreshSection("appSettings");

                SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["SMTP Server"]);
                MailMessage msg = new MailMessage();

                string toList = ConfigurationManager.AppSettings["Admin Notification Recepient"];
                msg.From = new MailAddress(ConfigurationManager.AppSettings["Notification Sender"]); ;
                string[] recipients = toList.Split(',');
                foreach (string recipient in recipients)
                    msg.To.Add(new MailAddress(recipient.Trim()));

                DateTime now = DateTime.Now;

                string temp = now.ToString() + " - " + body;
                msg.Subject = subject;
                msg.Body = temp;
                msg.Priority = importance;
//#if(!DEBUG)
                client.Send(msg);
//#endif
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<string> GetTestRecipientsList()
        {
            var r = new List<string>();
            try
            {
                string emails = ConfigurationManager.AppSettings["Test Email Recipients"];
                foreach (string s in emails.Split(';', ','))
                {
                    if (!String.IsNullOrWhiteSpace(s))
                        r.Add(s);
                }
            }
            catch
            {
                r.Add("dhuang2@infineon.com");
            }
            return r;
        }
    }
}