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 final String MES_LOGIN = "_MES_LOGIN";
    private final String MES_UID = "_MES_UID";
    private final String COOKIE = "_COOKIE";
    private final String IS_AGREE = "_IS_AGREE";

    private UserKeeper() {
    }

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

    /**
     * 同意协议
     *
     * @param info
     */
    public void keepIsAgree(Boolean info) {
        sp.put(IS_AGREE, info);
    }

    public Boolean getIsAgree() {
        return sp.getBoolean(IS_AGREE);
    }

    public void clearIsAgree() {
        sp.put(IS_AGREE, false);
    }

    /**
     * mescookie
     *
     * @param info
     */
    public void keepCookie(String info) {
        sp.put(COOKIE, info);
    }

    public String getCookie() {
        return sp.getString(COOKIE);
    }

    public void clearCookie() {
        sp.put(COOKIE, "");
    }

    /**
     * mes登录状态
     *
     * @param info
     */
    public void keepMesLogin(Boolean info) {
        sp.put(MES_LOGIN, info);
    }

    public Boolean getMesLogin() {
        return sp.getBoolean(MES_LOGIN);
    }

    public void clearMesLogin() {
        sp.put(MES_LOGIN, false);
    }

    public void keepMesUid(String info) {
        sp.put(MES_UID, info);
    }

    public String getMesUid() {
        return sp.getString(MES_UID);
    }

    public void clearMesUid() {
        sp.put(MES_UID, "");
    }

    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 String getUserDepRuler() {
        if (TextUtils.isEmpty(sp.getString(USER_INFO))) {
            return "";
        }
        UserInfoBean infoBean = JSON.parseObject(sp.getString(USER_INFO), UserInfoBean.class);
        if (infoBean == null ) {
            return "";
        }
        return infoBean.getOfDeptRuler();
    }

    public String getUserDepName() {
        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().getName();
    }

    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();
        clearMesLogin();
        clearMesUid();
    }
}
