using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
using System.Net.Mail;
using System.Net.Sockets;
using System.Xml;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Collections.Specialized;

public partial class Distributor_smsservices : System.Web.UI.Page
{
    static string flagREFREV;
    float balance;
    string refrenceno;
    CommonFunction objcommonfun = new CommonFunction();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERTYPE"] == null)
            {
                Response.Redirect("../login.aspx");
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    protected void btn_confirm_Click(object sender, EventArgs e)
    {
        try
        {
            getuserdetail();
            if (ddl_Service.SelectedItem.Text.Equals("Fund Transfer & Reversal"))
            {
                if (flagREFREV.Equals("0"))
                {
                    userdetails();
                    if (Convert.ToDouble(lbl_ActivationCharge.Text) < balance)
                    {
                        activeservices();
                    }
                    else
                    {
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('You don’t have enough balance to do this transaction. Please take balance in your login.');"), true);
                    }
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('You have been alreday active this services into your accoount and can not activate again.');"), true);
                }
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please Select service Name');"), true);
            }
        }
        catch (Exception ex)
        {
           // Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some problem .Please try again.');"), true);
            throw (ex);
        }
    }
    public void getuserdetail()
    {
        try
        {
            SqlParameter[] getserviceactivation = new SqlParameter[] 
            {
                new SqlParameter("@LOGINID",Session["LOGINID"].ToString())
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_USERDETAILS_forsmserviceActivation", getserviceactivation);
            if (ds.Tables[0].Rows.Count > 0)
            {
                flagREFREV = ds.Tables[0].Rows[0]["SMServicesTRFREV"].ToString();
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some problem .Please try again.');"), true);
        }
    }

    public void activeservices()
    {
        try
        {
            refrenceno = TID_GEN();            
            SqlParameter[] serviceactivate = new SqlParameter[] 
            {
                new SqlParameter("@LOGINID",Session["LOGINID"].ToString()),
                new SqlParameter("@ServiceName","ACT"),
                new SqlParameter("@RequestAmount",lbl_ActivationCharge.Text),
                new SqlParameter("@Transactionid",refrenceno),
                new SqlParameter ("@ErrorMessage",SqlDbType.Int)
            };
            serviceactivate[4].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_smserviceActivation", serviceactivate);
            int output = Convert.ToInt32(serviceactivate[4].Value);
            if (output == 1)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some problem .Please try again.');"), true);
            }
            else
            {
                objcommonfun.SendSMSByAdmin(Session["MobileNo"].ToString(), "Use TRF for Fund Transfer and REV for Fund Reversal Formats are TRF/TRE<Space>User Code<Space>Amount<Space>MPIN Example: TRF/TRE AG10098 5000 1234", refrenceno, Session["LOGINID"].ToString());
                sendmail();
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Transaction Successfull');"), true);
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some problem .Please try again.');"), true);
        }
    }

    public void userdetails()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",Session["LOGINID"].ToString())
            };

            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_getavailablebalance", get_user_details);
            if (dr.Read())
            {
                balance = Convert.ToInt32(dr["balance"]);
            }
        }
        catch (Exception ex)
        {
            string st = ex.Message;
        }
    }
    public void sendmail()    
    {
        try
        {
            MailMessage message = new MailMessage();
            message.Body = mailmsg(); ;
            message.IsBodyHtml = true;
            message.To.Add(Session["EMAILID"].ToString());
            message.Bcc.Add(Session["helpdeskemailid"].ToString());
            message.Bcc.Add("support@egenservices.com");
            message.From = new System.Net.Mail.MailAddress("enquiry@egenservices.com");
            message.Subject = "Fund Transfer Through SMS - Activation Mail";
            message.Priority = MailPriority.High;
            SmtpClient SmtpMail = new SmtpClient();
            {
                SmtpMail.Host = "smtp.gmail.com";
                SmtpMail.Port = 587;
                SmtpMail.EnableSsl = true;
                SmtpMail.UseDefaultCredentials = true;
                SmtpMail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                SmtpMail.Credentials = new System.Net.NetworkCredential("mailenquiryindia@gmail.com", "@google.com#");
                SmtpMail.Send(message);
            }
            message = null;
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Send Email Failed.');"), true);
        }
    }

    private string mailmsg()
    {
        StringBuilder strHTML = new StringBuilder();
        strHTML.Append("<table width=100% style='font-family:Trebuchet MS'>");
        strHTML.Append("<tr><td width=100%  align=center ><b></b></td></tr>");
        strHTML.Append("<tr><td width=100%>Dear Partner,</td></tr>");
        strHTML.Append("<tr><td width=100%></td></tr>");
        strHTML.Append("<tr><td width=100%></td></tr>");
        strHTML.Append("<tr><td width=100%>You have been successfully activate Fund Transfer through SMS for your account. Now You can do fund transfer and reversal to/from their under hierarchy users through  </td></tr>");
        strHTML.Append("<tr><td width=100%>SMS from their registered Mobile Number. Please find SMS formats below:</td></tr>");
        strHTML.Append("<tr><td width=100%></td></tr>");
        strHTML.Append("<tr><td width=100%><b>For Fund Transfer:</b></td></tr>");        
        strHTML.Append("<tr><td width=100%>SMS Format: TRF User Code Amount MPIN</td></tr>");
        strHTML.Append("<tr><td width=100%>Example: TRF AG10098 5000 1234</td></tr>");
        strHTML.Append("<tr><td width=100%><b></b></td></tr>");
        strHTML.Append("<tr><td width=100%><b>For Fund Reversal:</b></td></tr>");        
        strHTML.Append("<tr><td width=100%>SMS Format: REV User Code Amount MPIN</td></tr>");
        strHTML.Append("<tr><td width=100%>Example: REV AG10098 5000 1234</td></tr>");
        strHTML.Append("<tr><td width=100%><b></b></td></tr>");
        strHTML.Append("<tr><td width=100%><b>Note:</b></td></tr>");
        strHTML.Append("<tr><td width=100%>1.	Please use registered mobile number with us for this service, else no action perform.</td></tr>");
        strHTML.Append("<tr><td width=100%>2.	Distributor will charge @ 0.25 paisa per transaction.</td></tr>");
        strHTML.Append("<tr><td width=100%>3.	Fund Transfer & Reversal can do between 1000 to 5000 value per transaction.</td></tr>");
        strHTML.Append("<tr><td width=100%><b></b></td></tr>");
        strHTML.Append("<tr><td width=100%><b>Please call our support desk for any assistance or clarification.</b></td></tr>");
        strHTML.Append("<tr><td width=100%><b></b></td></tr>");
        strHTML.Append("<tr><td width=100%></td></tr>");
        strHTML.Append("<tr><td width=100%>Regards,</td></tr>");
        strHTML.Append("<tr><td width=100%>Support Desk</td></tr>");
        strHTML.Append("<tr><td width=100%>+91-7407904263</td></tr>");
        strHTML.Append("<tr><td width=100%><b></b></td></tr>");
        strHTML.Append("</table>");
        return strHTML.ToString();
    }

    public static string TID_GEN()
    {
        string new_tid = System.Guid.NewGuid().ToString();
        new_tid = new_tid.Replace("-", string.Empty).ToLower();
        new_tid = new_tid.Substring(0, 16).Replace("a", "1");
        new_tid = new_tid.Substring(0, 16).Replace("b", "2");
        new_tid = new_tid.Substring(0, 16).Replace("c", "3");
        new_tid = new_tid.Substring(0, 16).Replace("d", "4");
        new_tid = new_tid.Substring(0, 16).Replace("e", "5");
        new_tid = new_tid.Substring(0, 16).Replace("f", "6");
        new_tid = new_tid.Substring(0, 16).Replace("g", "7");
        new_tid = new_tid.Substring(0, 16).Replace("h", "8");
        new_tid = new_tid.Substring(0, 16).Replace("i", "9");
        new_tid = new_tid.Substring(0, 16).Replace("j", "0");
        new_tid = new_tid.Substring(0, 16).Replace("k", "1");
        new_tid = new_tid.Substring(0, 16).Replace("l", "2");
        new_tid = new_tid.Substring(0, 16).Replace("m", "3");
        new_tid = new_tid.Substring(0, 16).Replace("n", "4");
        new_tid = new_tid.Substring(0, 16).Replace("o", "5");
        new_tid = new_tid.Substring(0, 16).Replace("p", "0");
        new_tid = new_tid.Substring(0, 16).Replace("q", "9");
        new_tid = new_tid.Substring(0, 16).Replace("r", "8");
        new_tid = new_tid.Substring(0, 16).Replace("s", "7");
        new_tid = new_tid.Substring(0, 16).Replace("t", "6");
        new_tid = new_tid.Substring(0, 16).Replace("u", "5");
        new_tid = new_tid.Substring(0, 16).Replace("v", "4");
        new_tid = new_tid.Substring(0, 16).Replace("w", "3");
        new_tid = new_tid.Substring(0, 16).Replace("x", "2");
        new_tid = new_tid.Substring(0, 16).Replace("y", "1");
        new_tid = new_tid.Substring(0, 16).Replace("z", "0");
        return new_tid;
    }
}
