package com.wd.workoffice.util;


import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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_LOGIN_INFO = "_MES_LOGIN_INFO";
    private final String COOKIE = "_COOKIE";
    private final String IS_AGREE = "_IS_AGREE";
    private final String FIRST_LOGIN = "_FIRST_LOGIN";

    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);
    }

    /**
     * mes的用户信息
     * {
     * "userId": "91c7f1d7722d11ea8334fa163e1166bd",
     * "name": "吴同得",
     * "cardid": "91c7f1d7722d11ea8334fa163e1166bd",
     * "roleid": "",
     * "rolename": "",
     * "userCode": "wtd",
     * "departmentId": "1c37c889ffa211e98334fa163e1166bd",
     * "departmentName": "铝碳厂",
     * "mobile": "1"
     * },
     *
     * @param info
     */
    public void keepMesLoginInfo(String info) {
        sp.put(MES_LOGIN_INFO, info);
    }

    public String getMesLoginInfo() {
        return sp.getString(MES_LOGIN_INFO);
    }

    public void clearMesLoginInfo() {
        sp.put(MES_LOGIN_INFO, "");
    }

    /**
     * mes的用户id
     *
     * @param
     */

    public String getMesUid() {
        JSONObject mesInfo = JSON.parseObject(getMesLoginInfo());
        if (!TextUtils.isEmpty(getMesLoginInfo())) {
            return mesInfo.getString("userId");
        }
        return "";
    }

    public String getMesErpId() {
        JSONObject mesInfo = JSON.parseObject(getMesLoginInfo());
        if (!TextUtils.isEmpty(getMesLoginInfo())) {
            return mesInfo.getString("userCode");
        }
        return "";
    }

    /**
     * bat 用户信息
     *
     * @param info
     */
    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();
    }

    /**
     * 获取部门下单权限
     *
     * @return
     */
    public List<String> getOrderDeptPermission() {
        if (TextUtils.isEmpty(sp.getString(USER_INFO))) {
            return null;
        }
        UserInfoBean infoBean = JSON.parseObject(sp.getString(USER_INFO), UserInfoBean.class);
        if (infoBean == null || infoBean.getOfDept() == null) {
            return null;
        }
        return infoBean.getOfDept().getDeptPermission();
    }

    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 String getMesNickName() {
        if (TextUtils.isEmpty(sp.getString(MES_LOGIN_INFO))) {
            return "";
        }
        JSONObject infoBean = JSON.parseObject(sp.getString(MES_LOGIN_INFO));
        if (infoBean == null) {
            return "";
        }
        return infoBean.getString("name");
    }

    public String getMesDep() {
        if (TextUtils.isEmpty(sp.getString(MES_LOGIN_INFO))) {
            return "";
        }
        JSONObject infoBean = JSON.parseObject(sp.getString(MES_LOGIN_INFO));
        if (infoBean == null) {
            return "";
        }
        return infoBean.getString("departmentName");
    }

    public String getMesUserCode() {
        if (TextUtils.isEmpty(sp.getString(MES_LOGIN_INFO))) {
            return "";
        }
        JSONObject infoBean = JSON.parseObject(sp.getString(MES_LOGIN_INFO));
        if (infoBean == null) {
            return "";
        }
        return infoBean.getString("userCode");
    }

    public String getMesMobile() {
        if (TextUtils.isEmpty(sp.getString(MES_LOGIN_INFO))) {
            return "";
        }
        JSONObject infoBean = JSON.parseObject(sp.getString(MES_LOGIN_INFO));
        if (infoBean == null) {
            return "";
        }
        return infoBean.getString("mobile");
    }

    /**
     * keepFirstLogin
     *
     * @param login
     */
    public void keepFirstLogin(boolean login) {
        sp.put(FIRST_LOGIN, login);
    }

    public boolean getFirstLogin() {
        return sp.getBoolean(FIRST_LOGIN,true);
    }

    public void clearFirstLogin() {
        sp.put(FIRST_LOGIN, true);
    }


    public void clearAll() {
        clearLoginInfo();
        clearUserInfo();
        clearMesLogin();
        clearMesLoginInfo();
        clearCookie();
    }
}
