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

public partial class Agent_SenderRegistrationGo : System.Web.UI.Page
{
    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"]);
            BindState();
        }
    }
    public void BindState()
    {
       string result = "";
        try
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.goprocessing.in/getServiceData.go");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = new JavaScriptSerializer().Serialize(new
                {
                    goid = "5201078519",
                    apikey = "5r4BH2HEsDaHcRw",//"CI4QN7ALHFL6SWM",
                    type = "states",
                    rtype = "json"
                });
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }
            if (result != "")
            {
                JavaScriptSerializer JsonSerializer = new JavaScriptSerializer();
                JObject jsonResponse = JObject.Parse(result);
                if (jsonResponse["status"].ToString() == "1")
                {
                    JArray objResponse = (JArray)jsonResponse["result"];
                    if (objResponse.Count > 0)
                    {
                        List<State> _objBank = objResponse.ToObject<List<State>>();
                        foreach (State p in _objBank.OrderBy(s => s.state_name))
                        {
                            //string listvalue = p.bank_id + "/" + p.bank_code + "/" + p.is_without_ifsc;
                            ListItem item = new ListItem(p.state_name, p.state_code);
                            ddlState.Items.Add(item);
                        }

                        ddlState.Items.Insert(0, new ListItem("Select State", "0"));
                    }
                }
                else if (jsonResponse["status"].ToString() == "2")
                {
                    
                    lblmsg.Text = "Remitter Not Found.";
                }
            }
        }
        catch(Exception ex)
        {
        }
    }
    protected void btnReg_Click(object sender, EventArgs e)
    {
        string Result = "";
        if (txtSenderNo.Text.Trim() != "" || txtSenderName.Text.Trim() != "" || txtAddress.Text.Trim() != "" || txtpincode.Text.Trim() != "")
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.goprocessing.in/dmt/moneyRegister.go");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = new JavaScriptSerializer().Serialize(new
                {
                    goid = "5201078519",
                    apikey = "5r4BH2HEsDaHcRw",//"CI4QN7ALHFL6SWM",
                    rtype = "json",
                    service_family = "25",
                    apimode = "live",
                    agent_id = "Venus001",
                    msisdn = Convert.ToString(Session["sendernumber"]),
                    user_name=txtSenderName.Text.Trim(),
                    user_address=txtAddress.Text.Trim(),
                    user_city=txtCity.Text.Trim(),
                    user_state_code=ddlState.SelectedValue,
                    user_pincode=txtpincode.Text.Trim()
                });
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                Result = streamReader.ReadToEnd();
            }
            if (Result != "")
            {
                JObject jsonResponse = JObject.Parse(Result);
                if (jsonResponse["status"].ToString() == "1")
                {
                    string message = "You are successfully registered with us.";
                    string url = "MoneyTransferGo.aspx";
                    string script = "window.onload = function(){ alert('";
                    script += message;
                    script += "');";
                    script += "window.location = '";
                    script += url;
                    script += "'; }";
                    ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);

                }
                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.";
        }
    }
}