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

/// <summary>
/// Summary description for Hermes_Recharge_Bill
/// </summary>
public class Hermes_Recharge_Bill
{
    public static string UserID = "Venus";
    public static string Pass = "apiVenus";
    int valu, transStatus;
    double Amount;
    string UserTrackId, Remarks, ReferenceNumber, MobileNumber, OperatorTransactionId, FailureRemarks;
    public Hermes_Recharge_Bill()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    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>");
       // CommonFunction.writeToLogFile(Convert.ToString(soapEnvelopeXml.InnerXml));
        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"]);
        //int valu = Convert.ToInt32(ResponseStatus.InnerText);

        if (valu == 1)
        {
            stresponse = HermesRechargeStatus(Loginid, meroid, usertrackid, servicename);
            // response = obj_BillPay_Res.ResponseStatus.ToString() + obj_BillPay_Res.BillPaymentOutput.MobileNumber + obj_BillPay_Res.BillPaymentOutput.OperatorDescription + obj_BillPay_Res.BillPaymentOutput.ReferenceNumber  + obj_BillPay_Res.UserTrackId+ Convert.ToString(obj_BillPay_Res.BillPaymentOutput.Amount);
        }
        else
        {
           // XmlNode FailureRemarks = doc.SelectSingleNode("GetBillPaymentDoneResponse/GetBillPaymentDoneResult/FailureRemarks");
            stresponse = valu.ToString() + "&" + Convert.ToInt32(ds.Tables["GetBillPaymentDoneResult"].Rows[0]["FailureRemarks"]);
        }
        CommonFunction.vendorinputoutputResponce(Loginid, meroid, "", stresponse, "");
        return stresponse;
    }


    public string HermesRechargeStatus(string Loginid, string meroid, string usertrackid, string servicename)
    {
        string soapResult = string.Empty;
        string stresponse = string.Empty;
      


        HttpWebRequest request = CreateWebRequest("GetTransactionStatus");
        XmlDocument soapEnvelopeXml = new XmlDocument();
        soapEnvelopeXml.LoadXml(@"<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'><s:Body><GetTransactionStatus xmlns='http://hermes-it.in/API'><GetTransactionStatusRequest xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><Authentication><LoginId>" + UserID + @"</LoginId><Password>" + Pass + @"</Password></Authentication><UserTrackId>" + usertrackid + @"</UserTrackId></GetTransactionStatusRequest></GetTransactionStatus></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 = "GetTransactionStatus" + "&" + usertrackid;
        CommonFunction.vendorinputoutputResponce(Loginid, meroid, strUrl, "", servicename);
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(soapResult);
      //  DataSet ds = new DataSet();
      //  ds.ReadXml(new StreamReader(soapResult));
        XmlNodeList nodes1 = doc.GetElementsByTagName("GetTransactionStatusResult");

        foreach (XmlNode xNode in nodes1)
        {
            XmlNodeList innerNodeList = xNode.ChildNodes;
            foreach (XmlNode xNodeInner in innerNodeList)
            {
                if (xNodeInner.Name == "ResponseStatus")
                {
                    valu = Convert.ToInt32(xNodeInner.InnerText);
                }
                if (xNodeInner.Name == "UserTrackId")
                {
                    UserTrackId = xNodeInner.InnerText;
                }
            }
        }
      
        if (valu == 1)
        {
            XmlNodeList nodes2 = doc.GetElementsByTagName("TransactionStatusOutput");

            foreach (XmlNode xNode1 in nodes2)
            {
                XmlNodeList innerNodeList = xNode1.ChildNodes;
                foreach (XmlNode xNodeInner in innerNodeList)
                {
                    if (xNodeInner.Name == "TransactionStatus")
                    {
                        transStatus = Convert.ToInt32(xNodeInner.InnerText);
                    }
                    if (xNodeInner.Name == "Remarks")
                    {
                        Remarks = xNodeInner.InnerText;
                    }
                }
            }
           
            int TransactionStatus_valu = transStatus;
            if (TransactionStatus_valu == 1)
            {

                XmlNodeList nodes3 = doc.GetElementsByTagName("TransactionDetails");

                foreach (XmlNode xNode1 in nodes3)
                {
                    XmlNodeList innerNodeList = xNode1.ChildNodes;
                    foreach (XmlNode xNodeInner in innerNodeList)
                    {
                        if (xNodeInner.Name == "ReferenceNumber")
                        {
                            ReferenceNumber = Convert.ToString(xNodeInner.InnerText);
                        }
                        if (xNodeInner.Name == "MobileNumber")
                        {
                            MobileNumber = xNodeInner.InnerText;
                        }
                        if (xNodeInner.Name == "Amount")
                        {
                            Amount = Convert.ToDouble(xNodeInner.InnerText);
                        }
                        if (xNodeInner.Name == "OperatorTransactionId")
                        {
                            OperatorTransactionId = xNodeInner.InnerText;
                        }
                    }
                }
                stresponse = valu.ToString() + "&" + MobileNumber + "&" + Remarks + "&" + ReferenceNumber + "&" + UserTrackId + "&" + Convert.ToString(Amount) + "&" + Convert.ToString(TransactionStatus_valu) + "&" + OperatorTransactionId;
            }
            else if (TransactionStatus_valu == 3)
            {
                stresponse = valu.ToString() + "&" + "" + "&" + Remarks + "&" + "" + "&" + UserTrackId + "&" + Convert.ToString("") + "&" + Convert.ToString(TransactionStatus_valu);
            }
            else
            {
                XmlNodeList nodes4 = doc.GetElementsByTagName("TransactionDetails");

                foreach (XmlNode xNode1 in nodes4)
                {
                    XmlNodeList innerNodeList = xNode1.ChildNodes;
                    foreach (XmlNode xNodeInner in innerNodeList)
                    {
                        if (xNodeInner.Name == "ReferenceNumber")
                        {
                            ReferenceNumber = Convert.ToString(xNodeInner.InnerText);
                        }
                        if (xNodeInner.Name == "MobileNumber")
                        {
                            MobileNumber = xNodeInner.InnerText;
                        }
                        if (xNodeInner.Name == "Amount")
                        {
                            Amount = Convert.ToDouble(xNodeInner.InnerText);
                        }

                    }
                }
                stresponse = valu.ToString() + "&" + MobileNumber + "&" + Remarks + "&" + ReferenceNumber + "&" + UserTrackId + "&" + Amount.ToString() + "&" + Convert.ToString(TransactionStatus_valu);
            }
        }
        else
        {
            XmlNodeList nodes5 = doc.GetElementsByTagName("GetTransactionStatusResult");

            foreach (XmlNode xNode in nodes5)
            {
                XmlNodeList innerNodeList = xNode.ChildNodes;
                foreach (XmlNode xNodeInner in innerNodeList)
                {
                    if (xNodeInner.Name == "ResponseStatus")
                    {
                        valu = Convert.ToInt32(xNodeInner.InnerText);
                    }
                    if (xNodeInner.Name == "UserTrackId")
                    {
                        UserTrackId = xNodeInner.InnerText;
                    }
                    if (xNodeInner.Name == "FailureRemarks")
                    {
                        FailureRemarks = xNodeInner.InnerText;
                    }
                }
            }

            stresponse = valu.ToString() + "&" + UserTrackId + "&" + FailureRemarks;
        }
        CommonFunction.vendorinputoutputResponce(Loginid, meroid, "", stresponse, "");

        return stresponse;
    }


    public string mobile_recharge(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("GetRechargeDone");
        XmlDocument soapEnvelopeXml = new XmlDocument();
        soapEnvelopeXml.LoadXml(@"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:api='http://hermes-it.in/API'><soapenv:Header/><soapenv:Body><api:GetRechargeDone><api:GetRechargeDoneRequest><api:Authentication><api:LoginId>" + UserID + @"</api:LoginId><api:Password>"+Pass+@"</api:Password></api:Authentication><api:UserTrackId>" + usertrackid + @"</api:UserTrackId><api:RechargeInput><api:OperatorCode>" + OperatorCode + @"</api:OperatorCode><api:MobileNumber>" + Mobile + @"</api:MobileNumber><api:Amount>" + Amount + @"</api:Amount></api:RechargeInput></api:GetRechargeDoneRequest></api:GetRechargeDone></soapenv:Body></soapenv:Envelope>");
        // soapEnvelopeXml.LoadXml(@"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:api='http://hermes-it.in/API'><soapenv:Header/><soapenv:Body><api:GetRechargeOperators><api:GetRechargeOperatorsRequest><api:Authentication><api:LoginId>Venus</api:LoginId><api:Password>apiVenus</api:Password></api:Authentication></api:GetRechargeOperatorsRequest></api:GetRechargeOperators></soapenv:Body></soapenv: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));
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(soapResult);
        XmlNodeList nodes1 = doc.GetElementsByTagName("GetRechargeDoneResult");
        foreach (XmlNode xNode in nodes1)
        {
            XmlNodeList innerNodeList = xNode.ChildNodes;
            foreach (XmlNode xNodeInner in innerNodeList)
            {
                if (xNodeInner.Name == "ResponseStatus")
                {
                    valu = Convert.ToInt32(xNodeInner.InnerText);
                }
                //if (xNodeInner.Name == "UserTrackId")
                //{
                //    UserTrackId = xNodeInner.InnerText;
                //}
            }
        }

        

        if (valu == 1)
        {
            stresponse = HermesRechargeStatus(Loginid, meroid, usertrackid, servicename);
            // response = obj_BillPay_Res.ResponseStatus.ToString() + obj_BillPay_Res.BillPaymentOutput.MobileNumber + obj_BillPay_Res.BillPaymentOutput.OperatorDescription + obj_BillPay_Res.BillPaymentOutput.ReferenceNumber  + obj_BillPay_Res.UserTrackId+ Convert.ToString(obj_BillPay_Res.BillPaymentOutput.Amount);
        }
        else
        {

            XmlNodeList nodes5 = doc.GetElementsByTagName("GetRechargeDoneResult");

            foreach (XmlNode xNode in nodes5)
            {
                XmlNodeList innerNodeList = xNode.ChildNodes;
                foreach (XmlNode xNodeInner in innerNodeList)
                {
                    if (xNodeInner.Name == "ResponseStatus")
                    {
                        valu = Convert.ToInt32(xNodeInner.InnerText);
                    }
                    if (xNodeInner.Name == "UserTrackId")
                    {
                        UserTrackId = xNodeInner.InnerText;
                    }
                    if (xNodeInner.Name == "FailureRemarks")
                    {
                        FailureRemarks = xNodeInner.InnerText;
                    }
                }
            }
            // XmlNode FailureRemarks = doc.SelectSingleNode("GetBillPaymentDoneResponse/GetBillPaymentDoneResult/FailureRemarks");
            stresponse = valu.ToString() + "&" + FailureRemarks;
        }
        CommonFunction.vendorinputoutputResponce(Loginid, meroid, "", stresponse, "");
        return stresponse;
    }
}