package com.fq.channel.sdk.api;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.fq.channel.sdk.api.bean.LoginInfo;
import com.fq.channel.sdk.api.bean.PaymentInfo;
import com.fq.channel.sdk.base.constants.AppConfig;
import com.fq.channel.sdk.base.constants.FqConfig;
import com.fq.channel.sdk.base.interfaces.IPermissionRequestCallback;
import com.fq.channel.sdk.base.interfaces.PluginResult;
import com.fq.channel.sdk.base.interfaces.PluginResultHandler;
import com.fq.channel.sdk.base.net.HttpManager;
import com.fq.channel.sdk.base.permission.PermissionManager;
import com.fq.channel.sdk.base.utils.DeviceUtils;
import com.fq.channel.sdk.base.utils.FqLog;
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: api逻辑处理
 * @Author: FLuty
 * @CreateDate: 2020/4/3 15:59
 */
public class FqGameHander {

    private static final String TAG = "FqGameHander";

    private static IResult<String> resultInit;

    public static void init(Activity activity, IResult<String> result) {
        resultInit = result;
        //初始化配置
        initSDKConfig(activity);
        PermissionManager.getInstance().applyPermission(activity, new IPermissionRequestCallback() {
            @Override
            public void onDone() {
                //处理获取到读取IMEI权限的
                String imei = DeviceUtils.getIMEI();
                AppConfig.sImei = TextUtils.isEmpty(imei) ? "" : imei;
                initSDKNet();
            }
        });
    }

    /**
     * 网络初始化
     */
    private static void initSDKNet() {
        //TODO
//        FqNetRequest.init(new BaseCallback<ResultInitBody>() {
//            @Override
//            public void onFailure(BaseException msg) {
//                FqLog.e(TAG, "initSDKNet onFailure :"+msg.getMsg());
//                resultInit.onFail(msg.getMsg());
//            }
//
//            @Override
//            public void onSuccess(int code, String msg, ResultInitBody data) {
//                FqLog.i(TAG, "initSDKNet onSuccess :" + data.toString());
        initChannel(resultInit);
//            }
//        });
    }

    /**
     * sdk配置初始化
     */
    private static void initSDKConfig(Activity activity) {
        //初始化
        FqConfig fqConfig = FqConfig.get(activity);
        fqConfig.setCurrentActivity(activity);
        fqConfig.init(activity);
        fqConfig.commit();
        HttpManager.getInstance().initHttp();
        JointManager.getInstance().initCfgConfigFile(activity);
    }

    /**
     * 渠道初始化
     */
    private static void initChannel(final IResult<String> resultInit) {
        JointManager.getInstance().init(FqConfig.get().getApplicationContext(), new PluginResultHandler() {
            @Override
            public void onHandlePluginResult(PluginResult result) {
                PluginResult.Status mStatus = result.getStatus();
                FqLog.i(TAG, "init mStatus :" + mStatus);
                if (PluginResult.Status.OK == mStatus) {
                    resultInit.onSuccess(result.getMessage());
                } else if (PluginResult.Status.CANCEL == mStatus) {
                    resultInit.onFail(result.getMessage());
                } else if (PluginResult.Status.ERROR == mStatus) {
                    resultInit.onFail(result.getMessage());
                }
            }
        });
    }

    public static void login(@NonNull Activity activity, final IResult<LoginInfo> resultLoginInfo) {
        JointManager.getInstance().login(activity, new PluginResultHandler() {
            @Override
            public void onHandlePluginResult(PluginResult result) {
                PluginResult.Status mStatus = result.getStatus();
                FqLog.i(TAG, "login mStatus :" + mStatus);

                if (PluginResult.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 (PluginResult.Status.CANCEL == mStatus) {
                    resultLoginInfo.onFail(result.getMessage());
                } else if (PluginResult.Status.ERROR == mStatus) {
                    resultLoginInfo.onFail(result.getMessage());
                }
            }
        });
    }

    public static void pay(@NonNull Activity activity, @NonNull final PaymentInfo paymentInfo, final IResult<String> resultPay) {
        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) {
                    PluginResult.Status mStatus = result.getStatus();
                    FqLog.i(TAG, "pay mStatus :" + mStatus);
                    if (PluginResult.Status.OK == mStatus) {
                        resultPay.onSuccess(result.getMessage());
                    } else if (PluginResult.Status.CANCEL == mStatus) {
                        resultPay.onFail(result.getMessage());
                    } else if (PluginResult.Status.ERROR == mStatus) {
                        resultPay.onFail(result.getMessage());
                    }
                }
            });
        } catch (IllegalAccessException e) {
            resultPay.onFail("params is null");
            e.printStackTrace();
        }
    }

    public static void exit(Activity activity, final IExit exit) {
        JointManager.getInstance().exit(activity, new PluginResultHandler() {
            @Override
            public void onHandlePluginResult(PluginResult result) {
                PluginResult.Status mStatus = result.getStatus();
                FqLog.i(TAG, "exit mStatus :" + mStatus);
                if (PluginResult.Status.OK == mStatus) {
                    exit.onExit();
                } else if (PluginResult.Status.CANCEL == mStatus) {
                    exit.onNoConfirmDialogExit();
                }
            }
        });
    }

    protected static void onRequestPermissionsResult(int requestCode,
                                                     String[] permissions, int[] grantResults) {
        PermissionManager.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}
