package com.ym.mec.common.validcode.impl;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.mec.common.redis.service.RedisCache;
import com.ym.mec.common.validcode.SmsCodeService;
import com.ym.mec.thirdparty.message.MessageSenderPluginContext;

@Service
public class SmsCodeServiceImpl implements SmsCodeService {
	
	@Autowired
	private RedisCache<String,Object> redisCache;
	
	private final String loginVerifyCodeKey = "loginVerifyCode:";
	
	private int expireTime = 60;

	@Autowired
	private MessageSenderPluginContext messageSenderPluginContext;

	@Override
	public boolean verifyValidCode(String mobile, String authCode) {
		String verifyCode = (String) redisCache.get(loginVerifyCodeKey + mobile);
		if(StringUtils.isNoneEmpty(verifyCode) && StringUtils.equalsIgnoreCase(verifyCode,authCode)){
			return true;
		}
		return true;
	}

	@Override
	public boolean sendValidCode(String mobile) throws Exception {
		String code = RandomStringUtils.randomNumeric(6);
		// 发送验证码
//		messageSenderPluginContext.send(MessageSenderPluginContext.MessageSender.YIMEI,"","",mobile,"");
		redisCache.put(loginVerifyCodeKey + mobile, code, expireTime);
		return true;
	}

	@Override
	public String getValidCode(String mobile) {
		return (String) redisCache.get(loginVerifyCodeKey + mobile);
	}

	@Override
	public void removeValidCode(String mobile) {
		if (redisCache.exists(loginVerifyCodeKey + mobile)) {
			redisCache.delete(loginVerifyCodeKey + mobile);
		}
	}

}
