package com.community.huiju.controller;

import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.service.WxLoginServiceI;
import com.community.huiju.vo.TaUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author FXF
 * @date 2019-03-29
 */
@RestController
@RefreshScope
@RequestMapping("/")
@Api(value = "微信登录 API", description = "微信登录 API")
public class WxLoginController extends BaseController {
	
	@Autowired
	private WxLoginServiceI wxLoginService;
	
	/**
	 * 微信登录
	 */
	@RequestMapping(value = "/wxLogin/{wxcode}", method = RequestMethod.POST)
	public ResponseBean autoLogin(@PathVariable String wxcode, HttpServletRequest request) {
		ResponseBean responseBean = new ResponseBean();
		if (StringUtils.isEmpty(wxcode)){
			responseBean.addError("fail");
			return responseBean;
		}
		//根据wxcode获取openid
		responseBean = wxLoginService.getOpenidByWxcode(wxcode, request.getSession());
		Object data = responseBean.getData();
		if (null != data && data instanceof TaUserVO) {
			setUserElement((TaUserVO) data, request.getSession());
		}
		return responseBean;
	}

	/**
	 * 关联微信账号
	 * @return
	 */
	@RequestMapping(value = "/wx/associated", method = RequestMethod.POST)
	@ApiOperation(value = "手机号和验证码 关联微信账号", notes = "手机号和验证码 关联微信账号")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataTypeClass = String.class, name = "parameter", value = "loginName手机号;code验证码;openid微信标识;nickname微信用户名称;headimgurl微信头像")
	})
	public ResponseBean associatedWX(@RequestBody String parameter, HttpSession session) {
		ResponseBean responseBean = new ResponseBean();
		responseBean = wxLoginService.associatedWX(parameter);
		TaUserVO userVO = (TaUserVO) responseBean.getData();
		if (null != userVO) {
			setUserElement(userVO, session);
		}
		return responseBean;
	}

	@ApiOperation(value = "注册", notes = "注册")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "userName用户姓名;idCard身份证;gender性别(1：男  2：女);loginName手机号;code验证码;openid微信标识;nickname微信用户名称;headimgurl微信头像")
	})
	@RequestMapping(value = "/wx/register",method = RequestMethod.POST)
	public ResponseBean register(@RequestBody String parameter, HttpServletRequest request ,HttpSession session) {
		ResponseBean responseBean = new ResponseBean();
		responseBean = wxLoginService.register(parameter, request);
		TaUserVO userVO = (TaUserVO) responseBean.getData();
		if (null != userVO) {
			setUserElement(userVO, session);
		}
		return responseBean;
	}

	@RequestMapping(value = "/unbundlingWX", method = RequestMethod.PUT)
	@ApiOperation(value = "解绑微信", notes = "解绑微信")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	public ResponseBean unbundlingWX(HttpSession session) {
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		responseBean = wxLoginService.unbundlingWX(userElement);
		return responseBean;
	}

	@RequestMapping(value = "/bundlingWX/{wxcode}", method = RequestMethod.POST)
	@ApiOperation(value = "绑定微信", notes = "绑定微信")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	public ResponseBean bundlingWX(HttpSession session, @PathVariable("wxcode") String wxcode) {
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		responseBean = wxLoginService.bundlingWX(userElement, wxcode);
		return responseBean;
	}
}
