package com.iamberry.app.international.controller;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.iamberry.app.config.Response;
import com.iamberry.app.config.ResponseHeader;
import com.iamberry.app.face.CodeService;
import com.iamberry.app.face.LogService;
import com.iamberry.app.face.MachineService;
import com.iamberry.app.face.MessageService;
import com.iamberry.app.face.MilkService;
import com.iamberry.app.face.UserService;
import com.iamberry.wechat.tools.ResponseJson;

public class BaseController {

	@Autowired
	protected UserService userService;
	@Autowired
	protected MachineService machineService;
	@Autowired
	protected MilkService milkService;
	@Autowired
	LogService logService;
	@Autowired
	MessageService messageService;
	@Autowired
	HttpServletRequest request;
	@Autowired
	CodeService codeService;

	public final static Map<String, Object> validateCodeMap = new ConcurrentHashMap<>();

	protected static Response createResponse(ResponseHeader header, Object payload) {
		return new Response(header, payload);
	}

	protected static Response createResponse(ResponseHeader header) {
		return new Response(header);
	}

/**
 **?*
 */
	/**
	 * 判断用户的手机号码是否和短信验证码匹配，注意，此类实现方法，本身就是一个弊端，系统一旦长期运行，会导致内存崩溃。
	 * @param phone				手机号码
	 * @param verificationCode	短信验证码
	 * @return
	 */
	protected int checkVerifyCode(String phone, String verificationCode, int codeScenario) {
		ResponseJson json = codeService.validCode(phone, verificationCode, codeScenario);
		if (json.getReturnCode() == 200) {
			return 1;
		} else if (json.getReturnCode() == 404) {
			return -1;
		}
		return -2;
	}

	/**
	 * 校验时间，如果发送时间+180s 大于 当前时间，返回false 
	 * @param date
	 * @return
	 */
	protected static boolean verifyCodeTime(Date date) {
		Long nowDate = new Date().getTime();
		if (date.getTime() + 180000 > nowDate) {
			return false;
		}
		return true;
	}
}
