using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HAPI;
using System.Net;
using System.Xml;
using System.IO;
using System.Data;


/// <summary>
/// Summary description for Hermes_Rech
/// </summary>
public class Hermes_Rech_BAL
{
    public static string UserID = "Venus";
    public static string Pass = "apiVenus";
    //public static string Pass = "Venus123";
    public Hermes_Rech_BAL()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public string mobile_Rech(string Loginid, string usertrackid, string meroid, string OperatorCode, string Mobile, double Amount, string voucher, string servicename)
    {
        string stresponse = string.Empty;
        string strUrl = Loginid + "&" + Amount.ToString() + "&" + Mobile + "&" + OperatorCode + "&" + meroid + "&" + usertrackid + "&" + voucher + "&" + servicename;
        CommonFunction.vendorinputoutputResponce(Loginid, meroid, strUrl, "", servicename);
        HAPI.Authentication obj_Auth = new HAPI.Authentication();
        HAPI.HermesMobile obj_HMob = new HAPI.HermesMobile();
        HAPI.RechargeInput obj_RechInput = new RechargeInput();
        obj_RechInput.OperatorCode = OperatorCode;
        obj_RechInput.MobileNumber = Mobile;
        obj_RechInput.Amount = Amount;

        obj_Auth.LoginId = UserID;
        obj_Auth.Password = Pass;
        HAPI.RechargeDoneRequest obj_RechReq = new RechargeDoneRequest();
        HAPI.RechargeDoneResponse obj_RechRes = new RechargeDoneResponse();
        obj_RechReq.Authentication = obj_Auth;
        obj_RechReq.UserTrackId = usertrackid;
        obj_RechReq.RechargeInput = obj_RechInput;
        obj_RechRes = obj_HMob.GetRechargeDone(obj_RechReq);
        if (obj_RechRes.ResponseStatus == 1)
        {
            stresponse = HermesRechargeStatus(Loginid, meroid, usertrackid, servicename);
        }
        else
        {
            stresponse = Convert.ToString(obj_RechRes.ResponseStatus) + "&" + obj_RechRes.FailureRemarks;
        }
        CommonFunction.vendorinputoutputResponce(Loginid, meroid, "", stresponse, "");
        return stresponse;
    }

    public string HermesRechargeStatus(string Loginid, string meroid, string usertrackid, string servicename)
    {
        string RechargeStatus = string.Empty;
        string strUrl = "GetTransactionStatus" + "&" + usertrackid;
        CommonFunction.vendorinputoutputResponce(Loginid, meroid, strUrl, "", servicename);
        HAPI.Authentication obj_Auth = new HAPI.Authentication();
        HAPI.HermesMobile obj_HMob = new HAPI.HermesMobile();
        obj_Auth.LoginId = UserID;
        obj_Auth.Password = Pass;
        HAPI.TransactionStatusRequest obj_TranStatusReq = new TransactionStatusRequest();
        HAPI.TransactionStatusResponse obj_TransStatusRes = new TransactionStatusResponse();
        obj_TranStatusReq.Authentication = obj_Auth;
        obj_TranStatusReq.UserTrackId = usertrackid;
        obj_TransStatusRes = obj_HMob.GetTransactionStatus(obj_TranStatusReq);
        if (obj_TransStatusRes.ResponseStatus == 1)
        {
            if (obj_TransStatusRes.TransactionStatusOutput.TransactionStatus == 1)
            {
                RechargeStatus = Convert.ToString(obj_TransStatusRes.ResponseStatus) + "&" + obj_TransStatusRes.TransactionStatusOutput.TransactionDetails.MobileNumber + "&" + obj_TransStatusRes.TransactionStatusOutput.Remarks + "&" + obj_TransStatusRes.TransactionStatusOutput.TransactionDetails.ReferenceNumber + "&" + obj_TransStatusRes.UserTrackId + "&" + Convert.ToString(obj_TransStatusRes.TransactionStatusOutput.TransactionDetails.Amount) + "&" + Convert.ToString(obj_TransStatusRes.TransactionStatusOutput.TransactionStatus) + "&" + obj_TransStatusRes.TransactionStatusOutput.TransactionDetails.OperatorTransactionId;
            }
            if (obj_TransStatusRes.TransactionStatusOutput.TransactionStatus == 3)
            {
                RechargeStatus = Convert.ToString(obj_TransStatusRes.ResponseStatus) + "&" + "" + "&" + obj_TransStatusRes.TransactionStatusOutput.Remarks + "&" + "" + "&" + obj_TransStatusRes.UserTrackId + "&" + Convert.ToString("") + "&" + Convert.ToString(obj_TransStatusRes.TransactionStatusOutput.TransactionStatus);
            }
            if (obj_TransStatusRes.TransactionStatusOutput.TransactionStatus == 2)
            {
                RechargeStatus = Convert.ToString(obj_TransStatusRes.ResponseStatus) + "&" + obj_TransStatusRes.TransactionStatusOutput.TransactionDetails.MobileNumber + "&" + obj_TransStatusRes.TransactionStatusOutput.Remarks + "&" + obj_TransStatusRes.TransactionStatusOutput.TransactionDetails.ReferenceNumber + "&" + obj_TransStatusRes.UserTrackId + "&" + Convert.ToString(obj_TransStatusRes.TransactionStatusOutput.TransactionDetails.Amount) + "&" + Convert.ToString(obj_TransStatusRes.TransactionStatusOutput.TransactionStatus);
            }
        }
        else
        {
            RechargeStatus = Convert.ToString(obj_TransStatusRes.ResponseStatus) + "&" + obj_TransStatusRes.UserTrackId + "&" + obj_TransStatusRes.FailureRemarks;
        }
        return RechargeStatus;
    }

    //public string bill_payment(string Loginid, string usertrackid, string meroid, string OperatorCode, string Mobile, double Amount, string voucher, string servicename)
    //{
    //    string stresponse = string.Empty;
    //    string strUrl = Loginid + "&" + Amount.ToString() + "&" + Mobile + "&" + OperatorCode + "&" + meroid + "&" + usertrackid + "&" + voucher + "&" + servicename;
    //    CommonFunction.vendorinputoutputResponce(Loginid, meroid, strUrl, "", servicename);
    //    HAPI.Authentication obj_Auth = new HAPI.Authentication();
    //    HAPI.HermesMobile obj_HMob = new HAPI.HermesMobile();
    //    HAPI.BillPaymentInput obj_BillInput = new BillPaymentInput();
    //    obj_BillInput.OperatorCode = OperatorCode;
    //    obj_BillInput.MobileNumber = Mobile;
    //    obj_BillInput.Amount = Convert.ToDouble(Amount);
    //    obj_BillInput.OtherDetails = "BillPayment";

    //    obj_Auth.LoginId = UserID;
    //    obj_Auth.Password = Pass;
    //    HAPI.BillPaymentDoneRequest obj_BillReq = new BillPaymentDoneRequest();
    //    HAPI.BillPaymentDoneResponse obj_BillRes = new BillPaymentDoneResponse();
    //    obj_BillReq.Authentication = obj_Auth;
    //    obj_BillReq.BillPaymentInput = obj_BillInput;
    //    obj_BillReq.UserTrackId = usertrackid;

    //    obj_BillRes = obj_HMob.GetBillPaymentDone(obj_BillReq);
    //    if (obj_BillRes.ResponseStatus == 1)
    //    {
    //        stresponse = HermesRechargeStatus(Loginid, meroid, usertrackid, servicename);
    //    }
    //    else
    //    {
    //        stresponse = Convert.ToString(obj_BillRes.ResponseStatus) + "&" + obj_BillRes.FailureRemarks;
    //    }
    //    CommonFunction.vendorinputoutputResponce(Loginid, meroid, "", stresponse, "");
    //    return stresponse;
    //}

    public static HttpWebRequest CreateWebRequest(string method)
    {
        //HttpWebRequest webRequest =(HttpWebRequest)WebRequest.Create(@"http://115.248.39.80/HermesMobAPI/HermesMobile.svc/SOAPService");
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://api.hermes-it.in/mobile/hermesmobile.svc/SoapService");
        webRequest.Headers.Add(@"SOAPAction: http://hermes-it.in/API/HermesMobile/" + method);
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Method = "POST";
        return webRequest;
    }
    public string bill_payment(string Loginid, string usertrackid, string meroid, string OperatorCode, string Mobile, double Amount, string voucher, string servicename)
    {
        string soapResult = string.Empty;
        string stresponse = string.Empty;

        HttpWebRequest request = CreateWebRequest("GetBillPaymentDone");
        XmlDocument soapEnvelopeXml = new XmlDocument();
        soapEnvelopeXml.LoadXml(@"<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'><s:Body><GetBillPaymentDone  xmlns='http://hermes-it.in/API'><GetBillPaymentDoneRequest  xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><Authentication><LoginId>" + UserID + @"</LoginId><Password>" + Pass + @"</Password></Authentication> <UserTrackId>" + usertrackid + @"</UserTrackId>
<BillPaymentInput><OperatorCode>" + OperatorCode + @"</OperatorCode><MobileNumber>" + Mobile + @"</MobileNumber><Amount>" + Amount + @"</Amount><OtherDetails>bill payment</OtherDetails></BillPaymentInput>
</GetBillPaymentDoneRequest></GetBillPaymentDone></s:Body></s:Envelope>");
        using (Stream stream = request.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }
        using (WebResponse response = request.GetResponse())
        {
            using (StreamReader rd = new StreamReader(response.GetResponseStream()))
            {
                soapResult = rd.ReadToEnd();

            }
        }
       // CommonFunction.writeToLogFile(Convert.ToString(soapResult));
        string strUrl = Loginid + "&" + Amount.ToString() + "&" + Mobile + "&" + OperatorCode + "&" + meroid + "&" + usertrackid + "&" + voucher + "&" + servicename;
        CommonFunction.vendorinputoutputResponce(Loginid, meroid, strUrl, "", servicename);
        DataSet ds = new DataSet();
        ds.ReadXml(new StringReader(soapResult));
        int valu = Convert.ToInt32(ds.Tables["GetBillPaymentDoneResult"].Rows[0]["ResponseStatus"]);
        if (valu == 1)
        {
            stresponse = HermesRechargeStatus(Loginid, meroid, usertrackid, servicename);
        }
        else
        {
            stresponse = valu.ToString() + "&" + Convert.ToInt32(ds.Tables["GetBillPaymentDoneResult"].Rows[0]["FailureRemarks"]);
        }
        CommonFunction.vendorinputoutputResponce(Loginid, meroid, "", stresponse, "");
        return stresponse;
    }


    public string getApibalance()
    {
        try
        {
            string RechargeStatus;
            HAPI.Authentication obj_Auth = new HAPI.Authentication();
            HAPI.HermesMobile obj_HMob = new HAPI.HermesMobile();
            HAPI.AgentCreditBalanceRequest obj_RechReq = new AgentCreditBalanceRequest();
            HAPI.AgentCreditBalanceResponse obj_RechRes = new AgentCreditBalanceResponse();
            obj_Auth.LoginId = UserID;
            obj_Auth.Password = Pass;
            obj_RechReq.Authentication = obj_Auth;

            obj_RechRes = obj_HMob.GetAgentCreditBalance(obj_RechReq);
            if (obj_RechRes.ResponseStatus == 1)
            {

                RechargeStatus = Convert.ToString(obj_RechRes.AgentCreditBalanceOutput.RemainingAmount);

            }
            else
            {
                RechargeStatus = "NA";
            }
            return RechargeStatus;
        }
        catch (Exception ex)
        {
            return "NA";
        }
    }

}