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 on 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 boolean isLogin() {
        return !TextUtils.isEmpty(getUserToken());
    }


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

    public static void handleLoginSuccessAction(UserLoginInfo data) {
        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) {
        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 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);
    }
}
