package com.wechat.controller;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wechat.common.utils.StringsUtils;
import com.wechat.global.base.BaseController;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.responseDto.ResultEntity;
import com.wechat.service.VerificationCodeService;

@Controller
@RequestMapping("verifyCode")
public class VerificationCodeController extends BaseController {

	@Resource
	private VerificationCodeService vefificationCodeService;

	/**
	 * 短信验证码取得
	 * 
	 * @param map
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "getVerifyCode", method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<String> getVerifyCode(@RequestBody Map<String, String> map, HttpServletRequest request)
			throws IOException {

		Cookie[] cookies = request.getCookies();
		String type = map.get("type");
		// type 0：个人 1：企业 2:忘记密码
		if (StringsUtils.isEmpty(type)) {
			return new ResultEntity<String>(InfoMsg.ERROR_NULL_PARAM);
		}
		String userName = StringsUtils.trim(map.get("userName"));
		String imageCode = map.get("imageCode");
		String entPhone = StringsUtils.trim(map.get("entPhone"));
		String phoneCode = map.get("phoneCode");
		
		//去除验证码首尾空格
		imageCode = StringsUtils.trim(imageCode);
		
		// 个人注册
		if (type.equals("0")) {
			return vefificationCodeService.getPersonVerifyCode(userName, imageCode, cookies, phoneCode);
		} else if (type.equals("1")) {
			// 企业注册
			return vefificationCodeService.getEnterpriseVerifyCode(userName, imageCode, entPhone, cookies);
		} else if (type.equals("2")) {
			// 忘记密码
			return vefificationCodeService.getForgetVerifyCode( userName, imageCode, cookies, request);
		} else {
			return new ResultEntity<>(InfoMsg.ERROR_TYPE_ERROR);
		}
	}

	/**
	 * 企业发送汇款短信
	 * 
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "sendRemittanceInformation", method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<String> sendRemit(@RequestBody Map<String, String> param) {
		String phoneNo = param.get("phoneNo"); // 手机号
		String orderId = param.get("orderId"); // 汇付识别码
		if (StringsUtils.isEmpty(orderId) || StringsUtils.isEmpty(phoneNo)) {
			return new ResultEntity<>(InfoMsg.ERROR_PARAMS_ERROR);
		}
		return vefificationCodeService.sendRemit( phoneNo, orderId);

	}
	
	
}
