package com.fq.channel.sdk.api;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.util.Log;

import com.fq.channel.sdk.api.bean.LoginInfo;
import com.fq.channel.sdk.api.bean.PaymentInfo;
import com.fq.channel.sdk.base.interfaces.PluginResult.Status;
import com.fq.channel.sdk.base.interfaces.PluginResult;
import com.fq.channel.sdk.base.interfaces.PluginResultHandler;
import com.fq.channel.sdk.base.utils.Utils;
import com.fq.channel.sdk.core.JointManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * @Description: 提供给CP访问的接口类
 * @Author: FLuty
 * @CreateDate: 2020/3/24 11:21
 */
public class FqGame {

    private static final String TAG = "FQSDK_LOGFqGame";

    /**
     * 提过给CP调用的初始化接口
     *
     * @param activity
     * @param resultInit
     */
    public static void init(@NonNull Activity activity, final IResult<String> resultInit) {
        Log.i(TAG, "init");
        JointManager.getInstance().init(activity, new PluginResultHandler() {
            @Override
            public void onHandlePluginResult(PluginResult result) {
                Status mStatus = result.getStatus();
                Log.i(TAG, "init mStatus :" + mStatus);
                if (Status.OK == mStatus) {
                    resultInit.onSuccess(result.getMessage());
                } else if (Status.CANCEL == mStatus) {
                    resultInit.onFail(result.getMessage());
                } else if (Status.ERROR == mStatus) {
                    resultInit.onFail(result.getMessage());
                }
            }
        });
    }

    /**
     * 提供给CP调用登录的接口
     *
     * @param activity
     */
    public static void login(@NonNull Activity activity, final IResult<LoginInfo> resultLoginInfo) {
        Log.i(TAG, "login");
        JointManager.getInstance().login(activity, new PluginResultHandler() {
            @Override
            public void onHandlePluginResult(PluginResult result) {
                Status mStatus = result.getStatus();
                Log.i(TAG, "login mStatus :" + mStatus);

                if (Status.OK == mStatus) {
                    Object res_msg = result.getRawMessage();
                    JSONObject jo = (JSONObject) res_msg;
                    try {
                        resultLoginInfo.onSuccess(new LoginInfo(jo.get("uid").toString(), jo.get("token").toString()));
                    } catch (JSONException e) {
                        resultLoginInfo.onFail("JSONException");
                        e.printStackTrace();
                    }
                } else if (Status.CANCEL == mStatus) {
                    resultLoginInfo.onFail(result.getMessage());
                } else if (Status.ERROR == mStatus) {
                    resultLoginInfo.onFail(result.getMessage());
                }
            }
        });
    }

    /**
     * 提供给CP调用的支付接口
     *
     * @param activity
     * @param paymentInfo CP传入的订单信息
     * @param resultPay
     */
    public static void pay(@NonNull Activity activity, @NonNull final PaymentInfo paymentInfo, final IResult<String> resultPay) {
        Log.i(TAG, "pay");
        try {
            Map map = Utils.getObjectToMap(paymentInfo);
            if (map == null){
                resultPay.onFail("params is null");
                return;
            }
            JointManager.getInstance().pay(activity, map, new PluginResultHandler() {
                @Override
                public void onHandlePluginResult(PluginResult result) {
                    Status mStatus = result.getStatus();
                    Log.i(TAG, "pay mStatus :" + mStatus);
                    if (Status.OK == mStatus) {
                        resultPay.onSuccess(result.getMessage());
                    } else if (Status.CANCEL == mStatus) {
                        resultPay.onFail(result.getMessage());
                    } else if (Status.ERROR == mStatus) {
                        resultPay.onFail(result.getMessage());
                    }
                }
            });
        } catch (IllegalAccessException e) {
            resultPay.onFail("params is null");
            e.printStackTrace();
        }
    }


    /**
     * 提供给CP接收回调的接口
     */

    public interface IResult<T> {

        void onSuccess(T t);

        void onFail(String failMsg);
    }

}
