package com.duben.microtribe.manager;

import android.text.TextUtils;

import com.duben.microtribe.MintsApplication;
import com.duben.microtribe.mvp.model.UserBean;

import net.grandcentrix.tray.AppPreferences;


/**
 * 描述：管理用户信息
 * 作者：孟崔广
 * 时间：2022/12/22 13:54
 */
public class UserManager {

    private static UserManager _inst;
    private AppPreferences ps;

    /**
     * 游客 or 登录用户
     */
    private static final String IS_TEMP_USER = "is_temp_user";
    /**
     * 用户ID
     */
    private static final String USER_ID = "userId";
    /**
     * 登陆状态ID
     */
    private static final String TOKEN_ID = "tokenId";
    /**
     * 手机号
     */
    private static final String MOBILE = "mobile";
    /**
     * 姓名
     */
    private static final String REAL_NAME = "realName";
    /**
     * 微信id
     */
    private static final String WX_OPENID = "wx_openid";
    /**
     * 头像
     */
    private static final String HEAD = "head";
    /**
     * vip是否有效
     */
    private static final String VIP_FLAG = "vipFlag";
    /**
     * vip是否永久
     */
    private static final String VIP_FOREVER = "VIP_FOREVER";
    private static final String VIP_DATE = "VIP_DATE";
    private static final String CODE_ID = "codeId";
    // 匹配用户 = true
    private static final String NEW_FLAG = "new_flag";

    public static UserManager getInstance() {
        if (_inst != null) {
            return _inst;
        } else {
            _inst = new UserManager();
            return _inst;
        }
    }

    private UserManager() {
        init();
        return;
    }

    private void init() {
        ps = new AppPreferences(MintsApplication.getContext());
    }

    /**
     * 保存用户信息
     */
    public void saveUserInfo(UserBean user) {
        if (ps == null) {
            return;
        }

        String token = user.getToken();

        if (token != null) {
            ps.put(TOKEN_ID, token);
        }

        if (user != null) {
            String uid = String.valueOf(user.getPk_id());
            String mobile = user.getMobile();
            String openid = user.getOpenid();
            if (!TextUtils.isEmpty(mobile) || !TextUtils.isEmpty(openid)) {
                ps.put(IS_TEMP_USER, uid);
            } else {
                ps.remove(IS_TEMP_USER);
            }

            if (user.getExpireTime() > 0) {
                ps.put(VIP_FLAG, true);
            } else {
                ps.put(VIP_FLAG, false);
            }

            ps.put(VIP_FOREVER, user.isForever());
            ps.put(REAL_NAME, user.getNickName());
            ps.put(HEAD, user.getHead());
            ps.put(USER_ID, uid);
            ps.put(MOBILE, user.getMobile());
            ps.put(VIP_DATE, user.getExpireTime());
            ps.put(WX_OPENID, openid);
            ps.put(CODE_ID, user.getIdcode());
            ps.put(NEW_FLAG, user.getActiviteFlag() == 1);
        }

    }

    /**
     * 单独获取用户id
     */
    public String getMobile() {
        if (ps == null) {
            return null;
        }
        return ps.getString(MOBILE, "");
    }

    public String getName() {
        if (ps == null) {
            return null;
        }
        return ps.getString(REAL_NAME, "");
    }

    public long getVipDate() {
        return ps.getLong(VIP_DATE, 0);
    }

    /**
     * 判断用户是否已登录
     *
     * @return true为已登录   false 为未登录
     */
    public boolean userIsLogin() {
        if (ps == null) {
            return false;
        }
//        return !TextUtils.isEmpty(ps.getString(TOKEN_ID));
        return !TextUtils.isEmpty(ps.getString(IS_TEMP_USER, ""));
    }

    /**
     * 单独获取用户id
     */
    public String getUserID() {
        if (ps == null) {
            return null;
        }
        return ps.getString(USER_ID, "");
    }

    public boolean getVipFlag() {
        if (ps == null) {
            return false;
        }
        return ps.getBoolean(VIP_FLAG, false);
    }

    public boolean getNewFlag() {
        if (ps == null) {
            return false;
        }
        return ps.getBoolean(NEW_FLAG, false);
    }

    /**
     * 获取用户登陆状态
     */
    public String getTokenID() {
        if (ps == null) {
            return null;
        }
        return ps.getString(TOKEN_ID, "");
    }

    public String getHead() {
        if (ps == null) {
            return null;
        }
        return ps.getString(HEAD, "");
    }

    public String getWxOpenid() {
        if (ps == null) {
            return null;
        }
        return ps.getString(WX_OPENID, "");
    }

    public String getCodeId() {
        if (ps == null) {
            return null;
        }
        return ps.getString(CODE_ID, "");
    }

    public boolean getVipForever() {
        if (ps == null) {
            return false;
        }
        return ps.getBoolean(VIP_FOREVER, false);
    }


    public void setVipFlag(boolean vipFlag) {
        if (ps == null) {
            return;
        }
        ps.put(VIP_FLAG, vipFlag);
    }

    public void userLogout() {
        if (ps != null) {
            ps.remove(USER_ID);
            ps.remove(MOBILE);
            ps.remove(TOKEN_ID);
            ps.remove(REAL_NAME);
            ps.remove(HEAD);
            ps.remove(IS_TEMP_USER);
            ps.remove(VIP_FLAG);
            ps.remove(VIP_FOREVER);
            ps.remove(WX_OPENID);
            ps.remove(CODE_ID);
            ps.remove(VIP_DATE);
            ps.remove(NEW_FLAG);
        }

        _inst = null;
    }


}
