using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
using System.Net.Mail;
using System.Net.Sockets;
using System.Xml;
using System.Xml.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;

public class WBMobile_Noverification
{
    WBMobile _ObjWBMobile = new WBMobile();
    SqlConnection con;
    SqlCommand cmd, cmd1;
    SqlDataReader dr;
    SqlDataAdapter da;
    DataSet ds;
    static string ret = string.Empty;
    string SessVal = string.Empty;    
	public WBMobile_Noverification()
	{
		// TODO: Add constructor logic here	
	}
    public string Gen_RS_WBMobile(string MobileNo, string Amount, string Comment, string SessVal, string account, string TERM_ID)   
    {
        string RS = string.Empty;
        string GetRQ = string.Empty;
        string Datenow = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
        //SessVal = DateTime.Now.ToString("dd") + "" + DateTime.Now.ToString("MM") + "" + DateTime.Now.ToString("yyyy") + "123" + DateTime.Now.ToString("HHmmssfff");
        GetRQ = Gen_RQ_WBMobile(MobileNo, Amount, Datenow, SessVal, Comment, account, TERM_ID);
        RS = _ObjWBMobile.GetResponse("$ervice$@egen051", GetRQ); 
        return RS;
    }
    
    private string Gen_RQ_WBMobile(string MobileNo, string Amount, string Datenow, string SessVal, string Comment, string account, string TERM_ID)
    {
        string RQ = string.Empty;
        if (MobileNo.ToString() != "" || Amount.ToString() != "")
        {
            RQ = "SD=104209" + Environment.NewLine;
            RQ = RQ + "AP=104261" + Environment.NewLine;
            RQ = RQ + "OP=104262" + Environment.NewLine;
            RQ = RQ + "SESSION=" + SessVal.ToString().Trim() + Environment.NewLine;
            RQ = RQ + "NUMBER=" + MobileNo.ToString().Trim() + Environment.NewLine;
            RQ = RQ + "ACCOUNT=" + Environment.NewLine;
            RQ = RQ + "ACCOUNT=" + account + Environment.NewLine;
            RQ = RQ + "AMOUNT=" + Amount.ToString().ToString().Trim() + ".00" + Environment.NewLine;
            RQ = RQ + "AMOUNT_ALL=" + Amount.ToString().ToString().Trim() + ".00" + Environment.NewLine;
            RQ = RQ + "COMMENT=" + Environment.NewLine;
            RQ = RQ + "TERM_ID=" + TERM_ID.ToString().Trim() + Environment.NewLine;
            RQ = RQ + "PAY_TOOL=0" + Environment.NewLine;
        }
        return RQ;
    }
   
    public string Gen_RS_WBDTHConnection(string SessVal, string Amount)
    {
        string RS = string.Empty;
        string GetRQ = string.Empty;
        string Datenow = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
        GetRQ = Gen_RQ_WBDTHConnection(SessVal, Amount);
        RS = _ObjWBMobile.GetResponse("$ervice$@egen051", GetRQ);
        return RS;
    }
    
    private string Gen_RQ_WBDTHConnection(string SessVal, string Amount)
    {
        string RQ = string.Empty;
        if (Amount.ToString() != "")
        {
            RQ = "SD=104209" + Environment.NewLine;
            RQ = RQ + "AP=104261" + Environment.NewLine;
            RQ = RQ + "OP=104262" + Environment.NewLine;
            RQ = RQ + "ACCEPT_KEYS=40" + Environment.NewLine;
            RQ = RQ + "SESSION=" + SessVal.ToString().Trim() + Environment.NewLine;
            RQ = RQ + "NUMBER=42" + Environment.NewLine;
            RQ = RQ + "AMOUNT=1390.00" + Environment.NewLine;
            RQ = RQ + "AMOUNT_ALL=1390.00" + Environment.NewLine;
        }
        return RQ;
    }

    public string Gen_RS_WEPostpaidBillPayment(string MobileNo, string Amount, string SessVal, string account)
    {
        string RS = string.Empty;
        string GetRQ = string.Empty;
        string Datenow = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
        GetRQ = Gen_RQ_WBPostpaidBillPayment(MobileNo, Amount, Datenow, SessVal, account);
        RS = _ObjWBMobile.GetResponse("$ervice$@egen051", GetRQ);
        return RS;
    }

    private string Gen_RQ_WBPostpaidBillPayment(string MobileNo, string Amount, string Datenow, string SessVal, string account)
    {
        string RQ = string.Empty;
        if (MobileNo.ToString() != "" || Amount.ToString() != "")
        {
            RQ = "SD=104209" + Environment.NewLine;
            RQ = RQ + "AP=104261" + Environment.NewLine;
            RQ = RQ + "OP=104262" + Environment.NewLine;
            RQ = RQ + "SESSION=" + SessVal.ToString().Trim() + Environment.NewLine;
            RQ = RQ + "NUMBER=" + MobileNo.ToString().Trim() + Environment.NewLine;
            RQ = RQ + "ACCOUNT=" + Environment.NewLine;
            //RQ = RQ + "ACCOUNT=" + account + Environment.NewLine;
            RQ = RQ + "AMOUNT=" + Amount.ToString().ToString().Trim() + ".00" + Environment.NewLine;
            RQ = RQ + "AMOUNT_ALL=" + Amount.ToString().ToString().Trim() + ".00" + Environment.NewLine;
            RQ = RQ + "COMMENT=" + Environment.NewLine;
            //RQ = RQ + "TERM_ID=" + TERM_ID.ToString().Trim() + Environment.NewLine;
            //RQ = RQ + "PAY_TOOL=0" + Environment.NewLine;
        }
        return RQ;
    }

    public string GetSessVal()
    {
        string Datenow = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
        SessVal = DateTime.Now.ToString("dd") + "" + DateTime.Now.ToString("MM") + "" + DateTime.Now.ToString("yyyy") + "123" + DateTime.Now.ToString("HHmmssfff");
        return SessVal;
    }

    public void FinalUpdate(string WLID, string AgentID, string SessVal, string REQUEST_AMOUNT, string ERROR, string ERRORMSG, string Tranlog, string ezpaytid, string OperatorID)
    {
        if (ERROR == "Fail")
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WLID",WLID),
                new SqlParameter("@AgentID",AgentID),
                new SqlParameter("@refrenceno",SessVal),
                new SqlParameter("@REQUEST_AMOUNT",REQUEST_AMOUNT),
                new SqlParameter("@errormessage",ERRORMSG),
                new SqlParameter("@TranLog",Tranlog),
                new SqlParameter("@ezpaytid",ezpaytid),
                new SqlParameter ("@OperatorTxnID",OperatorID)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updatestatus_fail", param);
        }
        else if (ERROR == "Pending")
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WLID",WLID),
                new SqlParameter("@AgentID",AgentID),
                new SqlParameter("@refrenceno",SessVal),
                new SqlParameter("@REQUEST_AMOUNT",REQUEST_AMOUNT),
                new SqlParameter("@errormessage",ERRORMSG),
                new SqlParameter("@TranLog",Tranlog),
                new SqlParameter("@ezpaytid",ezpaytid),
                new SqlParameter ("@OperatorTxnID",OperatorID)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updatestatus_Pending", param);
        }
        else
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WLID",WLID),
                new SqlParameter("@AgentID",AgentID),
                new SqlParameter("@refrenceno",SessVal),
                new SqlParameter("@REQUEST_AMOUNT",REQUEST_AMOUNT),
                new SqlParameter("@errormessage",ERRORMSG),
                new SqlParameter("@TranLog",Tranlog),
                new SqlParameter("@ezpaytid",ezpaytid),
                new SqlParameter ("@OperatorTxnID",OperatorID)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updatestatus_success", param);

        }
    }

    public void FinalUpdatetest(string WLID, string AgentID, string SessVal, string REQUEST_AMOUNT, string ERROR, string ERRORMSG, string Tranlog, string ezpaytid, string OperatorId)
    {
        if (ERROR == "Fail")
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WLID",WLID),
                new SqlParameter("@AgentID",AgentID),
                new SqlParameter("@refrenceno",SessVal),
                new SqlParameter("@REQUEST_AMOUNT",REQUEST_AMOUNT),
                new SqlParameter("@errormessage",ERRORMSG),
                new SqlParameter("@TranLog",Tranlog),
                new SqlParameter("@ezpaytid",ezpaytid),
                new SqlParameter ("@OperatorTxnID",OperatorId)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updatestatus_fail", param);
        }
        else
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WLID",WLID),
                new SqlParameter("@AgentID",AgentID),
                new SqlParameter("@refrenceno",SessVal),
                new SqlParameter("@REQUEST_AMOUNT",REQUEST_AMOUNT),
                new SqlParameter("@errormessage",ERRORMSG),
                new SqlParameter("@TranLog",Tranlog),
                new SqlParameter("@ezpaytid",ezpaytid),
                new SqlParameter("@OperatorTxnID",OperatorId)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updatestatus_success", param);
        }
    }

    public void FinalUpdateDTHConnection(string AgentID, string SessVal, string REQUEST_AMOUNT, string ERROR, string ERRORMSG, string Tranlog, string ezpaytid, string OperatorId, string DTHPinNo, string DTHSerialNo)
    {
        if (ERROR == "Fail")
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@AgentID",AgentID),
                new SqlParameter("@refrenceno",SessVal),
                new SqlParameter("@REQUEST_AMOUNT",REQUEST_AMOUNT),
                new SqlParameter("@errormessage",ERRORMSG),
                new SqlParameter("@TranLog",Tranlog),
                new SqlParameter("@ezpaytid",ezpaytid),
                new SqlParameter ("@OperatorTxnID",OperatorId),
                new SqlParameter("@DTHSerialNo",DTHSerialNo),
                new SqlParameter("@DTHPinNo",DTHPinNo)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updatestatus_faliourcase_DTHConnection", param);
        }
        if (ERROR == "Pending")
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@AgentID",AgentID),
                new SqlParameter("@refrenceno",SessVal),
                new SqlParameter("@REQUEST_AMOUNT",REQUEST_AMOUNT),
                new SqlParameter("@errormessage",ERRORMSG),
                new SqlParameter("@TranLog",Tranlog),
                new SqlParameter("@ezpaytid",ezpaytid),
                new SqlParameter ("@OperatorTxnID",OperatorId),
                new SqlParameter("@DTHSerialNo",DTHSerialNo),
                new SqlParameter("@DTHPinNo",DTHPinNo)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updatestatus_Pendingcase_DTHConnection", param);
        }
        else 
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@AgentID",AgentID),
                new SqlParameter("@refrenceno",SessVal),
                new SqlParameter("@REQUEST_AMOUNT",REQUEST_AMOUNT),
                new SqlParameter("@errormessage",ERRORMSG),
                new SqlParameter("@TranLog",Tranlog),
                new SqlParameter("@ezpaytid",ezpaytid),
                new SqlParameter("@OperatorTxnID",OperatorId),
                new SqlParameter("@DTHSerialNo",DTHSerialNo),
                new SqlParameter("@DTHPinNo",DTHPinNo) 
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updatestatus_successcase_DTHConnection", param);
        }
    }
    
    public int FN_ChkMsg(string RSMsg)
    {
        int idx = RSMsg.LastIndexOf("ERROR=");
        string errorval = RSMsg.Substring(idx + 6, 2);
        if (IsNumeric(errorval) == false)
        {
            errorval = RSMsg.Substring(idx + 6, 1);
        }
        return Convert.ToInt32(errorval);
    }

    public string getErrorMessage(int ErrorCode)
    {
        SqlParameter[] FetchErrorMessageParam = new SqlParameter[]
        {
            new SqlParameter("@ServicesProvider","Cyber Plat"),
            new SqlParameter("@ServicesProviderCode",ErrorCode),
        };
        DataSet _DS = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.Text, "SELECT [DisplayMessage] FROM [dbo].[MOBILE_DTH_ERROR_MESSAGE] WHERE [ServicesProvider]=@ServicesProvider AND [ServicesProviderCode]=@ServicesProviderCode", FetchErrorMessageParam);
        string ErrorMessage = _DS.Tables[0].Rows[0]["DisplayMessage"].ToString();
        return ErrorMessage;
    }

    public static bool IsNumeric(string strToCheck)
    {
        return Regex.IsMatch(strToCheck, "^\\d+(\\.\\d+)?$");
    }
  
    public string FN_TRANSID(string RSMsg, string SessVal)
    {
        int idx = RSMsg.LastIndexOf("TRANSID=");
        string errorval = RSMsg.Substring(idx + 8, 14);
        if (IsNumeric(errorval) == false)
        {
            errorval = RSMsg.Substring(idx + 8, 13);
        }
        try
        {
            Convert.ToDouble(errorval);
            return errorval.ToString();
        }
        catch
        {
            errorval = RSMsg.Substring(idx + 9, 14);
            if (IsNumeric(errorval) == false)
            {
                errorval = RSMsg.Substring(idx + 9, 13);
            }
            try
            {
                Convert.ToDouble(errorval);
                return errorval.ToString();
            }
            catch
            {
                return SessVal.ToString();
            }
        }
    }

    public int FN_ChkMsg_result(string RSMsg_result)
    {
        int idx = RSMsg_result.LastIndexOf("RESULT=");
        string resultval = RSMsg_result.Substring(idx + 7, 2);
        if (IsNumeric(resultval) == false)
        {
            resultval = RSMsg_result.Substring(idx + 7, 1);
        }
        return Convert.ToInt32(resultval);
    }





}
