package com.gh.gamecenter.message;

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

import com.therouter.TheRouter;
import com.gh.gamecenter.common.constant.Constants;
import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.gamecenter.common.constant.RouteConsts;
import com.gh.gamecenter.common.utils.NetworkUtils;
import com.gh.gamecenter.core.utils.CurrentActivityHolder;
import com.gh.gamecenter.core.utils.SPUtils;
import com.gh.gamecenter.login.user.UserManager;
import com.gh.gamecenter.login.utils.QuickLoginHelper;
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, "需要登录");

            if (SPUtils.getBoolean(Constants.SP_HAS_GET_PHONE_INFO) || NetworkUtils.isOpenMobileData(context)) {
                startQuickLogin(context, entrance);
            } else {
                TheRouter.build(RouteConsts.activity.loginActivity)
                        .withString(EntranceConsts.KEY_ENTRANCE, entrance)
                        .navigation();
            }
        } 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 boolean isLogin() {
        return !TextUtils.isEmpty(UserManager.getInstance().getToken());
    }

    public interface OnLoginListener {
        void onLogin();
    }
}
