package com.gh.common.util;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;

import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.gamecenter.core.utils.CurrentActivityHolder;
import com.gh.gamecenter.common.constant.Constants;
import com.gh.gamecenter.login.utils.QuickLoginHelper;
import com.gh.gamecenter.login.view.LoginActivity;
import com.gh.gamecenter.common.utils.NetworkUtils;
import com.gh.gamecenter.core.utils.SPUtils;
import com.gh.gamecenter.login.user.UserManager;
import com.lightgame.utils.Utils;


/**
 * Created by khy on 28/06/17.
 */

public class CheckLoginUtils {

    public static void checkLogin(Context context, String entrance, OnLoginListener listener) {
        if (!isLogin()) {
            if (listener != null) Utils.toast(context, "需要登录");
            LogUtils.login("dialog", null, entrance);
            LogUtils.login("activity", null, entrance);

            if (SPUtils.getBoolean(Constants.SP_HAS_GET_PHONE_INFO) || NetworkUtils.isOpenMobileData(context)) {
                startQuickLogin(context, entrance);
            } else {
                // 有可能App未启动
                Bundle bundle = new Bundle();
                bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance);
                bundle.putString(EntranceConsts.KEY_TO, LoginActivity.class.getName());
                EntranceUtils.jumpActivity(context, bundle);
            }
        } else {
            if (listener != null) {
                listener.onLogin();
            }
        }
    }

    private static void startQuickLogin(Context context, String entrance) {
        // 需要确保传入的 context 不为 application
        if (!(context instanceof Activity)) {
            context = CurrentActivityHolder.getCurrentActivity();
        }

        if (context != null) {
            QuickLoginHelper.startLogin(context, entrance);
        }
    }

    public static void checkLogin(final Context context, Bundle nextToBundle, boolean isTriggerNextStep, String entrance, OnLoginListener listener) {
        if (!isLogin()) {
            if (listener != null) Utils.toast(context, "需要登录");
            LogUtils.login("dialog", null, entrance);
            LogUtils.login("activity", null, entrance);

            // 有可能App未启动
            Bundle bundle = new Bundle();
            bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance);
            bundle.putString(EntranceConsts.KEY_TO, LoginActivity.class.getName());
            EntranceUtils.jumpActivity(context, nextToBundle, bundle, (resultCode, data) -> {
                if (isTriggerNextStep && listener != null && isLogin()) {
                    listener.onLogin();
                }
            });
        } else {
            if (listener != null) {
                listener.onLogin();
            }
        }
    }

    public static boolean isLogin() {
        return !TextUtils.isEmpty(UserManager.getInstance().getToken());
    }

    public interface OnLoginListener {
        void onLogin();
    }
}
