package cn.yyxx.commsdk.core.impl;

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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;



import cn.yyxx.commsdk.base.entity.Function;
import cn.yyxx.commsdk.base.entity.ResultInfo;
import cn.yyxx.commsdk.base.entity.SdkChargeInfo;
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.utils.Logger;

import cn.yyxx.commsdk.core.common.CommonOperationManager;

import cn.yyxx.commsdk.core.ui.dialog.CommPayDialog;
import cn.yyxx.commsdk.core.utils.MMKVUtils;
import cn.yyxx.commsdk.core.utils.ResourceUtil;
import cn.yyxx.support.permission.IPermissionCallback;
import cn.yyxx.support.permission.PermissionKit;


/**
 * @author #Suyghur.
 * Created on 2021/12/25
 */


public final class SdkBridgeImpl {

    private Object mSdkProxy;
    public SdkBridgeImpl(Context context) {

    }

    private Object getSdkProxyManager(Context context){
        if (mSdkProxy == null) {
            mSdkProxy = SdkProxyManager.initSdkProxyManager(context);
        }
        return mSdkProxy;
    }

    public void attachBaseContext(Application application, Context context) {
        Logger.i("application   attachBaseContext ...");
        // 初始化Zap日志框架
        Logger.initZap(application);
        CommonOperationManager.getInstance().initMsaDeviceIds(application);

        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(application);
        }
        SdkProxyManager.call(Function.ATTACH_BASE_CONTEXT,  new Class[]{Application.class, Context.class}, new Object[]{application, context});


    }

    public void initApplication(Application application) {
        Logger.i("initApplication ...");
        MMKVUtils.getInstance().init(application);
        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(application);
        }
        SdkProxyManager.call(Function.INIT_APPLICATION,  new Class[]{Application.class}, new Object[]{application});

    }

    public void initAgreement(Activity activity,ICallback callback) {

        CommonOperationManager.getInstance().initAgreement(activity,callback);
    }
    public void initialize(Activity activity, ICallback callback) {


        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(activity);
        }

        SdkProxyManager.call(Function.INITIALIZE,  new Class[]{Activity.class, ICallback.class}, new Object[]{activity,callback});

        CommonOperationManager.getInstance().initialize(activity, new ICallback() {
            @Override
            public void onResult(int code, String result) {
                callback.onResult(code,result);
                if (code == ResultInfo.CODE_SUCCESS){
                    SdkProxyManager.call(Function.INITIALIZE_SUCCESS,  new Class[]{}, new Object[]{});
                }
            }
        });

    }

    public void setLogoutListener(Activity activity,ICallback callback){
        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(activity);
        }

        SdkProxyManager.call(Function.SET_LOGOUT_LISTENER,  new Class[]{Activity.class, ICallback.class}, new Object[]{activity,callback});

    }

    public void login(Activity activity , final ICallback callback) {
        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(activity);
        }

        SdkProxyManager.call(Function.LOGIN,  new Class[]{Activity.class,InitBean.class, ICallback.class}, new Object[]{activity,CommonOperationManager.initBean,callback});

    }

    public void logout() {
        SdkProxyManager.call(Function.LOGOUT,  new Class[]{}, new Object[]{});

    }


    public void getOrderInfo(Activity activity, SdkChargeInfo chargeInfo, ICallback callback) {

        String  sdk_params = (String) SdkProxyManager.callback(Function.GET_CHANNEL_ORDER_EXT,new Class[]{}, new Object[]{});
        chargeInfo.setSdk_params(sdk_params);
        ICallback iChargeCallBack = new ICallback() {
            @Override
            public void onResult(int code, String result) {
                if (code == ResultInfo.CODE_SUCCESS){
                    SdkOrderInfo sdkOrderInfo = new SdkOrderInfo();
                    sdkOrderInfo.initOrderInfo(chargeInfo,result);
                    if (sdkOrderInfo.getSdk_pay() == 1){
                        charge(activity,sdkOrderInfo,callback);
                    }else{
                        if (!TextUtils.isEmpty(sdkOrderInfo.getPay_web_url())){
                            new CommPayDialog(activity, ResourceUtil.getStyleId(activity, "hnyy_base_dialog"),sdkOrderInfo).show();
                        }
                    }
                }else{
                    callback.onResult(ResultInfo.CODE_FAIL,result);
                }
            }
        };
        CommonOperationManager.getInstance().getOrderInfo(activity,chargeInfo,iChargeCallBack);
    }



     public void charge(Activity activity, SdkOrderInfo sdkOrderInfo, ICallback callback) {


         SdkProxyManager.call(Function.CHARGE,  new Class[]{Activity.class,SdkOrderInfo.class,ICallback.class}, new Object[]{activity,sdkOrderInfo,callback});

     }

    public void uploadRoleInfo(Activity activity, String type,SdkRoleInfo roleInfo) {
        CommonOperationManager.getInstance().uploadRoleInfo(activity,type,roleInfo);
        SdkProxyManager.call(Function.UPLOAD_ROLE_INFO,  new Class[]{Activity.class,String.class,SdkRoleInfo.class}, new Object[]{activity,type,roleInfo});

    }



    public boolean hasExitView(Activity activity) {
         return (boolean)SdkProxyManager.callback(Function.HAS_EXIT_VIEW,new Class[]{Activity.class}, new Object[]{activity});
    }

    public void openExitView(Activity activity, final ICallback callback) {
        SdkProxyManager.call(Function.OPEN_EXIT_VIEW,new Class[]{Activity.class,ICallback.class}, new Object[]{activity,callback});
    }

    public void onStart(Activity activity) {

        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(activity);
        }

        SdkProxyManager.call(Function.ON_START, new Class[]{Activity.class}, new Object[]{activity});

    }

    public void onRestart(Activity activity) {

        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(activity);
        }

        SdkProxyManager.call(Function.ON_RESTART, new Class[]{Activity.class}, new Object[]{activity});

    }

    public void onResume(Activity activity) {

        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(activity);
        }

        SdkProxyManager.call(Function.ON_RESUME, new Class[]{Activity.class}, new Object[]{activity});

    }


    public void onPause(Activity activity) {
        
        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(activity);
        }
        
        SdkProxyManager.call(Function.ON_PAUSE, new Class[]{Activity.class}, new Object[]{activity});

    }

    public void onStop(Activity activity) {
        
        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(activity);
        }
        
        SdkProxyManager.call(Function.ON_STOP, new Class[]{Activity.class}, new Object[]{activity});

    }

    public void onDestroy(Activity activity) {
        
        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(activity);
        }
        SdkProxyManager.call(Function.ON_DESTROY, new Class[]{Activity.class}, new Object[]{activity});

    }

    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) {

        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(activity);
        }
        
        SdkProxyManager.call(Function.ON_ACTIVITY_RESULT,
                new Class[]{Activity.class, Integer.class, Integer.class, Intent.class},
                new Object[]{activity, requestCode, resultCode, intent});
        
    }

    public void onNewIntent(Activity activity, Intent intent) {

        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(activity);
        }
        
        SdkProxyManager.call(Function.ON_NEW_INTENT, new Class[]{Activity.class, Intent.class}, new Object[]{activity, intent});

    }

    public void onConfigurationChanged(Activity activity, Configuration newConfigs){
        

        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(activity);
        }
        
        
        SdkProxyManager.call(Function.ON_CONFIGURATION_CHANGED, new Class[]{Activity.class,Configuration.class}, new Object[]{activity,newConfigs});

        
    }
    public void onCreate(Activity activity, Bundle savedInstanceState){

        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(activity);
        }
        
        SdkProxyManager.call(Function.ON_CREATE, new Class[]{Activity.class, Bundle.class}, new Object[]{activity, savedInstanceState});

    }


    public void onWindowFocusChanged(Activity activity,boolean hasFocus){

        if (mSdkProxy == null) {
            mSdkProxy = getSdkProxyManager(activity);
        }
        
        SdkProxyManager.call(Function.ON_WINDOW_FOCUS_CHANGED, new Class[]{Activity.class,boolean.class}, new Object[]{activity,hasFocus});

    }


    public void requestPermission(Activity activity, List<String> permissions, ICallback callback) {
        Logger.i(" request  extend permission ...");
        PermissionKit.with().permission(permissions).request(activity, new IPermissionCallback() {
            @Override
            public void onGranted(List<String> list, boolean all) {
                Logger.i(" request  extend permission:onGranted");
                JSONObject jsonObject = permissionList2Json(list);
                if (jsonObject != null) {
                    callback.onResult(0, jsonObject.toString());
                } else {
                    callback.onResult(0, "");
                }
            }

            @Override
            public void onDenied(List<String> list, boolean never) {

                Logger.i(" request  extend permission :onDenied...");


                JSONObject jsonObject = permissionList2Json(list);
                if (jsonObject != null) {
                    callback.onResult(-1, jsonObject.toString());
                } else {
                    callback.onResult(-1, "");
                }
            }

            @Override
            public void onProxyFinish() {
                Logger.i(" request  extend permission :onProxyFinish...");

            }
        });

    }






    private JSONObject permissionList2Json(List<String> list) {
        try {
            JSONObject jsonObject = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            for (String permission : list) {
                jsonArray.put(permission);
            }
            jsonObject.put("permissions", jsonArray);
            return jsonObject;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

}
