package com.hotent.uc.controller;


import com.alibaba.fastjson.JSONObject;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.controller.BaseController;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.RSAUtil;
import com.hotent.base.util.StringUtil;
import com.hotent.uc.manager.OrgExternalSettingManager;
import com.hotent.uc.manager.PwdStrategyManager;
import com.hotent.uc.manager.ShortMsgLogManager;
import com.hotent.uc.manager.UserManager;
import com.hotent.uc.model.OrgExternalSetting;
import com.hotent.uc.model.PwdStrategy;
import com.hotent.uc.model.ShortMsgLog;
import com.hotent.uc.model.User;
import com.hotent.uc.params.user.UserPwdObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 组织外部系统参数配置 接口控制器
 *
 * @company 广州宏天软件股份有限公司
 * @author pangquan
 * @since 2020-10-30
 */
@RestController
@RequestMapping("/uc/shortMsg/v1/")
@Api(tags="组织外部系统参数配置")
@ApiGroup(group= {ApiGroupConsts.GROUP_UC})
public class ShortMsgLogController extends BaseController<OrgExternalSettingManager, OrgExternalSetting> {
	@Autowired
	private ShortMsgLogManager shortMsgLogManager;
	@Autowired
	private UserManager userManager;
	@Autowired
	private PwdStrategyManager pwdStrategyManager;

	/**
	 * @description  f发送短信
	 * @author  yangzibin
	 * @date    2024/2/22 16:01
	 * @param	mobile
	 * @return  com.alibaba.fastjson.JSONObject
	 */
	@RequestMapping(value = "sendMsgYzmCode", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "发送短信", httpMethod = "POST", notes = "发送短信")
	public JSONObject sendMsgYzmCode(@ApiParam(name = "mobile", value = "电话", required = true) @RequestParam String mobile
	,@ApiParam(name = "use", value = "用途", required = true) @RequestParam String use)
			throws Exception {
		return shortMsgLogManager.sendMsgYzmCode(mobile,"",use);
	}


	/**
	 * @description
	 * @author  yangzibin
	 * @date    2024/2/22 16:01
	 * @param	mobile
	 * @return  java.lang.String
	 */
	@RequestMapping(value = "codeYzmLogin", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "短信验证码登录", httpMethod = "POST", notes = "短信验证码登录")
	public User codeYzmLogin(@ApiParam(name = "mobile", value = "电话", required = true) @RequestParam String mobile,
							 @ApiParam(name = "code", value = "电话", required = true) @RequestParam String code,
							 @ApiParam(name = "use", value = "用途", required = true) @RequestParam String use)
			throws Exception {
		return shortMsgLogManager.verifyPhoneCode(mobile,use,code);
	}


	/**
	 * @description  移除手机号的使用过的验证码
	 * @author  yangzibin
	 * @date    2024/2/22 16:01
	 * @param	mobile
	 * @return  com.alibaba.fastjson.JSONObject
	 */
	@RequestMapping(value = "removeMsgYzmCode", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "移除手机号的使用过的验证码", httpMethod = "POST", notes = "移除手机号的使用过的验证码")
	public JSONObject removeMsgYzmCode(@ApiParam(name = "mobile", value = "电话", required = true) @RequestParam String mobile
	,@ApiParam(name = "use", value = "用途", required = true) @RequestParam String use)
			throws Exception {
		shortMsgLogManager.removeMsgYzmCode(mobile,use);
		return null;
	}

	/**
	 * 忘记密码_重新设置密码功能
	 * @param account 账号
	 * @param mobile 手机号
	 * @param validateCode 验证码
	 * @param newPsw 新密码
	 * @param reNewPsw 重复新密码
	 */
	@RequestMapping(value = "forgotPwd", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "忘记密码_重新设置密码功能", httpMethod = "POST", notes = "忘记密码_重新设置密码功能")
	public CommonResult<String> forgotPwd(@RequestParam(value="account",required=true) String account, @RequestParam(value="mobile",required=true) String mobile,
								@RequestParam(value="validateCode",required=true) String validateCode, @RequestParam(value="newPsw",required=true) String newPsw,
								@RequestParam(value="reNewPsw",required=true) String reNewPsw) throws Exception {
		if(!newPsw.equals(reNewPsw)) {
			return CommonResult.<String>error().message("密码和重复密码不一致");
		}

		String codeRel = shortMsgLogManager.getMsgVerCode(mobile, "1");
		if(StringUtil.isEmpty(codeRel)) {
			return CommonResult.<String>error().message("验证码已失效，请重新获取");
		}
		if(!validateCode.equals(codeRel)) {
			return CommonResult.<String>error().message("您输入验证码有误");
		}

		User user = userManager.getByAccount(account);
		if(user==null) {
			return CommonResult.<String>error().message("账号信息不匹配");
		}
		String userMobile = user.getMobile();
		if(StringUtil.isEmpty(userMobile)) {
			return CommonResult.<String>error().message("该用户未设置手机信息，请与管理员联系");
		}
		if(!userMobile.equals(mobile)) {
			return CommonResult.<String>error().message("账号或手机号信息不匹配");
		}

		String newPwd = "";
		try {
			newPwd = RSAUtil.Decrypt(newPsw);
		} catch (Exception e) {
			return CommonResult.<String>error().message("密码解密失败");
		}

		PwdStrategy pwdStrategy = pwdStrategyManager.getDefault();
		if(BeanUtils.isNotEmpty(pwdStrategy)) {
			int pwdRule = pwdStrategy.getPwdRule(); // 密码策略
			int pwdLength = pwdStrategy.getPwdLength(); // 密码长度
			int enable = pwdStrategy.getEnable(); // 启用策略	0:停用，1:启用
			if(enable == 1) {
				if(newPwd.length() < pwdLength) {
					return CommonResult.<String>error().message("新密码长度不能小于" + pwdLength);
				}
				if(pwdRule == 2) {//必须包含数字、字母
					String regex ="^(?![a-zA-z]+$)(?!\\d+$)(?![!@#$%^&*]+$)[a-zA-Z\\d!@#$%^&*]+$";
					boolean result = newPwd.matches(regex);
					if(!result) {
						return CommonResult.<String>error().message("新密码必须包含数字、字母");
					}
				}else if(pwdRule == 3) {//必须包含数字、字母、特殊字符
					String regex = "^(?=.*?[A-Za-z])(?=.*?\\d)(?=.*?[~!@#$%^&*()_+`\\-={}:\";'<>?,.\\/])[a-zA-Z\\d~!@#$%^&*()_+`\\-={}:\";'<>?,.\\/]*$";
					boolean result = newPwd.matches(regex);
					if(!result) {
						return CommonResult.<String>error().message("新密码必须包含数字、字母、特殊字符");
					}
				}else if(pwdRule == 4) {//必须包含数字、大小字母、特殊字符
					String regex = "^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\\d)(?=.*?[~!@#$%^&*()_+`\\-={}:\";'<>?,.\\/])[a-zA-Z\\d~!@#$%^&*()_+`\\-={}:\";'<>?,.\\/]*$";
					boolean result = newPwd.matches(regex);
					if(!result) {
						return CommonResult.<String>error().message("新密码必须包含数字、大小字母、特殊字符");
					}
				}
			}
		}

		try {
			//重置密码
			UserPwdObject userPwdObject = new UserPwdObject();
			userPwdObject.setAccount(account);
			userPwdObject.setNewPwd(newPwd);
			userManager.updateUserPsw(userPwdObject);
			shortMsgLogManager.removeMsgYzmCode(mobile, "1");
			return CommonResult.<String>ok().message("忘记密码设置成功,请重新登录");
		} catch (Exception e) {
			e.printStackTrace();
			return CommonResult.<String>error().message("忘记密码设置失败");
		}
	}

}
