package com.cooleshow.usercenter.helper;

import android.text.TextUtils;

import com.cooleshow.base.data.net.CommonParamsHelper;
import com.cooleshow.base.utils.SPUtils;
import com.cooleshow.base.utils.Utils;
import com.cooleshow.usercenter.R;
import com.cooleshow.usercenter.bean.UserInfo;
import com.cooleshow.usercenter.bean.UserLoginInfo;

/**
 * Author by pq, Date nton 2022/4/22.
 */
public class UserHelper {
    public static final String USER_TOKEN_KEY = "user_token";
    public static final String USER_ID = "user_id";
    public static final String USERNAME = "username";
    public static final String USER_IM_TOKEN = "imToken";
    public static final String USER_PHONE = "phone";
    public static final String USER_AVATAR = "avatar";
    public static final String TEACHER_CERT_STATUS = "teacherCert";
    public static final String TEACHER_LIVE_FLAG_STATUS = "teacherLiveFlag";
    public static final String PRIVACY_KEY = "privacy_key";

    /**
     * 获取用户是否同意app隐私协议
     */
    public static int getAppPrivacyMode() {
        //0是未同意 1是已同意
        return SPUtils.getInstance().getInt(PRIVACY_KEY, 0);
    }

    public static boolean isAgreePrivacy() {
        return getAppPrivacyMode() == 1;
    }

    /**
     * 设置用户已同意app隐私协议
     */
    public static void setAppPrivacyModeIsAgree() {
        ////0是未同意 1是已同意
        SPUtils.getInstance().put(PRIVACY_KEY, 1);
    }

    public static boolean isLogin() {
        return !TextUtils.isEmpty(getUserToken());
    }


    public static String buildToken(String tokenType, String token) {
        return tokenType + " " + token;
    }

    public static void handleLoginSuccessAction(UserLoginInfo data) {
        //tokentype
        String resultToken = buildToken(data.authentication.token_type, data.authentication.access_token);
        saveUserToken(resultToken);
        CommonParamsHelper.getInstance().addCommonHeaderParams(CommonParamsHelper.USER_TOKEN, resultToken);
    }

    public static String handleLoginSuccessActionNotSave(UserLoginInfo data) {
        //tokentype
        String resultToken = buildToken(data.authentication.token_type, data.authentication.access_token);
        CommonParamsHelper.getInstance().addCommonHeaderParams(CommonParamsHelper.USER_TOKEN, resultToken);
        return resultToken;
    }

    public static void saveUserToken(String token) {
        SPUtils.getInstance().put(USER_TOKEN_KEY, token);
    }

    public static String getUserToken() {
        return SPUtils.getInstance().getString(USER_TOKEN_KEY);
    }

    /**
     * 达人认证状态
     */
    public static void saveTeacherCertificationStatus(String status) {
        SPUtils.getInstance().put(TEACHER_CERT_STATUS + "_" + getUserId(), status);
    }

    /**
     * 达人认证状态
     */
    public static String getTeacherCertificationStatus() {
        return SPUtils.getInstance().getString(TEACHER_CERT_STATUS + "_" + getUserId());
    }

    /**
     * 老师直播权限状态
     */
    public static void saveTeacherOpenLiveFlag(int status) {
        SPUtils.getInstance().put(TEACHER_LIVE_FLAG_STATUS + "_" + getUserId(), status);
    }

    /**
     * 老师直播权限状态
     */
    public static int getTeacherLiveFlag() {
        return SPUtils.getInstance().getInt(TEACHER_LIVE_FLAG_STATUS + "_" + getUserId(), 0);
    }


    public static void saveUserInfo(UserInfo userInfo) {
        if (userInfo == null) {
            return;
        }
        //userId
        SPUtils.getInstance().put(USER_ID, userInfo.id);
        //昵称
        SPUtils.getInstance().put(USERNAME, userInfo.username);
        //imtoken
        SPUtils.getInstance().put(USER_IM_TOKEN, userInfo.imToken);
        //电话
        SPUtils.getInstance().put(USER_PHONE, userInfo.phone);
        //头像
        SPUtils.getInstance().put(USER_AVATAR, userInfo.avatar);

    }

    public static String getUserId() {
        return SPUtils.getInstance().getString(USER_ID);
    }

    public static String getUserName() {
        return SPUtils.getInstance().getString(USERNAME);
    }

    public static String getUserIMToken() {
        return SPUtils.getInstance().getString(USER_IM_TOKEN);
    }

    public static String getUserPhone() {
        return SPUtils.getInstance().getString(USER_PHONE);
    }

    public static String getUserAvatar() {
        return SPUtils.getInstance().getString(USER_AVATAR);
    }

    public static String getTeacherName(String name, int id) {
        if (!TextUtils.isEmpty(name)) {
            return name;
        }
        return Utils.getApp().getString(R.string.nickname_fill_str, String.valueOf(id));
    }

    public static String getStudentName(String name, int id) {
        if (!TextUtils.isEmpty(name)) {
            return name;
        }
        return Utils.getApp().getString(R.string.nickname_fill_str, String.valueOf(id));
    }


    public static String getStudentName(String name, String id) {
        if (!TextUtils.isEmpty(name)) {
            return name;
        }
        return Utils.getApp().getString(R.string.nickname_fill_str, id);
    }

    public static void clearUserInfo() {
        //userId
        SPUtils.getInstance().put(USER_ID, "");
        //token
        SPUtils.getInstance().put(USER_TOKEN_KEY, "");
        //昵称
        SPUtils.getInstance().put(USERNAME, "");
        //imtoken
        SPUtils.getInstance().put(USER_IM_TOKEN, "");
        //电话
        SPUtils.getInstance().put(USER_PHONE, "");
        //头像
        SPUtils.getInstance().put(USER_AVATAR, "");
    }
}
