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

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

import cn.yyxx.commsdk.base.entity.Function;
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.IMsaDeviceCallback;
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;



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

    private static ChannelSdk platformSdk;

    private Context context;

    private Object mSdkBridge = null;


    public ChannelSdk(Context context){
        this.context = context;
    }

    public static ChannelSdk getInstance(Context context){
        if (platformSdk == null) {
            synchronized (ChannelSdk.class) {
                if (platformSdk == null) {
                    platformSdk = new ChannelSdk(context);
                }
            }
        }
        return platformSdk;
    }

    private Object getSdkProxyOperationManager(Context context) {
        if (null == mSdkBridge) {
            mSdkBridge = SdkProxyChannelManager.initSdkProxyChannelManager(context);
        }
        return mSdkBridge;
    }

    private void checkSdkBridgeNull(){
        if (null == mSdkBridge) {
            mSdkBridge = SdkProxyChannelManager.initSdkProxyChannelManager(context);
        }
    }
    @Override
    public void initialize(Activity activity, ICallback callback) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.INITIALIZE, new Class[]{Activity.class,ICallback.class}, new Object[]{activity, callback});

    }

    @Override
    public void initializeSuccess(Activity activity, int loginStatus) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.INITIALIZE_SUCCESS,  new Class[]{Activity.class,int.class}, new Object[]{activity,loginStatus});

    }


    @Override
    public void setLogoutListener(Activity activity, ICallback callback) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.SET_LOGOUT_LISTENER,  new Class[]{Activity.class, ICallback.class}, new Object[]{activity,callback});

    }

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

        checkSdkBridgeNull();

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

    }

    @Override
    public void logout() {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.LOGOUT, new Class[]{}, new Object[]{});

    }


    @Override
    public boolean hasExitView(Activity activity) {
        checkSdkBridgeNull();
        return (Boolean) SdkProxyChannelManager.callback(Function.HAS_EXIT_VIEW, new Class[]{Activity.class}, new Object[]{activity});
    }


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

    }

    @Override
    public void uploadRoleInfo(Activity activity, String type, SdkRoleInfo roleInfo) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.UPLOAD_ROLE_INFO, new Class[]{Activity.class, String.class,SdkRoleInfo.class}, new Object[]{activity,type,roleInfo});

    }

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

    }

    @Override
    public void initMsaDeviceIds(Context context, IMsaDeviceCallback iMsaDeviceCallback) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.INIT_MSA_DEVICE_IDS, new Class[]{Context.class, IMsaDeviceCallback.class}, new Object[]{context,iMsaDeviceCallback});

    }

    @Override
    public String getChannelOcfg(Activity activity) {

        checkSdkBridgeNull();

        return (String) SdkProxyChannelManager.callback(Function.GET_CHANNEL_O_CFG, new Class[]{Activity.class}, new Object[]{activity});

    }


    @Override
    public void attachBaseContext(Application application, Context context) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.ATTACH_BASE_CONTEXT,  new Class[]{Application.class, Context.class}, new Object[]{application, context});

    }

    @Override
    public void initApplication(Application application) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.INIT_APPLICATION,  new Class[]{Application.class}, new Object[]{application});

    }

    @Override
    public void onStart(Activity activity) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.ON_START, new Class[]{Activity.class}, new Object[]{activity});

    }

    @Override
    public void onResume(Activity activity) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.ON_RESUME, new Class[]{Activity.class}, new Object[]{activity});

    }

    @Override
    public void onRestart(Activity activity) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.ON_RESTART, new Class[]{Activity.class}, new Object[]{activity});

    }

    @Override
    public void onPause(Activity activity) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.ON_PAUSE, new Class[]{Activity.class}, new Object[]{activity});

    }

    @Override
    public void onStop(Activity activity) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.ON_STOP, new Class[]{Activity.class}, new Object[]{activity});

    }

    @Override
    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.ON_ACTIVITY_RESULT,
                new Class[]{Activity.class, int.class, int.class, Intent.class},
                new Object[]{activity, requestCode, resultCode, data});
        
    }

    @Override
    public void onNewIntent(Activity activity, Intent intent) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.ON_NEW_INTENT, new Class[]{Activity.class, Intent.class}, new Object[]{activity, intent});

    }

    @Override
    public void onConfigurationChanged(Activity activity, Configuration newConfigs) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.ON_CONFIGURATION_CHANGED, new Class[]{Activity.class,Configuration.class}, new Object[]{activity,newConfigs});

    }

    @Override
    public void onWindowFocusChanged(Activity activity, boolean hasFocus) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.ON_WINDOW_FOCUS_CHANGED, new Class[]{Activity.class,boolean.class}, new Object[]{activity,hasFocus});

    }

    @Override
    public void onDestroy(Activity activity) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.ON_DESTROY, new Class[]{Activity.class}, new Object[]{activity});

    }

    @Override
    public void onCreate(Activity activity, Bundle savedInstanceState) {
        checkSdkBridgeNull();
        SdkProxyChannelManager.call(Function.ON_CREATE, new Class[]{Activity.class, Bundle.class}, new Object[]{activity, savedInstanceState});

    }

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

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

    }

    @Override
    public String getChannelOrderExt() {
        return (String) SdkProxyChannelManager.callback(Function.GET_CHANNEL_ORDER_EXT, new Class[]{},new Object[]{});
    }
}
