package com.wechat.service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.wechat.common.utils.Md5Utils;
import com.wechat.common.utils.StringsUtils;
import com.wechat.global.CustomException;
import com.wechat.global.base.dao.RedisWithExpiryTimeDao;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.dbEntity.MdeUser;
import com.wechat.model.responseDto.ResultEntity;

@Service
public class ForgetPasswordService {

	@Value("#{configProperties['Code.expirationTime']}")
	private String codeExpirationTime;

	@Autowired
	private MdeUserService mdeUserService;

	@Autowired
	private CommonService commonService;

	@Autowired
	private RedisWithExpiryTimeDao redisDao;

	public static Logger log = LoggerFactory.getLogger(ForgetPasswordService.class);

	@Transactional
	public ResultEntity<String> forgetPassWord(JSONObject jo, HttpServletRequest request) {
		String userName = jo.getString("userName"); // 用户名
		String password = jo.getString("password"); // 密码
		String repassword = jo.getString("checkPass"); // 确认密码
		String identityCode = jo.getString("identityCode"); // 验证码
		// 用户名或密码为空
		if (StringUtils.isEmpty(userName) || StringsUtils.isEmpty(password)) {
			return new ResultEntity<>(InfoMsg.ERROR_LOGIN_MISS);
		}
		// 验证码验证
		if (!commonService.validateCode(identityCode, userName)) {
			return new ResultEntity<>(InfoMsg.ERROR_VERIFICATIONCODE);
		}
		// 判断两次输入的密码是否一致
		if (!StringsUtils.isEqual(password, repassword)) {
			return new ResultEntity<>(InfoMsg.ERROR_REPASSWORD_ERROR);
		}
		MdeUser mdeUser = mdeUserService.checkUserNameIsRegester(userName);
		// 要修改密码的用户名已经注册
		if (mdeUser != null) {
			// md5加密
			password = Md5Utils.EncoderPassword(password);
			mdeUser.setPassword(password);
			// 修改密码
			try {
				mdeUserService.changePassword(mdeUser);
				// 清空redis
				redisDao.delete("passwordErrorRecord" + userName.toLowerCase());
			} catch (Exception e) {
				log.info("密码修改失败！" + e.getMessage());
				new CustomException(InfoMsg.ERROR_PASSWORD_FILE);
			}
			return new ResultEntity<>(InfoMsg.SUCCESS_REQUEST);

		} else {
			if(checkMail(userName)) {
				return new ResultEntity<String>(InfoMsg.ERROR_ENTERPRISE_USER_NOTEXIST);
			}else {
				return new ResultEntity<String>(InfoMsg.ERROR_PERSON_USER_NOTEXIST);
			}
		}

	}
	/**
	 * 邮箱验证
	 * 
	 * @param userName
	 * @return
	 */
	public boolean checkMail(String mail) {
		boolean flag = false;
		try {
			String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(mail);
			flag = matcher.matches();
		} catch (Exception e) {
			log.info(e.getMessage());
			return flag;
		}
		return flag;
	}

}
