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.Text.RegularExpressions;
using System.IO;
using System.Net;
using System.Text;
using System.Data.SqlClient;
using System.Net.Sockets;
using System.Xml;
using System.Threading;
using System.Collections.Specialized;

public partial class MobileSMS_CheckBalance : System.Web.UI.Page
{
    string stringquery, msg, MobileNo, AgMobileNo, refrenceno,servicestype;
    string ActualMPIN, agent_Id, BalanceAfterTransaction;
    CommonFunction objcommonfun = new CommonFunction();
    int count, date, courtdate;
    double balance;
    com.ezypayindia.ws.network RechargeEzpayObj = new com.ezypayindia.ws.network();
    WBMobile_Noverification _ObjWBMNverify = new WBMobile_Noverification();
    static string ret = string.Empty;
    static string Payment_ERROR = string.Empty;
    string ErrorMessage = string.Empty;
    string GoPaymentURL;
    string SessVal;
    string Session_RQ_MRecharge;
    DataSet ds = new DataSet();
    string CpBalance, InstantPayBal;
    protected void Page_Load(object sender, EventArgs e)
    {
        //stringquery = "VUS+BAL";
        stringquery = Request.QueryString["Message"].ToString();
        MobileNo = Request.QueryString["MobNo"].ToString();
        refrenceno = Request.QueryString["Refno"].ToString();
        servicestype = Request.QueryString["servicetype"].ToString();
        if (servicestype == "VENBAL")
        {            
        }
        else
        {
            GetMpin();

            BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
            balance = Convert.ToDouble(BalanceAfterTransaction);
            string courentdate = DateTime.Now.ToString("dd");
            courtdate = Convert.ToInt32(courentdate);

            getcheckbalancecount(agent_Id);
            if (courtdate == date)
            {
                if (count == 1)
                {
                    try
                    {
                        updateDatecheckbalancecount();
                        objcommonfun.SendSMS(MobileNo, "Current Balance is " + BalanceAfterTransaction + " Next SMS for Balance check will be charge Rs. 0.25 Paisa from login balance One time balance check free in a day only", refrenceno, MobileNo, stringquery);
                    }
                    catch (Exception ex)
                    {
                        CommonFunction.writeToLogFile(Convert.ToString(ex));
                    }
                }
                else
                {
                    try
                    {
                        if (balance > 0.25)
                        {
                            updatecheckbalancecount();//updatecount
                            deductamount();//entryintransactiontable  //amountdeduct
                            BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                            objcommonfun.SendSMS(MobileNo, "Current Balance is " + BalanceAfterTransaction + " Next SMS for Balance check will be charge Rs. 0.25 Paisa from login balance One time balance check free in a day only", refrenceno, MobileNo, stringquery);
                        }
                    }
                    catch (Exception ex)
                    {
                        //CommonFunction.writeToLogFile(Convert.ToString(ex));
                    }
                }
            }
            else
            {
                try
                {
                    updateDatecheckbalancecount(); //update date from courent date and +1 count
                    BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                    objcommonfun.SendSMS(MobileNo, "Current Balance is " + BalanceAfterTransaction + " Next SMS for Balance check will be charge Rs. 0.25 Paisa from login balance One time balance check free in a day only", refrenceno, MobileNo, stringquery);
                }
                catch (Exception ex)
                {
                    //CommonFunction.writeToLogFile(Convert.ToString(ex));
                }
            }
        }
    }
    public void getcheckbalancecount(string Agentid)
    {
        SqlParameter[] checkbalancecount = new SqlParameter[]
        {
            new SqlParameter ("@Loginid",Agentid)
        };
        SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_checkbalancecountcourentDate", checkbalancecount);
        if (dr.Read())
        {
            date = Convert.ToInt32(dr["CourentDate"]);
            count = Convert.ToInt32(dr["CountBalCheckInCourentDate"]);
        }
    }
    public void GetMpin()
    {
        try
        {            
            SqlParameter[] get_user_details = new SqlParameter[]
                {
                    new SqlParameter("@MobileNo",MobileNo)
                };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetMpin", get_user_details);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ActualMPIN = ds.Tables[0].Rows[0]["Mpin1"].ToString();
                agent_Id = ds.Tables[0].Rows[0]["LOGINID"].ToString();                
            }
        }
        catch (Exception ex)
        {
            CommonFunction.writeToLogFile(Convert.ToString(ex));
        }
    }
    public void updatecheckbalancecount()
    {
        try
        {
            SqlParameter[] updatecount = new SqlParameter[]
            {
                new SqlParameter("@Loginid",agent_Id),
                new SqlParameter ("@date",courtdate),
                new SqlParameter("@status","count"),
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updatecheckbalancecount", updatecount);
        }
        catch (Exception ex)
        {
            CommonFunction.writeToLogFile(Convert.ToString(ex));
        }
    }
    public void updateDatecheckbalancecount()
    {
        try
        {
            SqlParameter[] updatecount = new SqlParameter[]
            {
                new SqlParameter("@Loginid",agent_Id),
                new SqlParameter ("@date",courtdate),
                new SqlParameter("@status","Datecount"),
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updatecheckbalancecount", updatecount);
        }
        catch (Exception ex)
        {
            CommonFunction.writeToLogFile(Convert.ToString(ex));
        }
    }
    public void deductamount()
    {
        try
        {
            SqlParameter[] deductamount = new SqlParameter[]
            {
                new SqlParameter("@Loginid",agent_Id),
                new SqlParameter("@balance",BalanceAfterTransaction),
                new SqlParameter ("@refrenceno",refrenceno),
                new SqlParameter("@ServiceName","SMSCharge")
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_deductAmountForSMS", deductamount);
        }
        catch (Exception ex)
        {
            CommonFunction.writeToLogFile(Convert.ToString(ex));
        }
    }
}
