using SGModule.Common.Helper;
using SGModule.Net;
using UnityEngine.Events;

namespace SGModule.ApplePay
{
    public static class ApplePayNet
    {
        #region IOSPay
        ///  <summary>
        /// ios支付创建订单
        /// </summary>
        ///  <param name="data"></param>
        ///  <param name="onCompleted"></param>
        public static void ApplePayCreate<T>(ApplePayData data, UnityAction<ResponseData<T>> onCompleted = null)
        {
            NetKit.NetKit.Instance.Post<T>("shop/applePayCreate", data,
                response => { onCompleted?.Invoke(response); });
        }

        ///  <summary>
        /// ios支付取消订单
        /// </summary>
        ///  <param name="data"></param>
        ///  <param name="onCompleted"></param>
        public static void ApplePayCancel<T>(ApplePayData data, UnityAction<ResponseData<T>> onCompleted = null)
        {
            NetKit.NetKit.Instance.Post<T>("shop/applePayCancel", data,
                response => { onCompleted?.Invoke(response); });
        }
        public static void ApplePaySubscriptionHistory<T>(UnityAction<ResponseData<T>> onCompleted = null)
        {
            NetKit.NetKit.Instance.Post<T>("shop/subscribeHistory", null,
                response => { onCompleted?.Invoke(response); });
        }
        // public static void ApplePaySubscriptionCheck<T>(HistoryObject obj,UnityAction<ResponseData<T>> onCompleted = null)
        // {
        //     NetKit.NetKit.Instance.Post<T>("shop/subscribeCheck", obj,
        //         response => { onCompleted?.Invoke(response); });
        // }


        ///  <summary>
        /// ios支付验证订单
        /// </summary>
        ///  <param name="transactionID">apple支付返回的订单id</param>
        ///  <param name="innerOrderId">内部订单id，创建订单时返回的</param>
        ///  <param name="key">包名</param>
        ///  <param name="onCompleted">回调</param>
        public static void ApplePayCheck<T>(string transactionID, string innerOrderId,
            string key, UnityAction<ResponseData<T>> onCompleted = null)
        {
            var test = new AppleCheckData
            {
                signedPayload = Common.Helper.Cryptor.Encrypt(transactionID, key),
                innerOrderId = innerOrderId
            };

            NetKit.NetKit.Instance.Post<T>("shop/applePayCheck", test,
                response => { onCompleted?.Invoke(response); });
        }
        ///  <summary>
        /// ios支付订阅
        /// </summary>
        ///  <param name="transactionID">apple支付返回的订单id</param>
        ///  <param name="sku">商品id</param>
        ///  <param name="amount">价格</param>
        ///  <param name="key">包名</param>
        ///  <param name="onCompleted">回调</param>
        public static void AppleSubscribeCheck<T>(string transactionID, string sku, int amount,
            string key, UnityAction<ResponseData<T>> onCompleted = null)
        {
            var test = new AppleSubscribeData
            {
                signedPayload = Common.Helper.Cryptor.Encrypt(transactionID, key),
                sku = sku,
                amount = amount,
                currency = "USD"
            };
            Log.Info("[Apple pay]", $" AppleSubscribeCheck 开始...{sku}");

            NetKit.NetKit.Instance.Post<T>(
                "shop/appleSubscribe",
                test,
                response =>
                {
                    Log.Info("[Apple pay]", $"AppleSubscribeCheck 结束...{response.IsSuccess}");
                    onCompleted?.Invoke(response);
                });

        }

        #endregion
    }
}
