using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Agent_SenderRegistrationNew : System.Web.UI.Page
{
    public static string Token = "9f180b1b2a49d42aed015564fca83ef8";
    AirEpointSupport _objTransaction = new AirEpointSupport();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblmsg.Text = "Sender not found. Retry with different sender id Or Register Sender";
            txtSenderNo.Text = Convert.ToString(Session["sendernumber"]);
        }
    }
    public void InsertLog(string RefId, string Response, string ServiceName)
    {
        try
        {
            SqlParameter[] warlock = new SqlParameter[]
            {
                new SqlParameter("@RefId",RefId),
                new SqlParameter("@LogData",Response),
                new SqlParameter("@Service",ServiceName)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "InsertLog", warlock);
        }
        catch (Exception ex)
        {
        }
    }
    protected void btnReg_Click(object sender, EventArgs e)
    {
        string Result = "";
        if (txtSenderNo.Text.Trim() != "" || txtSenderFName.Text.Trim() != "" || txtSenderLName.Text.Trim() != "" || txtAddress.Text.Trim() != "" || txtpincode.Text.Trim() != "")
        { 
            string RefId=_objTransaction.Generate_TID();
            Result = CreateWallet(RefId, txtSenderNo.Text.Trim(), txtSenderFName.Text.Trim(),  txtSenderLName.Text.Trim(), txtpincode.Text, txtAddress.Text, txtCity.Text);
            InsertLog(RefId, Result,"CreateWallet");
            if (Result != "")
            {
                JObject joResponse = JObject.Parse(Result);
                JObject Response_JObj = (JObject)joResponse["response"];
                JValue ResponseCode = (JValue)Response_JObj["status_code"];
                JValue Description = (JValue)Response_JObj["desc"];
                JValue RefID = (JValue)Response_JObj["reqid"];
                JValue RequestNo = (JValue)Response_JObj["RequestNo"];
                JValue MobileNo = (JValue)Response_JObj["MobileNo"];
                JValue Name = (JValue)Response_JObj["Name"];
                if (Convert.ToString(ResponseCode) == "RCS")
                {
                    ViewState["RequestNo"] = Convert.ToString(RequestNo);
                    mp1.Show();
                }
                else
                {
                    lblmsg.Text = "Sender Registration process failed. Please try after some time.";
                }

            }
            else
            {
                lblmsg.Text = "Sender Registration process failed. Please try after some time.";
            }
        }
        else
        {
            lblmsg.Text = "Please enter all mandatory fields.";
        }
    }

    public string CreateWallet(string reqid, string mobile,string FName,string LName,string pincode,string address,string city)
    {
        
        //string URl = "https://www.apibox.xyz/api/ManageUser/Wallet?token=" + Token + "&skey=DMI&reqid=" + reqid + "&p1=add&p2=" + mobile + "&p3=" + name + "&format=json";
        string URl = "https://www.apibox.xyz/api/ManageUser/Wallet?token=" + Token + "&skey=DMP&reqid=" + reqid + "&p1=add&p2=" + mobile + "&p3=" + FName + "&p4=" + LName + "&p5=" + pincode + "&p6=" + address + "&p7=" + city + "&format=json";
        string Response = GetResponseUsingGetMethod(URl);
        return Response;
    }
    public string GetResponseUsingGetMethod(string URL)
    {
        try
        {
            // objBal.vendorinputoutputResponce(apiId, reqid, ServiceType, "Request", URL, "APIBOX");
            WebRequest request = HttpWebRequest.Create(URL);
            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();
            //objBal.vendorinputoutputResponce(apiId, reqid, ServiceType, "Response", dataString, "APIBOX");
            return dataString;
        }
        catch
        {
            return "";
        }
    }
    protected void btnAddConfirm_Click(object sender, EventArgs e)
    {
        if (txtOTP.Text.Trim() == "")
        {
            lblmsg.Text = "Please enter OTP for varification of Sender.";
            return;
        }
        else
        {
            string RefId = _objTransaction.Generate_TID();
            string url = "https://www.apibox.xyz/api/Action/otp?token=" + Token + "&skey=DMP&reqid=" + RefId + "&p1=verify&p2=" + Convert.ToString(ViewState["RequestNo"]) + "&p3=" + txtOTP.Text.Trim() + "&p4=create&p5=" + txtSenderNo.Text.Trim() + "&format=json";
            string Response = GetResponseUsingGetMethod(url);
            InsertLog(RefId, Response, "ConfirmWallet");
            JObject joResponse = JObject.Parse(Response);
            JObject Response_JObj = (JObject)joResponse["response"];
            JValue ResponseCode = (JValue)Response_JObj["status_code"];
            JValue Description = (JValue)Response_JObj["desc"];
            JValue RefID = (JValue)Response_JObj["reqid"];
            if (Convert.ToString(ResponseCode) == "RCS")
            {
                string message = "You are successfully registered with us.";
                string urlnew = "MoneyTransferNew.aspx";
                string script = "window.onload = function(){ alert('";
                script += message;
                script += "');";
                script += "window.location = '";
                script += urlnew;
                script += "'; }";
                ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
                
            }
            else
            {
                lblmsg.Text = Convert.ToString(Description);
            }
        }
    }
    protected void btnResend_Click(object sender, EventArgs e)
    {
            string url = "https://www.apibox.xyz/api/Action/otp?token=" + Token + "&skey=DMI&reqid=" + _objTransaction.Generate_TID() + "&p1=resend&p2=" + Convert.ToString(ViewState["RequestNo"]) + "&p3=create&format=json";
            string Response = GetResponseUsingGetMethod(url);
            JObject joResponse = JObject.Parse(Response);
            JObject Response_JObj = (JObject)joResponse["response"];
            JValue ResponseCode = (JValue)Response_JObj["status_code"];
            JValue Description = (JValue)Response_JObj["desc"];
            JValue RefID = (JValue)Response_JObj["reqid"];
            if (Convert.ToString(ResponseCode) == "RCS")
            {
                mp1.Show();
            }
            else
            {
                lblmsg.Text = Convert.ToString(Description);
            }
    }
}