package com.goafanti.user.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.goafanti.app.bo.UserBasicInfo;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseController;
import com.goafanti.common.dao.AdminMapper;
import com.goafanti.common.model.Admin;
import com.goafanti.common.model.User;
import com.goafanti.common.utils.TimeUtils;
import com.goafanti.common.utils.VerifyCodeUtils;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class UserLoginController extends BaseController {
	@Autowired
	private UserService userService;
	@Autowired
	private AdminMapper			adminMapper;

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	@ResponseBody
	public Result login() {
		Result res = new Result();
		TokenManager.logout();
		return res;
	}

	/**
	 * 用户登录
	 *
	 * @param user
	 * @param remember
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/signin", method = RequestMethod.POST)
	@ResponseBody
	public Result signin(User user, Boolean remember, HttpServletRequest request) {
		Result res = new Result();
		JSONObject jo = (JSONObject) JSON.toJSON(TokenManager.login(user, remember));
		SavedRequest sr = WebUtils.getAndClearSavedRequest(request);
		if (sr != null) {
			jo.put("requestURI", sr.getRequestURI());
			jo.put("queryString", sr.getQueryString());
		}
		res.setToken((String) SecurityUtils.getSubject().getSession().getId());
		return res;
	}


	/**
	 * 用户登录
	 *
	 * @param user
	 * @param remember
	 * @return
	 */
	@RequestMapping(value = "/mlogin", method = RequestMethod.POST)
	@ResponseBody
	public Result mLogin(User user, Boolean remember) {
		Result res = new Result();
		JSONObject jo = (JSONObject) JSON.toJSON(TokenManager.login(user, remember));

		UserBasicInfo ubi=userService.selectBaseInfo();
		jo.put("easemobName",ubi.getEasemobName());
		jo.put("easemobPass",ubi.getEasemobPass());
		res.setToken((String) SecurityUtils.getSubject().getSession().getId());
		res.setData(jo);
		// 发送推送消息
		return res;
	}

	/**
	 * 管理端登录
	 *
	 * @return
	 */
	@RequestMapping(value = "/managesignin", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public Result managesignin(Admin admin, Boolean remember) {
		Result res = new Result();
		Admin a =adminMapper.selectByMobile(admin.getMobile());
		if(a==null){
			res.getError().add(buildError(ErrorConstants.NON_REGISTER));
			return res;
		}
		if("1".equals(a.getStatus())){
			res.getError().add(buildError("","用户已被锁定不能登录"));
			return res;
		}
		if("2".equals(a.getStatus())){
			res.getError().add(buildError("","用户已被注销"));
			return res;
		}

		res.setData(TokenManager.manageLogin(admin, remember));

		if (a!=null) {
			shiroAddRedis();
			Admin updateDate =new Admin();
			updateDate.setId(a.getId());
			updateDate.setLastLoginTime(new Date());
			adminMapper.update(updateDate);
		}
		return res;
	}

	/**
	 * 忘记密码 使用手机号+手机验证码登录
	 *
	 */
	@RequestMapping(value = "/mobileSigin", method = RequestMethod.POST)
	@ResponseBody
	public Result mobileSignIn(User user, String mobileCode) {
		Result res = new Result();
		if (StringUtils.isBlank(user.getMobile())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "必须指定", "手机号"));
			return res;
		}
		if (TimeUtils.checkOverTime("register")) {
			res.getError().add(buildError(ErrorConstants.MCODE_OVERTIME_ERROR, "手机验证码超时失效"));
			return res;
		}
		if (!TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE).equals(mobileCode)) {
			res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "手机验证码错误"));
			return res;
		}
		return res;
	}

}
