using System;
using System.Net.Mail;
using System.IO;
using System.Linq;

namespace EmailMessage
{
    class Email
    {



        static void Main(string[] args)
        {

            try

            {
                // Initialize string and variables
                string Report;
                string path = @"\\belinda\Fei_UITests\Devtools\Test_Results\";
                string suffix = @"\Error_pictures";


                var foldertime = new DirectoryInfo(path).GetDirectories("*", SearchOption.TopDirectoryOnly).OrderByDescending(d => d.LastWriteTimeUtc).First();
                Report = File.ReadAllText(@"C:\_UITest\Result\TestResult.html");


                // Three types of Email messages
                string source_dir = @"C:\_UITest\Result\";
                string destination_dir = path + foldertime;

                // Create subdirectory structure in destination    
                foreach (string file_name in System.IO.Directory.GetFiles(source_dir, "*", System.IO.SearchOption.AllDirectories))
                {
                    System.IO.File.Copy(file_name, System.IO.Path.Combine(destination_dir, file_name.Substring(source_dir.Length + 1)));
                }

                if (string.IsNullOrEmpty(Report))
                {

                    //email part
                    MailMessage mail = new MailMessage();
                    SmtpClient SmtpServer = new SmtpClient("mail.edhouse.cz");

                    mail.From = new MailAddress("Automat@edhouse.cz");
                    mail.To.Add("kovacik@edhouse.cz; michalek@edhouse.cz");
                    mail.Subject = "! Devtools Automation Test Report Failed";

                    mail.IsBodyHtml = true;
                    string htmlBody;

                    htmlBody = "<b>No test was correctly started. Please check the test enviroment.</b>";

                    mail.Body = htmlBody;

                    SmtpServer.Port = 25;
                    SmtpServer.Send(mail);
                }
                else
                {

                    //email part
                    MailMessage mail = new MailMessage();
                    SmtpClient SmtpServer = new SmtpClient("mail.edhouse.cz");
                    mail.From = new MailAddress("Automat@edhouse.cz");
                    mail.To.Add("kovacik@edhouse.cz, michalek@edhouse.cz");
                    mail.Subject = "Devtools Automation Test Report";
                    mail.IsBodyHtml = true;
                    string htmlBody;
                    htmlBody = (string)Report;

                    //Add all files in test directory as attachment
                    /* DirectoryInfo di = new DirectoryInfo(path + foldertime + suffix);
                     foreach (FileInfo fi in di.GetFiles())
                      {


                          Attachment att = new Attachment(fi.FullName);
                          att.ContentDisposition.Inline = true;

                          mail.IsBodyHtml = true;
                          mail.Attachments.Add(att);


                      }*/


                    mail.Body = htmlBody;
                    SmtpServer.Port = 25;
                    SmtpServer.Send(mail);


                }

            }

            catch (Exception ex1)

            {
                Console.WriteLine(ex1.Message);

                //email part
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("mail.edhouse.cz");

                mail.From = new MailAddress("Automat@edhouse.cz");
                mail.To.Add("kovacik@edhouse.cz; michalek@edhouse.cz");
                mail.Subject = "! Devtools Automation Test Report Failed";

                mail.IsBodyHtml = true;

                string htmlBody;

                htmlBody = "<html><body><b>Unable to retrieve collected TestData. Please check the test enviroment</b><br><br>" + ex1 + "</body></html>";

                mail.Body = htmlBody;

                SmtpServer.Port = 25;
                SmtpServer.Send(mail);

            }
        }


    }
}
