package com.yihu.wlyy.web.common.sms;

import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yihu.wlyy.entity.doctor.profile.Doctor;
import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.repository.doctor.DoctorDao;
import com.yihu.wlyy.repository.patient.PatientDao;
import com.yihu.wlyy.service.common.SMSService;
import com.yihu.wlyy.util.NetworkUtil;
import com.yihu.wlyy.web.BaseController;

@Controller
@RequestMapping(value = "/common")
@Api(description = "短信")
public class SMSController extends BaseController {

	@Autowired
	public SMSService smsService;
	@Autowired
	private PatientDao patientDao;
	@Autowired
	private DoctorDao doctorDao;

	/**
	 * 发送短信验证码接口
	 * @param mobile 手机号
	 * @param type 消息类型:1微信端注册，2微信端找回密码，3医生端找回密码，4患者登录，5医生登录 .6患者签约验证 7用户变更手机号验证 8用户新手机号验证 9绑定手机号
	 *             10 家庭成员添加验证
	 * @return
	 */
	@RequestMapping(value = "captcha", method = RequestMethod.POST)
	@ResponseBody
	public String send(String mobile, int type, @RequestParam(required = false) String captchaToken) {
		try {
			if (type > 10 || type < 1) {
				return error(-1, "无效的请求！");
			}
			if (StringUtils.isEmpty(mobile)) {
				return error(-1, "手机号码不允许为空！");
			}
			if (type == 4) {
				// 登录时校验用户是否存在
				Patient patient = patientDao.findByMobile(mobile);
				if (patient == null) {
					return error(-1, "该手机号未注册！");
				}
			} else if (type == 5) {
				// 登录时校验用户是否存在
				Doctor doctor = doctorDao.findByMobile(mobile);
				if (doctor == null) {
					return error(-1, "该手机号未注册！");
				}
			}
			if (StringUtils.isNotEmpty(captchaToken)) {
				String ct = request.getSession().getAttribute("captchaToken").toString();
				if (!StringUtils.equalsIgnoreCase(captchaToken, ct)) {
					return error(-1, "图形验证码错误！");
				}
			}
			// 获取ip地址
			String ip = NetworkUtil.getIpAddress(request);
			if (StringUtils.isEmpty(ip)) {
				return error(-1, "无效的ip请求！");
			}
			String res = smsService.send(mobile, ip, type, getUID());
			if (StringUtils.equals(res, "ok")) {
				return write(200, "验证码短信已发送！");
			} else {
				return error(-1, res);
			}
		} catch (Exception e) {
			error(e);
			return error(-1, "短信发送失败！");
		}
	}

	/**
	 * 验证手机验证码
	 * @param type 消息类型:1微信端注册，2微信端找回密码，3医生端找回密码
	 * @param mobile
	 * @param captcha
	 * @return
	 */
	@RequestMapping(value = "check_captcha", method = RequestMethod.POST)
	@ResponseBody
	public String checkCaptcha(int type, String mobile, String captcha) {
		try {
			// 对验证码进行校验
			int res = smsService.check(mobile, type, captcha);
			switch (res) {
			case -2:
				return error(-1, "验证码已过期！");
			case -1:
				return error(-1, "验证码错误！");
			case 0:
				return error(-1, "验证码无效！");
			}
			return success("有效验证码！");
		} catch (Exception e) {
			error(e);
			return error(-1, "验证码检查失败！");
		}
	}

}
