package cn.yyxx.commsdk.platform.hnyy.impl;

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

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

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.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;
import cn.yyxx.commsdk.core.entity.SdkLoginInfo;
import cn.yyxx.commsdk.core.utils.ResourceUtil;
import cn.yyxx.commsdk.platform.PlatformSdk;
import cn.yyxx.commsdk.platform.hnyy.HNYYLoginActivity;
import cn.yyxx.commsdk.platform.hnyy.dialog.CustomExitGameDialog;
import cn.yyxx.commsdk.platform.hnyy.dialog.HnPayDialog;
import cn.yyxx.commsdk.platform.hnyy.floating.FloatViewManager;

public class SdkPlatformImpl implements IFeature, ILifeCycle, IOrder, IApplication {
    public static InitBean initConfig = null ;

    private static SdkPlatformImpl manager;


    private  ICallback logoutCallback;

    private Context context;

    private static Activity activity;
    public SdkPlatformImpl(Context context){
        this.context = context;
    }


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

    }

    @Override
    public void initApplication(Application application) {

    }

    @Override
    public void initialize(Activity activity, ICallback callback) {
        this.activity = activity;
    }

    @Override
    public void initializeSuccess() {
    }

    @Override
    public void setLogoutListener(Activity activity, ICallback callback) {
        logoutCallback = callback;
    }

    @Override
    public void login(final Activity activity, InitBean initBean, final ICallback callback) {
        initConfig  = initBean;
        HNYYLoginActivity.start(activity,callback);
    }

    @Override
    public void logout() {
        SdkLoginInfo.getInstance().reset();
        hideFloat();
    }



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

    @Override
    public void openExitView(final Activity activity, final ICallback callback) {
        if (hasExitView(activity)){

            CustomExitGameDialog.Builder builder = new CustomExitGameDialog.Builder(activity);
            builder.setPositiveButton(new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    callback.onResult(ResultInfo.CODE_SUCCESS,activity.getString(ResourceUtil.getStringId(activity,"hnyy_exit_game")));
                    dialog.dismiss();
                }
            });
            builder.setnegativeButton(new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    callback.onResult(ResultInfo.CODE_FAIL,activity.getString(ResourceUtil.getStringId(activity,"hnyy_continue_game")));

                    dialog.dismiss();
                }
            });

            builder.Create().show();
        }else{
            callback.onResult(ResultInfo.CODE_SUCCESS,activity.getString(ResourceUtil.getStringId(activity,"hnyy_open_cp_exit_view")));
        }
    }

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

    }

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

    }


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

    @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) {
        releaseFloat();
    }

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

    }


    public void showFloat(){
        FloatViewManager.getInstance(activity).showFloat();
    }
    public void hideFloat(){
        FloatViewManager.getInstance(activity).hideFloat();
    }
    public void releaseFloat(){
        FloatViewManager.getInstance(activity).releaseFloat();
    }

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

    }

    @Override
    public void charge(Activity activity, SdkOrderInfo sdkOrderInfo, ICallback callback) {
        if (!TextUtils.isEmpty(sdkOrderInfo.getPay_web_url())){
            new HnPayDialog(activity, ResourceUtil.getStyleId(activity, "hnyy_base_dialog"),sdkOrderInfo).show();

        }

    }

    @Override
    public String getChannelOrderExt() {
        return "";
    }
}
