package cn.dankal.basiclib;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import cn.dankal.basiclib.common.qiniu.QiniuConfigResponse;
import cn.dankal.basiclib.model.LoginModel;
import cn.dankal.basiclib.model.TokenModel;
import cn.dankal.basiclib.model.UserModel;
import cn.dankal.basiclib.util.PreferenceUtil;


/**
 * @author vane
 */
public class DKUserManager {

    private static Context mContext = DankalApplication.getContext();
    private static SharedPreferences mSpUserInfo;
    private static SharedPreferences mSpToken;
    private static final String PreferenceUserInfo = "userinfo";
    private static final String PreferenceToken = "token";
    private static UserModel userInfo;
    private static TokenModel userToken;

    private static SharedPreferences mQiniuInfo;
    private static final String PreferenceQiniuToken = "qiniu";
    private static QiniuConfigResponse qiniuHeader;

    static {
        mSpUserInfo = mContext.getSharedPreferences(PreferenceUserInfo, Context.MODE_PRIVATE);
        mSpToken = mContext.getSharedPreferences(PreferenceToken, Context.MODE_PRIVATE);
    }


    /**
     * 最初mUserInfo各属性内容为空，
     * 如登录后可更新当前的UserInfo和本地的缓存
     */
    public static void saveUserInfo(LoginModel userResponseBody) {
        UserModel userInfo = userResponseBody.getInfo();
        TokenModel token = userResponseBody.getMeta();
        if (userInfo != null) {
            PreferenceUtil.updateBean(mSpUserInfo, getUserInfo(), userInfo);
        }
        if (token != null) {
            PreferenceUtil.updateBean(mSpToken, getUserToken(), token);
        }
    }

    public static void updateUserInfo(UserModel userInfo) {
        if (userInfo != null) {
            PreferenceUtil.updateBean(mSpUserInfo, getUserInfo(), userInfo);
        }
    }

    public static void updateUserToken(TokenModel token) {
        if (token != null) {
            PreferenceUtil.updateBean(mSpToken, getUserToken(), token);
        }
    }

    /**
     * 如果从本地缓存中获取对象为空则实例化一个空对象
     * 判断是否登录全程通过user_id是不是为0来判断而不是通过
     * mUserInfo是否等于null，防止UserManager.getUserInfo出现空指针
     */
    private static void readUserInfo() {
        userInfo = (UserModel) PreferenceUtil.getBeanValue(mSpUserInfo, UserModel.class);
        if (userInfo == null) {
            userInfo = new UserModel();
        }
    }

    private static void readUserToken() {
        userToken = (TokenModel) PreferenceUtil.getBeanValue(mSpToken,
                TokenModel.class);
        if (userToken == null) {
            userToken = new TokenModel();
        }
    }

    /**
     * 清空缓存时调用
     */
    public static void resetUserInfo() {
        SharedPreferences.Editor editor = mSpUserInfo.edit();
        editor.clear();
        editor.apply();
        userInfo = new UserModel();
    }

    public static void resetToken() {
        SharedPreferences.Editor editor = mSpToken.edit();
        editor.clear();
        editor.apply();
        userToken = new TokenModel();
    }

    /**
     * 判断已经登录
     */
    public static boolean isLogined() {
        return !TextUtils.isEmpty(getUserToken().getAccess_token());
    }

    public static TokenModel getUserToken() {
        if (userToken == null) {
            readUserToken();
        }
        return userToken;
    }

    public static UserModel getUserInfo() {
        if (userInfo == null) {
            readUserInfo();
        }
        return userInfo;
    }


    private static void readQiniuheader() {
        qiniuHeader = (QiniuConfigResponse) PreferenceUtil.getBeanValue(mQiniuInfo, QiniuConfigResponse.class);

        if (qiniuHeader == null) {
            qiniuHeader = new QiniuConfigResponse();
            qiniuHeader.setToken("https://cdn.gdzhongchao.com/");
        }
    }

    public static QiniuConfigResponse getQiniuHeader() {
        if (qiniuHeader == null) {
            readQiniuheader();
        }
        return qiniuHeader;
    }

    public static void updateQiniuHeader(QiniuConfigResponse response) {
        if (response != null) {
            PreferenceUtil.updateBean(mQiniuInfo, getQiniuHeader(), response);
        }
    }
}
