using SinochemCloudClient;
using SinochemCloudClient.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wayne.Lib.Log;

namespace SinochemInternetPlusApp
{
    public class CloudManager
    {
        private readonly string SiteId = GenericSinochemEpsApp.AppSettings["SinochemSiteId"];
        private readonly string UrlPrefix = GenericSinochemEpsApp.AppSettings["SinochemCloudUrlPrefix"];

        private readonly string BalanceInquiryApi = GenericSinochemEpsApp.AppSettings["SinochemCloudBalanceApi"];
        public BalanceInquiryResponse BalanceInquiry(string cardNo, string encryptedPin, string tid, int nozzleId, DebugLogger debugLogger)
        {
            CloudCommunicator<BalanceInquiryRequest, BalanceInquiryResponse> communicator =
                    new CloudCommunicator<BalanceInquiryRequest, BalanceInquiryResponse>(UrlPrefix + BalanceInquiryApi);
            BalanceInquiryRequest request = new BalanceInquiryRequest
            {
                tid = tid,
                datetime = DateTime.Now.ToString("yyyyMMddHHmmss"),
                gun = nozzleId.ToString(),
                //token = "" , // not available
                mobile = "",
                dept_No = SiteId,
                card_No = cardNo,
                password = encryptedPin
            };

            debugLogger.Add("Sending BalanceInquiry: \n" + request.ToString());
            communicator.SendRequest(request);
            debugLogger.Add("Got BalanceInquiry response: \n" + communicator.Response?.ToString());

            return communicator.Response;
        }

        private readonly string PaymentApi = GenericSinochemEpsApp.AppSettings["SinochemCloudPaymentApi"];
        public PaymentResponse Payment(EpsTransactionModel epsTrxModel, DebugLogger debugLogger)
        {
            CloudCommunicator<PaymentRequest, PaymentResponse> communicator =
                new CloudCommunicator<PaymentRequest, PaymentResponse>(UrlPrefix + PaymentApi);
            PaymentRequest request = new PaymentRequest
            {
                tid = epsTrxModel.tid,
                datetime = epsTrxModel.created_time.ToString("yyyyMMddHHmmss"),//DateTime.Now.ToString("yyyyMMddHHmmss"),
                gun = epsTrxModel.jihao.ToString(),
                mobile = "",
                dept_No = SiteId,
                ttc = epsTrxModel.ttc,
                card_No = epsTrxModel.card_no?.TrimStart('0'),
                password = epsTrxModel.EnryptedPIN,
                code = FuelGradeConvertor.GetGradeNo(epsTrxModel.youpin),
                price = epsTrxModel.danjia,
                lpm = epsTrxModel.qty,
                amount = epsTrxModel.amount,
                gas_Up_Time = epsTrxModel.auth_time,
                rear_Pump_Code = "",
                off_Line_Sign = "0",
                tradeDateTime = epsTrxModel.created_time.ToString("yyyyMMddHHmmss"),//DateTime.Now.ToString("yyyyMMddHHmmss"),
                mac = epsTrxModel.mac,
                token = epsTrxModel.token + DateTimeUtility.CurrentTimeMillis().ToString()
            };

            debugLogger.Add("Sending Payment: \n" + request.ToString());
            communicator.SendRequest(request);
            debugLogger.Add("Got Payment response: \n" + communicator.Response?.ToString());

            return communicator.Response;
        }

        private readonly string TrxStatusInquiryApi = GenericSinochemEpsApp.AppSettings["SinochemCloudTrxStatusApi"];
        public TrxStatusInquiryResponse TrxStatusInquiry(EpsTransactionModel epsTrxModel, DebugLogger debugLogger)
        {
            CloudCommunicator<TrxStatusInquiryRequest, TrxStatusInquiryResponse> communicator =
                new CloudCommunicator<TrxStatusInquiryRequest, TrxStatusInquiryResponse>(UrlPrefix + TrxStatusInquiryApi);
            TrxStatusInquiryRequest request = new TrxStatusInquiryRequest
            {
                tid = epsTrxModel.tid,
                datetime = DateTime.Now.ToString("yyyyMMddHHmmss"),
                gun = epsTrxModel.jihao.ToString(),
                mobile = "",
                dept_No = SiteId,
                card_No = epsTrxModel.card_no,
                pre_Amount = epsTrxModel.real_pay_amount,
                ori_ttc = epsTrxModel.ttc,
                token = epsTrxModel.token + DateTimeUtility.CurrentTimeMillis().ToString()
            };

            debugLogger.Add("Sending TrxStatusInquiry: \n" + request.ToString());
            communicator.SendRequest(request);
            debugLogger.Add("Got TrxStatusInquiry response: \n" + communicator.Response?.ToString());

            return communicator.Response;
        }


        private readonly string RefundApi = GenericSinochemEpsApp.AppSettings["SinochemCloudRefundApi"];
        public RefundResponse Refund(EpsTransactionModel epsTrxModel, DebugLogger debugLogger)
        {
            CloudCommunicator<RefundRequest, RefundResponse> communicator =
                new CloudCommunicator<RefundRequest, RefundResponse>(UrlPrefix + RefundApi);
            RefundRequest request = new RefundRequest
            {
                tid = epsTrxModel.tid,
                datetime = DateTime.Now.ToString("yyyyMMddHHmmss"),
                gun = epsTrxModel.jihao.ToString(),
                mobile = "",
                dept_No = SiteId,
                card_No = epsTrxModel.card_no,
                pre_Amount = epsTrxModel.real_pay_amount,
                ori_ttc = epsTrxModel.ttc,
                token = epsTrxModel.token + DateTimeUtility.CurrentTimeMillis().ToString()
            };

            debugLogger.Add("Sending Refund: \n" + request.ToString());
            communicator.SendRequest(request);
            debugLogger.Add("Got Refund response: \n" + communicator.Response?.ToString());

            return communicator.Response;
        }
    }

    class DateTimeUtility
    {
        private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        public static long CurrentTimeMillis()
        {
            return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
        }
    }
}