package cn.yyxx.commsdk.platform;

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.os.NetworkOnMainThreadException;


import org.json.JSONObject;

import cn.yyxx.commsdk.base.entity.Function;


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.internal.ICallback;
import cn.yyxx.commsdk.base.entity.bean.InitBean;
import cn.yyxx.commsdk.base.internal.IRequestCallback;
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 PlatformSdk implements IFeature,ILifeCycle, IOrder, IApplication {

    private static PlatformSdk platformSdk;

    private Context context;

    private Object mSdkBridge = null;


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

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

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

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

    }

    @Override
    public void initializeSuccess() {
        checkSdkBridgeNull();
        SdkProxyOperationManager.call(Function.INITIALIZE_SUCCESS,  new Class[]{}, new Object[]{});

    }

    @Override
    public void setLogoutListener(Activity activity, ICallback callback) {
        checkSdkBridgeNull();
        SdkProxyOperationManager.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();

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

    }

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

    }


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


    public void openExitView(Activity activity, ICallback callback) {
        checkSdkBridgeNull();
        SdkProxyOperationManager.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();
        SdkProxyOperationManager.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 attachBaseContext(Application application, Context context) {
        checkSdkBridgeNull();
        SdkProxyOperationManager.call(Function.ATTACH_BASE_CONTEXT,  new Class[]{Application.class, Context.class}, new Object[]{application, context});

    }

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

    }

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

    }

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

    }

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

    }

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

    }

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

    }

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

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

    }

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

    }

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

    }

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

    }

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

    }

    @Override
    public void getOrderInfo(Activity activity, SdkChargeInfo sdkChargeInfo, IRequestCallback requestCallback) {

    }

    @Override
    public void charge(Activity activity, SdkOrderInfo sdkOrderInfo, ICallback callback) {
        SdkProxyOperationManager.call(Function.CHARGE, new Class[]{Activity.class, SdkOrderInfo.class,ICallback.class}, new Object[]{activity, sdkOrderInfo,callback});
     }

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