package com.yyrh.factory;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.yyrh.constant.Constants;
import com.yyrh.constant.RoleInfo;
import com.yyrh.core.entity.ResultInfo;
import com.yyrh.core.entity.SdkBackLoginInfo;
import com.yyrh.core.impl.HybridActivity;
import com.yyrh.core.impl.floatball.FloatCenterServiceManager;
import com.yyrh.core.impl.login.LoginActivity;
import com.yyrh.core.internal.IImplCallback;
import com.yyrh.core.internal.IRequestCallback;
import com.yyrh.core.network.SdkRequest;
import com.yyrh.sdk.SDKEntry;
import com.yyrh.sdk.YyrhSdkManager;
import com.yyrh.sdk.callback.RealNameMsgCallback;
import com.yyrh.ui.dialog.TipsDialog;
import com.yythird.base.ChannelSdkCallback;

import org.json.JSONObject;

import java.util.HashMap;

public class YyrhSdkOperation implements ISdkOperation {

    private TipsDialog exitDialog = null;

    public YyrhSdkOperation() {
    }

    @Override
    public void login(final Activity activity) {
        LoginActivity.start(activity, true, SdkManager.defaultManager().isLandscape, new IImplCallback() {
            @Override
            public void onResult(int code, String result) {
                if (code == 1) {
                    // 拉起实名页面
                    if (!SdkBackLoginInfo.getInstance().isRealName && !TextUtils.isEmpty(SdkBackLoginInfo.getInstance().realNameUrl)) {
                        HybridActivity.start(activity, SdkBackLoginInfo.getInstance().realNameUrl);
                    }
                    SdkManager.defaultManager().sdkCallback.onLoginSuccess(result);
                } else {
                    SdkManager.defaultManager().sdkCallback.onLoginError(result);
                }
            }
        });
    }

    @Override
    public void uploadRoleInfo(String type, Activity activity, RoleInfo roleInfo) {
        String reportTime = "";
        switch (type) {
            case Constants.UPLOADTYPE_CREATEROLE:
                reportTime = "createRole";
                break;
            case Constants.UPLOADTYPE_ENTERGAME:
                reportTime = "enterGame";
                break;
            case Constants.UPLOADTYPE_LEVELUP:
                reportTime = "roleUpgrade";
                break;
        }
        SdkRequest.submitRoleInfo(activity, reportTime, roleInfo, new IRequestCallback() {
            @Override
            public void onResponse(ResultInfo resultInfo) {

            }
        });
    }

    @Override
    public void setSDKLogoutListener(Activity activity, ChannelSdkCallback sdkCallback) {

    }


    @Override
    public void pay(final Activity activity, HashMap<String, Object> map) {
        SdkRequest.createOrder(activity, map, new IRequestCallback() {
            @Override
            public void onResponse(ResultInfo resultInfo) {
                if (resultInfo.code == 1) {
                    try {
                        JSONObject jsonObject = new JSONObject(resultInfo.data);
                        String url = jsonObject.getString("pay_web_url");
                        HybridActivity.start(activity, url);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    public void exit(Activity activity) {
        if (exitDialog != null) {
            if (exitDialog.isShowing()) {
                exitDialog.dismiss();
            }
            exitDialog = null;
        }
        exitDialog = new TipsDialog(activity, true);
        exitDialog.getTextView().setText("您确认立即退出游戏吗？");
        exitDialog.getLeftButton().setText("下次再见");
        exitDialog.getLeftButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SdkManager.defaultManager().sdkCallback.onExiGameCallback(true, "退出游戏");
                exitDialog.dismiss();
            }
        });
        exitDialog.getRightButton().setText("再玩一下");
        exitDialog.getRightButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SdkManager.defaultManager().sdkCallback.onExiGameCallback(false, "继续游戏");
                exitDialog.dismiss();
            }
        });
        exitDialog.show();
    }

    @Override
    public void logOut() {
        FloatCenterServiceManager.getInstance().detach();
        SdkBackLoginInfo.getInstance().reset();
        SdkManager.defaultManager().sdkCallback.onLogOutCallback("用户退出");
    }

    @Override
    public void getRealNameInfo(Activity activity, RealNameMsgCallback realNameMsgCallback) {
        YyrhSdkManager.defaultManager().yyrhGetRealNameInfo(activity, realNameMsgCallback);
    }

    @Override
    public void reportAdEvent(String event, JSONObject json) {
        SDKEntry.getSdkInstance().adSdkCallback.onReportAdEvent(event, json);
    }

    @Override
    public void sdkOnStart(Activity activity) {

    }

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

    }

    @Override
    public void sdkOnRestart(Activity activity) {

    }

    @Override
    public void sdkOnResume(Activity activity) {
        FloatCenterServiceManager.getInstance().attach();
    }


    @Override
    public void sdkOnPause(Activity activity) {
        FloatCenterServiceManager.getInstance().detach();
    }


    @Override
    public void sdkOnStop(Activity activity) {

    }

    @Override
    public void sdkOnDestroy(Activity activity) {
        FloatCenterServiceManager.getInstance().release();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfigs) {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    }

    @Override
    public void sdkOnNewIntent(Activity acitivty, Intent intent) {

    }

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

    }

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

    }
}
