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

public partial class AepsService_service : System.Web.UI.Page
{
    AEPS objAEPS = new AEPS();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string ReferalUrl ="";// Request.UrlReferrer.ToString();
            //IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); // `Dns.Resolve()` method is deprecated.
            //IPAddress ipAddress = ipHostInfo.AddressList[0];
            string IPAddress = "";//ipAddress.ToString();
            string userid = Request.QueryString["userid"].ToString();
            string WLID = Request.QueryString["wid"].ToString();
            if (userid.ToUpper() == "AG80297")
            {
                userid = "100080297";
            }
            else if (userid == "")
            {
                string message = "Opps!! Some thing problem in Authentication.";
                string url = ReferalUrl;
                string script = "window.onload = function(){ alert('";
                script += message;
                script += "');";
                script += "window.location = '";
                script += url;
                script += "'; }";
                ClientScript.RegisterClientScriptBlock(this.GetType(), "Redirect", script, true);
            }
            if (CheckAuth(ReferalUrl, IPAddress, WLID, userid))
            {
               
                string Response1 = objAEPS.way_in(Convert.ToString(userid));
                string[] response = Response1.Split('-');
                if (Response1.Contains("SU"))
                {
                 Response.Redirect(Convert.ToString(response[1]),false);
                    //Response.Redirect("index.aspx",);
                     // Response.Redirect(Convert.ToString(response[1]));
                      //Response.Write("<script>window.open ('" + Convert.ToString(response[1]) + "','_blank');</script>");
                }
                else
                {
                    string message = response[1];
                    string url = ReferalUrl;
                    string script = "window.onload = function(){ alert('";
                    script += message;
                    script += "');";
                    script += "window.location = '";
                    script += url;
                    script += "'; }";
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "Redirect", script, true);
                }
            }
            else
            {
                string message = "Opps!! Some thing problem in Authentication.";
                string url = ReferalUrl;
                string script = "window.onload = function(){ alert('";
                script += message;
                script += "');";
                script += "window.location = '";
                script += url;
                script += "'; }";
                ClientScript.RegisterClientScriptBlock(this.GetType(), "Redirect", script, true);
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("www.venusrecharge.in?"+ex.Message);
            
        }


    }

    public bool CheckAuth(string RequestUrl,string RequestIP,string WLID,string agentID)
    {
        bool Result = false;
        SqlParameter[] param = new SqlParameter[]
        {
        new SqlParameter("@ReqURL",RequestUrl),
        new SqlParameter("@ReqIP",RequestIP),
        new SqlParameter("@WLID",WLID),
        new SqlParameter("@AgentId",agentID),
        new SqlParameter("@Result",SqlDbType.Int)
        };
        param[4].Direction = ParameterDirection.Output;
        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_CheckWLAepsServiceAuth", param);
        int result = Convert.ToInt32(param[4].Value);
        if (result > 0)
            Result = true;
        return Result;
    }
}