package ink.wgink.register.base.service.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.interfaces.expand.register.IRegisterHandlerService;
import ink.wgink.interfaces.expand.register.IRegisterWithExpandInfoHandlerService;
import ink.wgink.interfaces.manager.ISystemConfigManager;
import ink.wgink.register.base.pojo.vos.RegisterDefaultVO;
import ink.wgink.register.base.pojo.vos.RegisterDefaultWithExpandInfoVO;
import ink.wgink.register.base.pojo.vos.RegisterPhoneVO;
import ink.wgink.register.base.pojo.vos.RegisterPhoneWithExpandInfoVO;
import ink.wgink.register.base.service.IRegisterService;
import ink.wgink.service.user.enums.UserStateEnum;
import ink.wgink.service.user.enums.UserTypeEnum;
import ink.wgink.service.user.pojo.vos.UserVO;
import ink.wgink.service.user.service.IUserService;
import ink.wgink.util.ReflectUtil;
import ink.wgink.util.RegexUtil;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.string.WStringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: RegisterServiceImpl
 * @Description: 注册
 * @Author: wanggeng
 * @Date: 2021/5/1 10:33 上午
 * @Version: 1.0
 */
@Service
public class RegisterServiceImpl extends DefaultBaseService implements IRegisterService {

    private static final Logger LOG = LoggerFactory.getLogger(RegisterServiceImpl.class);
    @Autowired(required = false)
    private IRegisterHandlerService registerHandlerService;
    @Autowired(required = false)
    private IRegisterWithExpandInfoHandlerService registerWithExpandInfoHandlerService;
    @Autowired
    private IUserService userService;
    private ISystemConfigManager systemConfigManager;

    @PostConstruct
    public void init() {
        try {
            systemConfigManager = ReflectUtil.getSingleInstance("ink.wgink.login.base.manager.ConfigManager", ISystemConfigManager.class);
        } catch (ReflectUtil.ReflectException e) {
            e.printStackTrace();
            LOG.error(e.getMessage());
        }
    }

    @Override
    public void registerDefault(RegisterDefaultVO registerDefaultVO, Map<String, Object> queryParams) throws Exception {
        register(registerDefaultVO.getUsername(), registerDefaultVO.getPassword(), queryParams);
    }

    @Override
    public void registerPhone(RegisterPhoneVO registerPhoneVO, Map<String, Object> queryParams) throws Exception {
        // 生成8位随机密码
        register(registerPhoneVO.getPhone(), WStringUtil.randomSubStr(UUIDUtil.get32UUID(), 8), queryParams);
    }

    @Override
    public void registerDefaultWithExpandInfo(RegisterDefaultWithExpandInfoVO registerDefaultWithExpandInfoVO, Map<String, Object> queryParams) throws Exception {
        registerWithExpandInfo(registerDefaultWithExpandInfoVO.getUsername(), registerDefaultWithExpandInfoVO.getPassword(), registerDefaultWithExpandInfoVO.getExpandInfo(), queryParams);
    }

    @Override
    public void registerPhoneWithExpandInfo(RegisterPhoneWithExpandInfoVO registerPhoneWithExpandInfoVO, Map<String, Object> queryParams) throws Exception {
        registerWithExpandInfo(registerPhoneWithExpandInfoVO.getPhone(), WStringUtil.randomSubStr(UUIDUtil.get32UUID(), 8), registerPhoneWithExpandInfoVO.getExpandInfo(), queryParams);
    }

    private void register(String username, String password, Map<String, Object> queryParams) throws Exception {
        UserVO userVO = new UserVO();
        userVO.setUserUsername(username);
        userVO.setUserPassword(password);
        userVO.setUserName(username);
        if (RegexUtil.isPhone(username)) {
            userVO.setUserPhone(username);
        }
        if (isRegisterUserExamine()) {
            userVO.setUserState(UserStateEnum.UN_EXAMINE.getValue());
        } else {
            userVO.setUserState(UserStateEnum.NORMAL.getValue());
        }
        userVO.setUserType(UserTypeEnum.NORMAL.getValue());
        String userId = userService.saveAndReturnId(userVO, true);
        if (registerHandlerService != null) {
            registerHandlerService.handler(userId, queryParams);
        }
    }

    private void registerWithExpandInfo(String username, String password, Map<String, Object> expandInfo, Map<String, Object> queryParams) throws Exception {
        UserVO userVO = new UserVO();
        userVO.setUserUsername(username);
        userVO.setUserPassword(password);
        userVO.setUserName(username);
        if (isRegisterUserExamine()) {
            userVO.setUserState(UserStateEnum.UN_EXAMINE.getValue());
        } else {
            userVO.setUserState(UserStateEnum.NORMAL.getValue());
        }
        userVO.setUserType(UserTypeEnum.NORMAL.getValue());
        String userId = userService.saveAndReturnId(userVO, true);
        if (registerWithExpandInfoHandlerService != null) {
            registerWithExpandInfoHandlerService.handler(userId, expandInfo, queryParams);
        }
    }

    /**
     * 是否需要审核
     *
     * @return
     */
    private Boolean isRegisterUserExamine() {
        return Boolean.parseBoolean(systemConfigManager.getSystemConfigParamsValue(ISystemConstant.REGISTER_USER_EXAMINE));
    }

}
