package cn.yyxx.commsdk.impl.channel.oppo;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;

import com.nearme.game.sdk.GameCenterSDK;
import com.nearme.game.sdk.callback.ApiCallback;
import com.nearme.game.sdk.callback.GameExitCallback;
import com.nearme.game.sdk.common.model.biz.PayInfo;
import com.nearme.game.sdk.common.model.biz.ReportUserGameInfoParam;
import com.nearme.game.sdk.common.model.biz.ReqUserInfoParam;

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

import java.util.HashMap;
import java.util.Map;

import cn.yyxx.commsdk.base.entity.ResultInfo;
import cn.yyxx.commsdk.base.entity.SdkOrderInfo;
import cn.yyxx.commsdk.base.entity.SdkRoleInfo;
import cn.yyxx.commsdk.base.entity.bean.InitBean;
import cn.yyxx.commsdk.base.internal.ICallback;
import cn.yyxx.commsdk.base.internal.feature.IApplication;
import cn.yyxx.commsdk.base.internal.feature.IFeature;
import cn.yyxx.commsdk.base.internal.feature.ILifeCycle;
import cn.yyxx.commsdk.base.internal.feature.IOrder;
import cn.yyxx.commsdk.impl.channel.LoginViewModel;
import cn.yyxx.commsdk.impl.channel.utils.ParamsUtils;

/**
 * @author #Suyghur.
 * Created on 2021/12/25
 */
public class ChannelBridgeImpl implements IFeature, ILifeCycle, IApplication, IOrder {

    String adId = "",channel = "";

    LoginViewModel loginViewModel;

    public ChannelBridgeImpl(Context context){

    }

    @Override
    public void attachBaseContext(Application application, Context context) {

    }

    @Override
    public void initApplication(Application application) {




        String appSecret = ParamsUtils.getCfgParam(application,"APP_SECRET");



        GameCenterSDK.init(appSecret, application);

    }

    @Override
    public void initialize(Activity activity, ICallback callback) {

    }

    @Override
    public void initializeSuccess() {

    }

    @Override
    public void setLogoutListener(Activity activity, ICallback callback) {

    }

    @Override
    public void login(final Activity activity, InitBean initBean, ICallback callback) {

        loginViewModel  = new LoginViewModel(callback);
        GameCenterSDK.getInstance().doLogin(activity, new ApiCallback() {

            @Override
            public void onSuccess(String s) {
                GameCenterSDK.getInstance().doGetTokenAndSsoid(
                        new ApiCallback() {


                            @Override
                            public void onSuccess(final String resultMsg) {
                                verfiyWtithAdid(activity,resultMsg);
                            }

                            @Override
                            public void onFailure(String arg0, int arg1) {
                                // TODO Auto-generated method stub

                            }
                        });
            }

            @Override
            public void onFailure(String s, int i) {

            }
        });
    }

    void verfiyWtithAdid(final Activity activity, final String resultMsg){
        try {
            JSONObject json  = new JSONObject(resultMsg);

            String token = json.optString("token");
            String ssoid = json.optString("ssoid");


            GameCenterSDK.getInstance().doGetUserInfo(new ReqUserInfoParam(token,ssoid), new ApiCallback() {
                @Override
                public void onSuccess(String s) {

                    try {
                        JSONObject json  = new JSONObject(s);
                        adId = json.getString("adId");
                        channel = json.getString("channel");
                        if (!TextUtils.isEmpty(adId) || !TextUtils.isEmpty(channel)){

                            json.put("adId",adId);
                            json.put("channel",channel);

                         }

                        loginViewModel.verifyChannelinfo(activity,json.toString());


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFailure(String s, int i) {

                 }
            });



        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void logout() {

    }

    @Override
    public boolean hasExitView(Activity activity) {
        return true;
    }

    @Override
    public void openExitView(Activity activity, final ICallback callback) {
        GameCenterSDK.getInstance().onExit(activity, new GameExitCallback() {
            @Override
            public void exitGame() {
                callback.onResult(ResultInfo.CODE_SUCCESS,"Exit Game.");
            }
        });
    }

    @Override
    public void uploadRoleInfo(Activity activity, String type, SdkRoleInfo roleInfo) {

        String role_power = roleInfo.getRolePower();
        String roleLevel = roleInfo.getRoleLevel();
        String roleId = roleInfo.getRoleId();
        String roleName = roleInfo.getRoleName();
        String serverId = roleInfo.getServerId();
        String serverName = roleInfo.getServerName();
        String vipLevel = roleInfo.getVipLevel();


        int i_roleLevel = Integer.valueOf(roleLevel).intValue();

        Map<String,Number> map = new HashMap();

        if (roleInfo.getRolePower().length() > 9){


            map.put("combatValue",Integer.valueOf("999999999").intValue());

        }else{


            map.put("combatValue",Integer.valueOf(role_power).intValue());

        }



        GameCenterSDK.getInstance().doReportUserGameInfoData(new ReportUserGameInfoParam(roleId, roleName, i_roleLevel, serverId, serverName, vipLevel, map), new ApiCallback() {
            @Override
            public void onSuccess(String s) {

            }

            @Override
            public void onFailure(String s, int i) {

            }
        });


    }

    @Override
    public void reportGameEvent(String event, JSONObject message) {

    }

    @Override
    public void onStart(Activity activity) {

    }

    @Override
    public void onResume(Activity activity) {

    }

    @Override
    public void onReStart(Activity activity) {

    }

    @Override
    public void onPause(Activity activity) {

    }

    @Override
    public void onStop(Activity activity) {

    }

    @Override
    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {

    }

    @Override
    public void onNewIntent(Activity activity, Intent intent) {

    }

    @Override
    public void onConfigurationChanged(Activity activity, Configuration newconfig) {

    }

    @Override
    public void onWindowFocusChanged(Activity activity, boolean hasFocus) {

    }

    @Override
    public void onDestroy(Activity activity) {

    }

    @Override
    public void onCreate(Activity activity, Bundle savedInstanceState) {

    }

    @Override
    public void startPay(Activity activity, SdkOrderInfo sdkOrderInfo, final ICallback callback) {


        String pay_money = sdkOrderInfo.getSdkChargeInfo().getPay_money();
        String order_id = sdkOrderInfo.getOrder_id();
        int i_pay_money = Integer.valueOf(pay_money).intValue();

        String order_desc = sdkOrderInfo.getSdkChargeInfo().getOrder_desc();
        String order_name = sdkOrderInfo.getSdkChargeInfo().getOrder_name();





        String ext = sdkOrderInfo.getOrder_id();

        String payExt = sdkOrderInfo.getOrder_ext();
        String callbackUrl=null;

        if (!ext.equals(""))
        {
            try {
                JSONObject vivoExtraInfo = new JSONObject(payExt);
                callbackUrl = vivoExtraInfo.optString("notifyUrl", "");

            } catch (JSONException e) {


                return ;
            }
        }


        PayInfo payInfo = new PayInfo(order_id, order_id, i_pay_money);
        payInfo.setProductDesc(order_desc);
        payInfo.setProductName(order_name);
        payInfo.setCallbackUrl(callbackUrl);





        GameCenterSDK.getInstance().doPay(activity, payInfo, new ApiCallback() {
            @Override
            public void onSuccess(String s) {
                callback.onResult(ResultInfo.CODE_SUCCESS,s);
            }

            @Override
            public void onFailure(String s, int i) {
                callback.onResult(ResultInfo.CODE_FAIL,s);
            }
        });



    }

    @Override
    public String getChannelOrderExt() {

        if (TextUtils.isEmpty(adId) || TextUtils.isEmpty(channel)){
            return "";
        }else {
            JSONObject json = new JSONObject();
            try {
                json.put("adId",adId);
                json.put("channel",channel);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return json.toString();
        }
    }
}
