using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;

/// <summary>
/// Summary description for GTVProcess
/// </summary>
public class GTVProcess
{
	public static string clientId = "NORTH_BIHAR_POWER_DISTRIBUTION_COM_Digi_Qr";
    public static string client_secret = "iisOS3oO8kTmPuy7Bj0JOe4VcTu6ZYZq";
    public static string grant_type = "client_credentials";
    public static string scope = "offline_access";
    public string GetToken()
	{
        string Token = "";
        string data = "client_id=" + clientId + "&client_secret=" + client_secret + "&grant_type=" + grant_type + "&scope=" + scope;
        string posturl = "https://identity.pinelabs.com/realms/pinelabs/protocol/openid-connect/token";
        try
        {
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            // string data = string.Format("ResponseXML={0}", xml);
            byte[] bytes = encoding.GetBytes(data);
            const System.Security.Authentication.SslProtocols _Tls12 = (System.Security.Authentication.SslProtocols)0x00000C00;
            const SecurityProtocolType Tls12 = (SecurityProtocolType)_Tls12;
            ServicePointManager.SecurityProtocol = Tls12;
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(posturl);

            httpRequest.Credentials = new NetworkCredential("1234567891", "Trimax#123");

            httpRequest.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;

            httpRequest.Method = "POST";
            httpRequest.ContentType = "application/x-www-form-urlencoded";
            httpRequest.ContentLength = bytes.Length;
            using (Stream stream = httpRequest.GetRequestStream())
            {
                stream.Write(bytes, 0, bytes.Length);
                stream.Close();
            }
            HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse();
            Stream s = (Stream)response.GetResponseStream();
            StreamReader readStream = new StreamReader(s);
            string dataString = readStream.ReadToEnd();
            response.Close();
            s.Close();
            readStream.Close();
            if (dataString != "")
            {
                JObject joResponse = JObject.Parse(dataString);
                JValue TokenVal = (JValue)joResponse["access_token"];
                Token = TokenVal.ToString();
            }
        }
        catch (Exception ex)
        {
            Token = "";
        }
        return Token;
    }

    public BOGTVDetail.GTVResponseCard GetGTVDetailsForChargeSlipNo(string ChargeSlipNo,string PaymentMode)
    {
        string Result= string.Empty;
        BOGTVDetail.GTVResponseCard objData =new BOGTVDetail.GTVResponseCard();
        string strUrl = "https://apigw.pinelabs.com/billing-integration/qr-payments/transactions/fetch/transaction?transactionId=" + ChargeSlipNo + "&reportType=" + PaymentMode;
        try
        {
            string Token = GetToken();
            if (Token != "")
            {
                WebRequest request = HttpWebRequest.Create(strUrl);
                request.Method = "GET";
                request.Headers.Add("x-internal-token", Token);
                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();
                Result = dataString;
                if (Result != "")
                {
                    SB_Infinite objSB = new SB_Infinite();
                    objSB.InsertServiceRequestResponseLog("GTVStatusCheck", ChargeSlipNo + "&" + PaymentMode, Result);
                    objData = JsonConvert.DeserializeObject<BOGTVDetail.GTVResponseCard>(Result);
                    if (objData == null)
                    {
                        objData.responseMessage = Result;
                    }
                    
                }
                else
                {
                    objData.responseMessage = Result;
                }
            }
            else
            {
                objData.responseMessage = "Invalid Token. Please retry.";
                
            }
        }
        catch (Exception ex)
        {
            objData.responseMessage = ex.Message;
            
        }
        return objData;
    }
}