package ink.wgink.service.user.util;

import ink.wgink.exceptions.base.SystemException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.interfaces.manager.ISystemConfigManager;
import ink.wgink.pojo.bos.LoginUser;
import ink.wgink.service.user.pojo.pos.UserPO;
import ink.wgink.util.ReflectUtil;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import java.util.Map;

/**
 * @ClassName: UserUtil
 * @Description: 用户工具
 * @Author: wanggeng
 * @Date: 2022/5/19 17:56
 * @Version: 1.0
 */
public class UserUtil {

    /**
     * 创建登录用户
     *
     * @param userPO
     * @return
     */
    public static LoginUser createLoginUser(UserPO userPO) {
        LoginUser loginUser = new LoginUser(userPO.getUserUsername(), userPO.getUserPassword(),
                userPO.getUserType() == 1 ? true : false,
                isAccountNonExpired(userPO),
                isCredentialsNonExpired(userPO),
                userPO.getUserState() == 0 ? true : false);
        loginUser.setUserId(userPO.getUserId());
        loginUser.setUserName(userPO.getUserName());
        loginUser.setUserPhone(userPO.getUserPhone());
        loginUser.setUserUKey(userPO.getUserUKey());
        loginUser.setLoginType(userPO.getLoginType());
        loginUser.setUserEmail(userPO.getUserEmail());
        loginUser.setUserAvatar(userPO.getUserAvatar());
        loginUser.setUserState(userPO.getUserState());
        loginUser.setUserType(userPO.getUserType());
        return loginUser;
    }

    /**
     * 账号是否没有过期
     *
     * @param userPO
     * @return
     */
    private static boolean isAccountNonExpired(UserPO userPO) {
        if (StringUtils.equals(userPO.getUserUsername(), ISystemConstant.ADMIN)) {
            return true;
        }
        if (!StringUtils.isBlank(userPO.getUserExpiredDate())) {
            // 判断用户是否超时失效
            DateTime expiredDateTime = DateTime.parse(userPO.getUserExpiredDate(), DateTimeFormat.forPattern(ISystemConstant.DATE_FORMATTER_YYYY_MM_DD_HH_MM_SS));
            DateTime nowDateTime = DateTime.now();
            if (expiredDateTime.isBefore(nowDateTime)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 密码是否没有过期
     *
     * @param userPO
     * @return
     */
    private static boolean isCredentialsNonExpired(UserPO userPO) {
        if (StringUtils.equals(userPO.getUserUsername(), ISystemConstant.ADMIN)) {
            return true;
        }
        try {
            ISystemConfigManager singleInstance = ReflectUtil.getSingleInstance("ink.wgink.login.base.manager.ConfigManager", ISystemConfigManager.class);
            Map<String, String> config = singleInstance.getConfig();
            String passwordValidity = config.get("passwordValidity");
            // 自定义密码有效期
            if (StringUtils.equals(passwordValidity, "custom")) {
                Object passwordValidityDays = config.get("passwordValidityDays");
                int validityDays = passwordValidityDays == null ? 0 : (Integer) passwordValidityDays;
                if (validityDays <= 0) {
                    return true;
                }
                DateTime passwordModifiedDateTime = DateTime.parse(userPO.getGmtPasswordModified(), DateTimeFormat.forPattern(ISystemConstant.DATE_FORMATTER_YYYY_MM_DD_HH_MM_SS));
                DateTime passwordExpiredDateTime = passwordModifiedDateTime.plusDays(validityDays);
                DateTime nowDateTime = DateTime.now();
                if (passwordExpiredDateTime.isBefore(nowDateTime)) {
                    return false;
                }
            }
            return true;
        } catch (ReflectUtil.ReflectException e) {
            throw new SystemException(e);
        }
    }

}
