﻿using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using UnityEngine;
using UnityEngine.Networking;

namespace NeoriginPCSDK
{
    public class VirtualCoinImpl : NeoBasePay
    {
        protected override void doPay(InterPayParams interPayParams, NeoriginSDK.PayCallBack callBack)
        {
            if (interPayParams == null)
            {
                callBack?.Invoke(NeoriginSDKStatusCode.ParameterError, "payParams is null");
                return;
            }
            
            Dictionary<string, string> reqParams = new Dictionary<string, string>
            {
                { "game_order_id", interPayParams.gameOrderId },
                { "server_id", interPayParams.serverId },
                { "role_name", interPayParams.roleName },
                { "role_id", interPayParams.roleId },
                { "pay_channel_id", interPayParams.payChannel },
                { "desc", interPayParams.productDesc },
                { "product_id", interPayParams.productId },
                { "extra", interPayParams.extra },
            };
            NeoriginSDKLoadingManager.Instance.Show(NeoriginSDK.Instance.GetSDKLangTranslation("paying")); 
            PayRequest(reqParams, (code, msg, status, retData) =>
            {
                NeoriginSDKLoadingManager.Instance.Dismiss();
                if (code != NeoriginSDKStatusCode.Success)
                {
                    Debug.Log($"VirtualCoinImpl PayResultRequest Error: {msg},code {code}");
                    callBack?.Invoke(code: code, msg: $"Platform request error.code:{code},Error:{msg}");
                    NeoriginSDKToastManager.Instance.Show(NeoriginSDK.Instance.GetSDKLangTranslation("pay_fail"));
                    return;
                }

                // 虚拟币不足或者没有虚拟币，跳转虚拟币不足充值页
                if (status == PAY_VIRTUALCOIN_NOT_ENOUGH || status == PAY_VIRTUALCOIN_NOT_EXIST)
                {
                    NeoriginPlatformFactory.ShowInsufficientVirtualCoin(interPayParams);
                    callBack?.Invoke(code: NeoriginSDKStatusCode.InternalError, msg: $"Platform return error.code:{status},message:{msg}");
                    return;
                }
                
                if (status != PAY_REQ_RESULT_SUCCESS) // 0:成功
                {
                    Debug.Log($"VirtualCoinImpl PayResultRequest Error: {msg}");
                    callBack?.Invoke(code: NeoriginSDKStatusCode.InternalError, msg: $"Platform return error.code:{status},message:{msg}");
                    NeoriginSDKToastManager.Instance.Show(NeoriginSDK.Instance.GetSDKLangTranslation("system_busy"));
                    return;
                }
                NeoriginSDKToastManager.Instance.Show(NeoriginSDK.Instance.GetSDKLangTranslation("pay_succ"));
                callBack?.Invoke(code: NeoriginSDKStatusCode.Success, msg: "");
            });
        }
        
        private void PayRequest(Dictionary<string, string> reqParams, NeoHttpUtils.HttpRequestCallBack callback)
        {
            Debug.Log("VirtualCoinImpl PayRequest #######:");
            
            NeoriginSDKPlatformUserInfo user = NeoriginSDKStorage.LoadPlatformUserInfo();
            string time = System.DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
            reqParams.Add("uuid", user.uuid);
            reqParams.Add("token", user.token);
            reqParams.Add("time", time);
            
            NeoHttpUtils.MakePostResult("virtualcoin/exchange", reqParams, callback);
        }
        
        protected override Dictionary<string, string> getExtraParams()
        {
          return new Dictionary<string, string>();
        }
    }
}
