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.Drawing;
using System.IO;
using System.Net;
using System.Net.Mail;

public partial class Index : System.Web.UI.Page
{
    CommonFunction objcommonfun = new CommonFunction();
    string dateSms = DateTime.Now.ToShortDateString();
    string date = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
    string usertype, RefrenceNo;
    string smsAnd = "Mobile Apps URL  http://venusrecharge.com/MobilePortal/VenusRecharge.apk User Id is your registered mobile no. and password same as web";
    int DBMonth;
    int count;
    //void Application_BeginRequest(object sender, EventArgs e)
    //{
    //    string fullOrigionalpath = Request.Url.ToString();

    //    if (fullOrigionalpath.Contains("http://localhost:38388/Whitelabel/Distributor/Index.aspx"))
    //    {
    //        Context.RewritePath("http://localhost:38388/Whitelabel/Products.aspx?Category=Books");
    //    }
    //    else if (fullOrigionalpath.Contains("http://localhost:38388/Whitelabel/Products/DVDs.aspx"))
    //    {
    //        Context.RewritePath("/Products.aspx?Category=DVDs");
    //    }
    //} 
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERTYPE"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            usertype = Session["USERTYPE"].ToString();
            lbl_current_date.Text = Convert.ToDateTime(System.DateTime.Now.ToShortDateString()).ToString("MMM-yyyy");
            if (!IsPostBack)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "askToConfirm();", true);
                userdetails();
                CurrentMargin();
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }
    public void userdetails()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",Session["LOGINID"].ToString())
            };

            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_USERDETAILS", get_user_details);
            if (dr.Read())
            {
                lblAgentID.Text = Convert.ToString(dr["USERNAME"]);
                lblAgency_firm_name.Text = Convert.ToString(dr["CORPORATENAME"]);
                lbl_FirstName.Text = Convert.ToString(dr["firstname"]);
                lbl_LastName.Text = Convert.ToString(dr["LASTNAME"]);
                lbl_registered_emailid.Text = Convert.ToString(dr["EMAILID"]);
                lbl_registered_mobileno.Text = Convert.ToString(dr["MOBILENO"]);
                lbl_balance.Text = Convert.ToString(dr["balance"]);
                //lbl_COMMISSION.Text = Convert.ToString(dr["COMMISSION"]);
            }
            dr.Close();
        }
        catch (Exception ex)
        {
            string st = ex.Message;
            throw (ex);
        }
    }
    public void CurrentMargin()
    {
        SqlParameter[] Margin = new SqlParameter[]
        {
            new SqlParameter("@Agentid",Session["LOGINID"].ToString())
        };
        SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "SP_GETCOMMISSIONOFMONTH", Margin);
        if (dr.Read())
        {
            lbl_COMMISSION.Text = Convert.ToString(dr["Margin"]);
            if (lbl_COMMISSION.Text=="")
            {
                lbl_COMMISSION.Text = "0.00";
            }
            else
            {
                lbl_COMMISSION.Text = Convert.ToString(dr["Margin"]);
            }  
        } 
        dr.Close();      
    }
    protected void btnFundRequestSelf_Click(object sender, EventArgs e)
    {
        Response.Redirect("FundRequestSelf.aspx");
    }
    
    private void SendSMS(string MobileNo, string sms)
    {
        string strUrl = "http://api.mVaayoo.com/mvaayooapi/MessageCompose?user=alliance@egenservices.com:8430082781&senderID=ACTVTE&receipientno=" + MobileNo + "&msgtxt=" + sms + "&state=4";
        //string strUrl = "http://sms.bulksms.co/instant1/web2sms.php?username=egen&password=egen@$ervice$09&to=" + MobileNo + "&sender=" + "RECHRG" + "&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();        
    }
    protected void AndroidBtn_Click(object sender, EventArgs e)
    {
        try
        {
            RefrenceNo = TID_GEN();
            string[] datefrom = dateSms.Split('/');
            string tomonth = Convert.ToString(datefrom.GetValue(0));
            int Mth = Convert.ToInt32(tomonth);

            SqlParameter[] GetMonthSMSCount = new SqlParameter[]
        {
            new SqlParameter ("@Loginid",Session["LOGINID"].ToString())
        };
            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_GetMonthSMSCount", GetMonthSMSCount);
            if (dr.Read())
            {
                DBMonth = Convert.ToInt32(dr["Month"]);
                count = Convert.ToInt32(dr["MobAppDnldURLcount"]);
            }
            dr.Close();
            if (Mth == DBMonth)
            {
                if (count < 6)
                {
                    objcommonfun.SendSMSByAdmin(lbl_registered_mobileno.Text, smsAnd, RefrenceNo, Session["LOGINID"].ToString());
                    count = count + 1;
                    SqlParameter[] updatecount = new SqlParameter[]
                {
                    new SqlParameter("@Loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@count",count)
                };
                    DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_updatecount", updatecount);
                    count = 6 - count;
                    lblerror.Text = "SMS sent to your registered mobile number. " + count + " SMS remaining.";
                    lblerror.ForeColor = Color.Blue;
                }
                else
                {
                    lblerror.Text = "All SMS has been used. Mobile apps URL is  http://venusrecharge.com/MobilePortal/VenusRecharge.apk";
                    lblerror.ForeColor = Color.Red;
                }
            }
            else
            {
                count = 2;
                SqlParameter[] UpdateGetMonthSMSCount = new SqlParameter[]
            {
                new SqlParameter ("@Loginid",Session["LOGINID"].ToString()),
                new SqlParameter ("@Month",Mth),
                new SqlParameter ("@Count",count)
            };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_UpdateGetMonthSMSCount", UpdateGetMonthSMSCount);
                objcommonfun.SendSMSByAdmin(lbl_registered_mobileno.Text, smsAnd, RefrenceNo, Session["LOGINID"].ToString());
                count = 6 - count;
                lblerror.Text = "SMS sent to your registered mobile number. " + count + " SMS remaining.";
                lblerror.ForeColor = Color.Blue;
            }
        }
        catch (Exception ex)
        { 
        
        }
    }

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