package com.mints.goodmoney.manager;

import android.text.TextUtils;

import com.mints.goodmoney.MintsApplication;
import com.mints.goodmoney.mvp.model.UserBean;
import com.mints.goodmoney.utils.PreferencesService;


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

    private static UserManager _inst;
    private PreferencesService ps;

    /**
     * 游客 or 登录用户
     */
    private static final String IS_TEMP_USER = "is_temp_user";
    /**
     * 用户ID
     */
    private static final String USER_ID = "userId";
    /**
     * 用户CODE_ID
     */
    private static final String CODE_ID = "codeId";
    /**
     * 登陆状态ID
     */
    private static final String TOKEN_ID = "tokenId";
    /**
     * 手机号
     */
    private static final String MOBILE = "mobile";
    /**
     * 姓名
     */
    private static final String REAL_NAME = "realName";
    /**
     * 支付宝
     */
    private static final String ALIPAY_ACCOUNT = "alipay_account";
    /**
     * 用户金币
     */
    private static final String USER_GOLD = "user_gold";
    /**
     * 用户零钱
     */
    private static final String USER_CASH = "user_cash";
    /**
     * 是否老用户  true-老用户
     */
    private static final String USER_OLD = "user_old";
    /**
     * 猎豹游戏info
     */
    private static final String GAME_INFO = "game_info";
    /**
     * 广告隐藏标识
     */
    private static final String AD_SHOW_FLAG = "ad_show_flag";
    /**
     * 微信名称
     */
    private static final String WX_NAME = "wx_name";
    /**
     * 微信头像
     */
    private static final String WX_HEADER = "wx_header";
    /**
     * 微信id
     */
    private static final String WX_OPENID = "wx_openid";


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

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

    private void init() {
        ps = PreferencesService.getInstance(MintsApplication.getContext());
    }

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

        UserBean.ConsumerBean user = userinfo.getConsumer();
        String token = userinfo.getToken();

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

        if (user != null) {
            String mobile = user.getMobile();
            String openid = user.getOpenid();
            if (TextUtils.isEmpty(mobile) && TextUtils.isEmpty(openid)) {
                ps.removeByKey(IS_TEMP_USER);
            } else {
                ps.putString(IS_TEMP_USER, mobile + openid);
            }

            ps.putString(USER_ID, String.valueOf(user.getPk_id()));
            ps.putString(CODE_ID, user.getIdcode());
            ps.putString(MOBILE, mobile);
//            ps.putString(GAME_INFO, user.getGameInfo());
//            ps.putString(REAL_NAME, user.getReal_name());
            ps.putString(ALIPAY_ACCOUNT, user.getAlipay_account());
            ps.putString(USER_GOLD, String.valueOf(user.getCoin()));
            ps.putString(USER_CASH, String.format("%.2f", user.getSurplus()));
            ps.putBoolean(USER_OLD, !user.isFirstSignInApp());

            ps.putString(WX_NAME, user.getNickName());
            ps.putString(WX_HEADER, user.getHead());
            ps.putString(WX_OPENID, openid);
        }

    }

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

    /**
     * 判断用户是否已登录
     *
     * @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 String getTokenID() {
        if (ps == null) {
            return null;
        }
        return ps.getString(TOKEN_ID);
    }

    /**
     * 获取用户邀请码
     */
    public String getCodeID() {
        if (ps == null) {
            return null;
        }
        return ps.getString(CODE_ID);
    }

    /**
     * 获取真实姓名
     */
    public String getRealName() {
        if (ps == null) {
            return null;
        }
        return ps.getString(REAL_NAME);
    }

    /**
     * 设置真实姓名
     *
     * @param name
     */
    public void setRealName(String name) {
        if (ps == null) {
            return;
        }
        ps.putString(REAL_NAME, name);
    }

    /**
     * 是否老用户  true-老用户
     */
    public boolean getUserOld() {
        if (ps == null) {
            return false;
        }
        return ps.getBoolean(USER_OLD, false);
    }

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

    /**
     * 设置真实姓名
     *
     * @param userOld
     */
    public void setUserOld(boolean userOld) {
        if (ps == null) {
            return;
        }
        ps.putBoolean(USER_OLD, userOld);
    }

    /**
     * 获取支付宝账户
     */
    public String getAlipayAccount() {
        if (ps == null) {
            return null;
        }
        return ps.getString(ALIPAY_ACCOUNT);
    }

    /**
     * 设置真实姓名
     *
     * @param alipay
     */
    public void setAlipayAccount(String alipay) {
        if (ps == null) {
            return;
        }
        ps.putString(ALIPAY_ACCOUNT, alipay);
    }

    /**
     * 获取用户金币/积分
     */
    public String getUserGold() {
        if (ps == null) {
            return null;
        }
        return ps.getString(USER_GOLD);
    }

    /**
     * 设置用户金币/积分
     *
     * @param gold
     */
    public void setUserGold(String gold) {
        if (ps == null) {
            return;
        }
        ps.putString(USER_GOLD, gold);
    }

    /**
     * 获取用户零钱
     */
    public String getUsercash() {
        if (ps == null) {
            return null;
        }
        return ps.getString(USER_CASH);
    }

    public String getGameInfo() {
        if (ps == null) {
            return null;
        }
        return ps.getString(GAME_INFO);
    }

    public String getWxName() {
        if (ps == null) {
            return null;
        }
        return ps.getString(WX_NAME);
    }

    public String getWxHeader() {
        if (ps == null) {
            return null;
        }
        return ps.getString(WX_HEADER);
    }

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

    /**
     * 设置用户零钱
     *
     * @param cash
     */
    public void setUsercash(String cash) {
        if (ps == null) {
            return;
        }
        ps.putString(USER_CASH, cash);
    }

    public void setGameInfo(String gameInfo) {
        if (ps == null) {
            return;
        }
        ps.putString(GAME_INFO, gameInfo);
    }

    public void setWxName(String wxName) {
        if (ps == null) {
            return;
        }
        ps.putString(WX_NAME, wxName);
    }

    public void setWxHeader(String wxHeader) {
        if (ps == null) {
            return;
        }
        ps.putString(WX_HEADER, wxHeader);
    }

    /**
     * mobile
     *
     * @param mobile
     */
    public void setMobile(String mobile) {
        if (ps == null) {
            return;
        }
        ps.putString(MOBILE, mobile);
    }

    public void setAdShowFlag(boolean adShowFlag) {
        if (ps == null) {
            return;
        }
        ps.putBoolean(AD_SHOW_FLAG, adShowFlag);
    }

    public void userLogout() {
        if (ps == null) {
            return;
        }
        ps.removeByKey(USER_ID);
//        ps.removeByKey(CODE_ID);
        ps.removeByKey(TOKEN_ID);
        ps.removeByKey(REAL_NAME);
        ps.removeByKey(IS_TEMP_USER);
        ps.removeByKey(ALIPAY_ACCOUNT);
        ps.removeByKey(USER_GOLD);
        ps.removeByKey(USER_CASH);
//        ps.removeByKey(GAME_INFO);
        ps.removeByKey(USER_OLD);

        ps.removeByKey(WX_NAME);
        ps.removeByKey(WX_HEADER);
        _inst = null;
    }


}
