package com.wd.workoffice.util;


import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.bean.TokenBean;
import com.wd.workoffice.bean.UserInfoBean;

import java.util.List;

import flexible.xd.android_base.utils.SPUtils;

/**
 * Created by Flexible on 2017/1/9 0009.
 */

public class UserKeeper {
    private volatile static UserKeeper keeper;
    private SPUtils sp = SPUtils.getInstance("UserKeeper");
    private final String LOGIN_INFO = "_LOGIN_INFO";
    private final String USER_INFO = "_USER_INFO";

    private UserKeeper() {
    }

    public static UserKeeper getInstance() {
        if (keeper == null) {
            synchronized (UserKeeper.class) {
                if (keeper == null) {
                    keeper = new UserKeeper();
                }
            }
        }
        return keeper;
    }

    public void keepLoginInfo(String info) {
        sp.put(LOGIN_INFO, info);
    }

    public String getLoginInfo() {
        return sp.getString(LOGIN_INFO);
    }

    public void clearLoginInfo() {
        sp.put(LOGIN_INFO, "");
    }

    public String getToken() {
        if (TextUtils.isEmpty(sp.getString(LOGIN_INFO))) {
            return "";
        }
        TokenBean tokenBean = JSON.parseObject(sp.getString(LOGIN_INFO), TokenBean.class);
        if (tokenBean == null || TextUtils.isEmpty(tokenBean.getAccess_token())) {
            return "";
        }
        return tokenBean.getAccess_token();
    }
    public String getRefreshToken() {
        if (TextUtils.isEmpty(sp.getString(LOGIN_INFO))) {
            return "";
        }
        TokenBean tokenBean = JSON.parseObject(sp.getString(LOGIN_INFO), TokenBean.class);
        if (tokenBean == null || TextUtils.isEmpty(tokenBean.getRefresh_token())) {
            return "";
        }
        return tokenBean.getRefresh_token();
    }
    public String getUserId() {
        if (TextUtils.isEmpty(sp.getString(LOGIN_INFO))) {
            return "";
        }
        TokenBean tokenBean = JSON.parseObject(sp.getString(LOGIN_INFO), TokenBean.class);
        if (tokenBean == null || tokenBean.getUserId() == 0) {
            return "";
        }
        return tokenBean.getUserId() + "";
    }

    public void keepUserInfo(String info) {
        sp.put(USER_INFO, info);
    }

    public String getUserInfo() {
        return sp.getString(USER_INFO);
    }

    public void clearUserInfo() {
        sp.put(USER_INFO, "");
    }

    public String getUserDepId() {
        if (TextUtils.isEmpty(sp.getString(USER_INFO))) {
            return "";
        }
        UserInfoBean infoBean = JSON.parseObject(sp.getString(USER_INFO), UserInfoBean.class);
        if (infoBean == null || infoBean.getOfDept() == null) {
            return "";
        }
        return infoBean.getOfDept().getId() + "";
    }

    public List<String> getUserPermission() {
        if (TextUtils.isEmpty(sp.getString(USER_INFO))) {
            return null;
        }
        UserInfoBean infoBean = JSON.parseObject(sp.getString(USER_INFO), UserInfoBean.class);
        if (infoBean == null || infoBean.getPermissionList() == null) {
            return null;
        }
        return infoBean.getPermissionList();
    }

    public String getUserErpId() {
        if (TextUtils.isEmpty(sp.getString(USER_INFO))) {
            return "";
        }
        UserInfoBean infoBean = JSON.parseObject(sp.getString(USER_INFO), UserInfoBean.class);
        if (infoBean == null || infoBean.getErpId() == null) {
            return "";
        }
        return infoBean.getErpId();
    }

    public String getUserNickName() {
        if (TextUtils.isEmpty(sp.getString(USER_INFO))) {
            return "";
        }
        UserInfoBean infoBean = JSON.parseObject(sp.getString(USER_INFO), UserInfoBean.class);
        if (infoBean == null || infoBean.getNickName() == null) {
            return "";
        }
        return infoBean.getNickName();
    }
    public void clearAll() {
        clearLoginInfo();
        clearUserInfo();
//        clearAccount();
    }
}
