package com.yonge.cooleshow.biz.dal.service.impl;

import java.util.List;

import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.SysConfig;
import com.yonge.cooleshow.biz.dal.entity.SysMessage;
import com.yonge.cooleshow.biz.dal.enums.MessageSendMode;
import com.yonge.cooleshow.biz.dal.enums.MessageTypeEnum;
import com.yonge.cooleshow.biz.dal.service.SmsCodeService;
import com.yonge.cooleshow.biz.dal.service.SysConfigService;
import com.yonge.cooleshow.biz.dal.service.SysMessageService;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.thirdparty.message.MessageSenderPluginContext;

@Service
public class SmsCodeServiceImpl implements SmsCodeService {

	@Autowired
	private SysMessageService sysMessageService;
	@Autowired
	private SysUserFeignService sysUserFeignService;
	@Autowired
	private SysConfigService sysConfigService;

	@Override
	public boolean verifyValidCode(String mobile, String authCode, String type) {
		MessageTypeEnum typeEnum;
		if ("PASSWD".equals(type)) {
			typeEnum  = MessageTypeEnum.SMS_VERIFY_CODE_UPDATE_PSW;
		} else if ("LOGIN".equals(type)) {
			typeEnum  = MessageTypeEnum.SMS_VERIFY_CODE_LOGIN;
		} else if ("REGISTER".equals(type)) {
			typeEnum  = MessageTypeEnum.SMS_VERIFY_CODE_REGISTER;
		} else if ("BANK".equals(type)){
			typeEnum  = MessageTypeEnum.SMS_VERIFY_CODE_BANK_CARD;
		} else if ("PHONE".equals(type)) {
			typeEnum  = MessageTypeEnum.SMS_VERIFY_CODE_UPDATE_PHONE;
		} else {
			typeEnum  = MessageTypeEnum.SMS_VERIFY_CODE;
		}
		String verifyCode = sysMessageService.getSendedVerificationCode(typeEnum, mobile);
		if (StringUtils.isNoneEmpty(verifyCode) && StringUtils.equalsIgnoreCase(verifyCode, authCode)) {
			return true;
		}
		return false;
	}

	@Override
	public boolean sendValidCode(String mobile, String type, ClientEnum platform) {
		//SMS_VERIFY_CODE_LOGOFF
		SysUser sysUser = sysUserFeignService.queryUserByMobile(mobile);
		// 修改手机号 有用户了不许修改
		if ("PHONE".equals(type) && sysUser != null) {
			throw new BizException("手机号已经注册过账号");
		}
		Long userId = null;
		if (sysUser != null) {
			userId = sysUser.getId();
		} else {
			sysUser = sysUserFeignService.queryUserInfo();
			if (sysUser != null) {
				userId = sysUser.getId();
			}
		}
		SysConfig sysConfig = sysConfigService.findByParamName(SysConfigService.SMS_MAX_TIMES);
		int times = 3;
		if (sysConfig != null) {
			times = Integer.parseInt(sysConfig.getParamValue());
		}
		// 十分钟内只能发3条
		List<SysMessage> list = sysMessageService.queryUserInRecentMinList(mobile, 10, MessageSendMode.SMS);
		if (list != null && list.size() >= times) {
			throw new BizException("对不起，您发送太频繁请稍后重试！");
		}
		if ("PASSWD".equals(type)) {
			sysMessageService.sendSecurityCode(MessageSenderPluginContext.MessageSender.AWSMS, userId, MessageTypeEnum.SMS_VERIFY_CODE_UPDATE_PSW, mobile,platform.getMsg());
		} else if ("LOGIN".equals(type)) {
			sysMessageService.sendSecurityCode(MessageSenderPluginContext.MessageSender.AWSMS, userId, MessageTypeEnum.SMS_VERIFY_CODE_LOGIN, mobile,platform.getMsg());
		} else if ("REGISTER".equals(type)) {
			sysMessageService.sendSecurityCode(MessageSenderPluginContext.MessageSender.AWSMS, userId, MessageTypeEnum.SMS_VERIFY_CODE_REGISTER, mobile,platform.getMsg());
		} else if ("BANK".equals(type)){
			sysMessageService.sendSecurityCode(MessageSenderPluginContext.MessageSender.AWSMS, userId, MessageTypeEnum.SMS_VERIFY_CODE_BANK_CARD, mobile,platform.getMsg());
		} else if ("PHONE".equals(type)) {
			sysMessageService.sendSecurityCode(MessageSenderPluginContext.MessageSender.AWSMS, userId, MessageTypeEnum.SMS_VERIFY_CODE_UPDATE_PHONE, mobile,platform.getMsg());
		} else if("LOGOFF".equals(type)){
			sysMessageService.sendSecurityCode(MessageSenderPluginContext.MessageSender.AWSMS, userId, MessageTypeEnum.SMS_VERIFY_CODE_LOGOFF, mobile,platform.getMsg());
		} else {
			sysMessageService.sendSecurityCode(MessageSenderPluginContext.MessageSender.AWSMS, userId, MessageTypeEnum.SMS_VERIFY_CODE, mobile,platform.getMsg());
		}
		return true;
	}

}
