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.Text.RegularExpressions;
using System.IO;
using System.Net;
using System.Text;

public partial class MobileSMS_SetMpin : System.Web.UI.Page
{
    string stringquery, msg, MobileNo, AgMobileNo, RefrenceNo;
    CommonFunction objcommonfun = new CommonFunction();
    protected void Page_Load(object sender, EventArgs e)
    {
        //stringquery = "VUS+MPIN+5544";
        stringquery = Request.QueryString["Message"].ToString();
        MobileNo = Request.QueryString["MobNo"].ToString();
        RefrenceNo = Request.QueryString["Refno"].ToString();
        //string[] stringquery11 = new string[10];
        //stringquery11 = stringquery.Split('/');

        msg = stringquery;
        msg = Regex.Replace(msg, @"\s+", " ");
        string[] sms = msg.Split(' ');
        ViewState["Mpin"] = Convert.ToString(sms.GetValue(2));
        AgMobileNo = MobileNo.Substring(MobileNo.Length - Math.Min(10, MobileNo.Length));
        string strNAme = string.Empty;
        strNAme = ViewState["Mpin"].ToString();
        if (Regex.IsMatch(strNAme, "[A-Z|a-z]"))
        {
            SendSMS(AgMobileNo, "Please set 4 digits numeric MPIN only. MPIN SMS format: MPIN XXXX and send to XXXX ");
        }
        else if (ViewState["Mpin"].ToString().Length.Equals(4))
        {
            try
            {
                SqlParameter[] paramUpdateMpin = new SqlParameter[]
                {
                    new SqlParameter("@MobileNo",AgMobileNo),
                    new SqlParameter("@Mpin",Convert.ToInt32(ViewState["Mpin"].ToString()))
                };
                SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_UpdateMpin", paramUpdateMpin);
                //SendSMS(AgMobileNo, "Dear Partner, Your MPIN set successfully. Pls %E2%80%99 share it to anyone. Delete MPIN containing SMS for security of your account");
                objcommonfun.SendSMS(AgMobileNo, "Dear Partner, Your MPIN set successfully. Pls don’t share it to anyone. Delete MPIN containing SMS for security of your account", RefrenceNo,MobileNo,"");
            }
            catch (Exception ex)
            {
            }
        }
    }
    private void SendSMS(string MobileNo, string sms)
    {
        string CourentDate = DateTime.Now.ToShortDateString();
        string strUrl = "http://sms.igenservices.in/api/web2sms.php?workingkey=A0c8f3938f877b4a46175dfa15e04b064&sender=RECHRG&to=" + MobileNo + "&message=" + sms + "";
        WebRequest request = HttpWebRequest.Create(strUrl);
        request.Method = "GET";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream s = (Stream)response.GetResponseStream();
        StreamReader readStream = new StreamReader(s);
        string dataString = readStream.ReadToEnd();
        response.Close();
        s.Close();
        readStream.Close();
        try
        {
            SqlParameter[] url = new SqlParameter[]
            {
                new SqlParameter("@RefNumber",RefrenceNo),
                new SqlParameter("@SentMessage",sms),
                new SqlParameter("@SMSResponce",dataString),
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_Responcegeturl", url);
        }
        catch (Exception ex)
        {
            CommonFunction.writeToLogFile(Convert.ToString(ex));
        }
    }
}