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

public partial class MobilePortal_MobileAppLogin : System.Web.UI.Page
{
    string version, mobileNo, password, MobileAppsStatus, url;
    public static string txnid;
    protected void Page_Load(object sender, EventArgs e)
    {
        url = HttpContext.Current.Request.Url.AbsoluteUri;
        version = Request.QueryString["version"];
        mobileNo = Request.QueryString["mobileNo"];
        password = Request.QueryString["password"];
        //version = "1.0";
        //mobileNo = "9999999999";
        //password = "l0HoaNMz1z";
        
        txnid = TID_GEN();
        saveinputoutput();
        try
        {
            bool loginid = IsDigitsOnly(mobileNo);
            string headXML = "<response>";
            string footerXML = "</response>";
            if (loginid == true)
            {
                if (Convert.ToDouble(version) >= 1.0)
                {
                    SqlParameter[] check_login_details = new SqlParameter[]
                    {
                        new SqlParameter("@User_details",mobileNo),
                        new SqlParameter("@Password",password),
                        new SqlParameter("@output",SqlDbType.Int)
                    };
                    check_login_details[2].Direction = ParameterDirection.Output;
                    DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_login", check_login_details);
                    string Initial = Convert.ToString(ds.Tables[0].Rows[0]["USERNAME"]);
                    int loginvariable = Convert.ToInt32(check_login_details[2].Value);
                    string TwoChar = Initial.Substring(0, 2);
                    if (TwoChar.Equals("AG") || TwoChar.Equals("ag") || TwoChar.Equals("Ag") || TwoChar.Equals("DS") || TwoChar.Equals("ds") || TwoChar.Equals("Ds"))
                    {
                        if (ds.Tables[0].Rows.Count != 0)
                        {
                            string result = Convert.ToString(ds.Tables[0].Rows[0]["Result"]);
                            if (result.Equals("1"))
                            {
                                WBMobile_Noverification objMob = new WBMobile_Noverification();
                                Session["txn"] = TID_GEN();
                                InsertTxnKey();
                                headXML += "<response-code>0</response-code><response-message>Activate</response-message><agent-initial>" + TwoChar + "</agent-initial><agent-dist-id>40</agent-dist-id><agent-id>" + Convert.ToString(ds.Tables[0].Rows[0]["LOGINID"]) + "</agent-id><txn_key>" + Convert.ToString(Session["txn"]) + "</txn_key><balance>" + Convert.ToString(ds.Tables[0].Rows[0]["Balance"]) + "</balance><mobile-number>" + mobileNo + "</mobile-number>" + footerXML;
                                WriteXML(headXML);
                            }
                            else if (result.Equals("0"))
                            {
                                headXML += "<response-code>1</response-code><response-message>Your account is not active</response-message>" + footerXML;
                                WriteXML(headXML);
                            }
                            else if (result.Equals("2"))
                            {
                                headXML += "<response-code>1</response-code><response-message>Invalid UserName/Password</response-message>" + footerXML;
                                WriteXML(headXML);
                            }
                            else if (result.Equals("3"))
                            {
                                headXML += "<response-code>1</response-code><response-message>Invalid UserName/Password</response-message>" + footerXML;
                                WriteXML(headXML);
                            }
                        }
                        else
                        {
                            headXML += "<response-code>1</response-code><response-message>Please Login With Correct Details Details</response-message>" + footerXML;
                            WriteXML(headXML);
                        }
                    }
                    else
                    {
                        headXML += "<response-code>1</response-code><response-message>Unauthorised user</response-message>" + footerXML;
                        WriteXML(headXML);
                    }
                }
                else
                {
                    headXML += "<response-code>1</response-code><response-message>New version is available kindly download it</response-message>" + footerXML;
                    WriteXML(headXML);
                }
            }
            else
            {
                headXML += "<response-code>1</response-code><response-message>Invalid Mobile No. or Password</response-message>" + footerXML;
                WriteXML(headXML);
            }
        }
        catch (Exception ex)
        { }
    }

    private void WriteXML(string str)
    {
        updateresponce(str);
        Response.ClearHeaders();
        Response.AddHeader("content-type", "text/xml");
        Response.Write("<?xml version=\"1.0\" ?>" + str);
        Response.End();
    }

    private void InsertTxnKey()
    {
        try
        {
            SqlParameter[] inserttxnkey = new SqlParameter[] 
            {
                new SqlParameter("@txnkey",Session["txn"]),
                new SqlParameter("@userid",mobileNo)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UspInsertTransactionKey", inserttxnkey);
        }
        catch (Exception ex)
        { }
    }

    bool IsDigitsOnly(string str)
    {
        foreach (char c in str)
        {
            if (c < '0' || c > '9')
                return false;
        }
        return true;
    }

    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, 14).Replace("a", "1");
        new_tid = new_tid.Substring(0, 14).Replace("b", "2");
        new_tid = new_tid.Substring(0, 14).Replace("c", "3");
        new_tid = new_tid.Substring(0, 14).Replace("d", "4");
        new_tid = new_tid.Substring(0, 14).Replace("e", "5");
        new_tid = new_tid.Substring(0, 14).Replace("f", "6");
        new_tid = new_tid.Substring(0, 14).Replace("g", "7");
        new_tid = new_tid.Substring(0, 14).Replace("h", "8");
        new_tid = new_tid.Substring(0, 14).Replace("i", "9");
        new_tid = new_tid.Substring(0, 14).Replace("j", "0");
        new_tid = new_tid.Substring(0, 14).Replace("k", "1");
        new_tid = new_tid.Substring(0, 14).Replace("l", "2");
        new_tid = new_tid.Substring(0, 14).Replace("m", "3");
        new_tid = new_tid.Substring(0, 14).Replace("n", "4");
        new_tid = new_tid.Substring(0, 14).Replace("o", "5");
        new_tid = new_tid.Substring(0, 14).Replace("p", "0");
        new_tid = new_tid.Substring(0, 14).Replace("q", "9");
        new_tid = new_tid.Substring(0, 14).Replace("r", "8");
        new_tid = new_tid.Substring(0, 14).Replace("s", "7");
        new_tid = new_tid.Substring(0, 14).Replace("t", "6");
        new_tid = new_tid.Substring(0, 14).Replace("u", "5");
        new_tid = new_tid.Substring(0, 14).Replace("v", "4");
        new_tid = new_tid.Substring(0, 14).Replace("w", "3");
        new_tid = new_tid.Substring(0, 14).Replace("x", "2");
        new_tid = new_tid.Substring(0, 14).Replace("y", "1");
        new_tid = new_tid.Substring(0, 14).Replace("z", "0");
        return new_tid;
    }

    public void GetMobileAppsStatus()
    {
        try
        {
            SqlParameter[] getserviceactivation = new SqlParameter[] 
            {
                new SqlParameter("@LOGINID",mobileNo)
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_USERDETAILS_forsmserviceActivation", getserviceactivation);
            if (ds.Tables[0].Rows.Count > 0)
            {
                MobileAppsStatus = ds.Tables[0].Rows[0]["AndroidApps"].ToString();
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some problem .Please try again.');"), true);
        }
    }

    public void saveinputoutput()
    {
        SqlParameter[] inputrequest = new SqlParameter[]
        {
            new SqlParameter("@mobileno",mobileNo),
            new SqlParameter("@url",url),
            new SqlParameter("@txnid",txnid)
        };
        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_insert_input_request_mobileApps", inputrequest);
    }

    public void updateresponce(string responce)
    {
        SqlParameter[] inputrequest = new SqlParameter[]
        {
            new SqlParameter("@txnid",txnid),
            new SqlParameter("@Responce",responce)
        };
        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_update_responce_mobileApps", inputrequest);
    }

    //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, 14).Replace("a", "1");
    //    new_tid = new_tid.Substring(0, 14).Replace("b", "2");
    //    new_tid = new_tid.Substring(0, 14).Replace("c", "3");
    //    new_tid = new_tid.Substring(0, 14).Replace("d", "4");
    //    new_tid = new_tid.Substring(0, 14).Replace("e", "5");
    //    new_tid = new_tid.Substring(0, 14).Replace("f", "6");
    //    new_tid = new_tid.Substring(0, 14).Replace("g", "7");
    //    new_tid = new_tid.Substring(0, 14).Replace("h", "8");
    //    new_tid = new_tid.Substring(0, 14).Replace("i", "9");
    //    new_tid = new_tid.Substring(0, 14).Replace("j", "0");
    //    new_tid = new_tid.Substring(0, 14).Replace("k", "1");
    //    new_tid = new_tid.Substring(0, 14).Replace("l", "2");
    //    new_tid = new_tid.Substring(0, 14).Replace("m", "3");
    //    new_tid = new_tid.Substring(0, 14).Replace("n", "4");
    //    new_tid = new_tid.Substring(0, 14).Replace("o", "5");
    //    new_tid = new_tid.Substring(0, 14).Replace("p", "0");
    //    new_tid = new_tid.Substring(0, 14).Replace("q", "9");
    //    new_tid = new_tid.Substring(0, 14).Replace("r", "8");
    //    new_tid = new_tid.Substring(0, 14).Replace("s", "7");
    //    new_tid = new_tid.Substring(0, 14).Replace("t", "6");
    //    new_tid = new_tid.Substring(0, 14).Replace("u", "5");
    //    new_tid = new_tid.Substring(0, 14).Replace("v", "4");
    //    new_tid = new_tid.Substring(0, 14).Replace("w", "3");
    //    new_tid = new_tid.Substring(0, 14).Replace("x", "2");
    //    new_tid = new_tid.Substring(0, 14).Replace("y", "1");
    //    new_tid = new_tid.Substring(0, 14).Replace("z", "0");
    //    return new_tid;
    //}
}