using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using System.Data.OracleClient;
using System.Runtime.InteropServices;
using System.Net.Mail;
using System.Collections;

public class Email
{
   	public Email()
	{
		//
		// TODO: Add constructor logic here
    }

    public static string GetMerchantEmail(string TerminalID)
    {
        string Email = "";
        DataTable dtRes = new DataTable();

        string DBConnection = Helper.dbConnection();

        OracleConnection oracon = new OracleConnection(DBConnection);

        OracleCommand oraCom = new OracleCommand();
        oraCom.CommandText = @" select ma.email_address from merchant_address ma,device_link_tbl dvl where ma.client_no=dvl.client_number
                    and dvl.term_id='" + TerminalID +"' and dvl.status='001' and ma.email_address is not null";
                    
        oraCom.Connection = oracon;
        OracleDataAdapter da = new OracleDataAdapter(oraCom);

        da.Fill(dtRes);

        if (dtRes.Rows.Count >= 1)
            Email = dtRes.Rows[0]["email_address"].ToString();

        return Email;
    }


    public static string GetEmailBy_UID(int UID)
    {
        string Email = "";
        DataTable dtRes = new DataTable();

        string DBConnection = Helper.dbConnection();

        OracleConnection oracon = new OracleConnection(DBConnection);

        OracleCommand oraCom = new OracleCommand();
        oraCom.CommandText = " select email from sec_user ss where ss.u_id  =" + UID + "";
        oraCom.Connection = oracon;
        OracleDataAdapter da = new OracleDataAdapter(oraCom);

        da.Fill(dtRes);

        if (dtRes.Rows.Count >= 1)
            Email = dtRes.Rows[0]["email"].ToString();

        return Email;
    }
    public static string Send_Email(string ToEmail, string Subject, string Body, string ccEmail, string AttachmentPath)
    {

        var EmailHost = System.Web.Configuration.WebConfigurationManager.AppSettings["EmailHost"] as string;
        var EmailHostPort = System.Web.Configuration.WebConfigurationManager.AppSettings["EmailHostPort"] as string;
        var Email_User_id = System.Web.Configuration.WebConfigurationManager.AppSettings["Email_User_id"] as string;
        
        //var Email_Pass =    System.Web.Configuration.WebConfigurationManager.AppSettings["Email_Pass"] as string;
        var Email_Pass = Helper.Decrypt_String(System.Web.Configuration.WebConfigurationManager.AppSettings["Email_Pass"]);

        try
        {
            SmtpClient client = new SmtpClient
            {
                Host = EmailHost,
                Port = Int32.Parse(EmailHostPort),
                //EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new System.Net.NetworkCredential(Email_User_id, Email_Pass),
                //Timeout = 10000,
            };

            MailMessage mail = new MailMessage(Email_User_id, ToEmail, Subject, Body);

            ExceptionUtility.LogException(0, "", "Send_Email", "Send_Email", "ToEmail:" + ToEmail + "Subject:" + Subject);

            if (!string.IsNullOrWhiteSpace(ccEmail))
            {
                ExceptionUtility.LogException(0, "", "Send_Email", "Send_Email", "ccEmail : " + ccEmail);
                mail.CC.Add(ccEmail);
            } if (!string.IsNullOrWhiteSpace(AttachmentPath))
            {
                ExceptionUtility.LogException(0, "", "Send_Email", "Send_Email", "AttachmentPath : " + AttachmentPath);
                Attachment attachItem = new Attachment(AttachmentPath);
                mail.Attachments.Add(attachItem);

            }

            client.Send(mail);

            ExceptionUtility.LogException(0, "", "Send_Email", "Send_Email", "ToEmail : " + ToEmail + " ccEmail : " + ccEmail + ",Body : " + Body);
            return ("Email Sent Sucessfully to ") + ToEmail;
        }
        catch (Exception e)
        {
            //Console.WriteLine("Could not end email\n\n" + e.ToString());
            ExceptionUtility.LogException(e, "", "Send_Email", "Send_Email", "");
            return "Failed To Send Email : " + ToEmail + " ccEmail : " + ccEmail + " Exception : Check log for more Details";
        }

    }



    
   
    

}
