package com.sv.api.controller;

import com.sv.api.context.PlatformContext;
import com.sv.dto.api.BindMobileDTO;
import com.sv.dto.api.MemberTokenDTO;
import com.sv.api.service.impl.LoginRegisterServiceImpl;
import com.sv.api.service.impl.wechat.WechatService;
import com.ydd.framework.core.annotation.AccessToken;
import com.ydd.framework.core.common.dto.ResponseDTO;
import com.ydd.framework.core.controller.BaseApiController;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RestController;

import javax.annotation.Resource;

/**
 * Controller - 用户注册登录
 *
 * @author lh
 * @since 2018-08-02
 */
@RestController
public class LoginOrRegisterController extends BaseApiController {

	private final Logger logger = LoggerFactory.getLogger(LoginOrRegisterController.class);

	@Resource
	private LoginRegisterServiceImpl loginRegisterService;
	@Resource
	private WechatService wechatService;

	/**
     * 用户手机号码注册
	 * @param mobile
     * @param captcha
     * @param name
     * @param sex
     * @param age
     * @param address
     * @return
     */
	@RequestMapping(value = "/member/register" , method = RequestMethod.POST)
	public ResponseDTO register(@RequestParam("mobile")String mobile,@RequestParam("captcha")String captcha,
								@RequestParam(value = "name",required = false)String name,
								@RequestParam(value = "sex",required = false)Integer sex,
								@RequestParam(value = "age",required = false)Integer age,
								@RequestParam(value = "address",required = false)String address
								){
		MemberTokenDTO memberTokenDTO =  loginRegisterService.registerByMobile(mobile, captcha, name, sex, age , address);
		return ResponseDTO.ok().addAttribute("authorization",memberTokenDTO);
	}

    /**
     * 手机号和验证码登录
     */
    @RequestMapping(value = "/member/login" , method = RequestMethod.POST)
    public ResponseDTO register(@RequestParam("mobile")String mobile,@RequestParam("captcha")String captcha){

        return ResponseDTO.ok().addAttribute("authorization",loginRegisterService.loginByMobile(mobile,captcha));
    }

	/**
	 * 微信授权登录
	 */
	@RequestMapping(value = "/member/login/wx" , method = RequestMethod.POST)
	public ResponseDTO loginWx(@RequestParam("code") String code, @RequestParam("nickname") String nickname,
							   @RequestParam("avatar") String avatar){
		String openId = wechatService.getXcxOpenId(code); //查询openId:
		MemberTokenDTO memberTokenDTO = loginRegisterService.loginWithXcx(openId,nickname,avatar);

		// memberTokenDTO为null则跳绑定手机页面
		return ResponseDTO.ok().addAttribute("authorization",memberTokenDTO)
				.addAttribute("information",loginRegisterService.info(avatar,nickname,openId));
	}

	/**
	 * 绑定手机号
	 */
	@RequestMapping(value = "/member/login/wx/phone" , method = RequestMethod.POST)
	public ResponseDTO phone(BindMobileDTO bindMobileDTO){
		MemberTokenDTO memberTokenDTO = loginRegisterService.bildMobile(bindMobileDTO);
		return ResponseDTO.ok()
				.addAttribute("authorization",memberTokenDTO);
	}

    /**
     * 退出登录
     */
    @RequestMapping(value = "/member/sms/logout" , method = RequestMethod.POST)
    @AccessToken
    public ResponseDTO logout(@Param("mobile") String mobile) {
        loginRegisterService.logout(getLoginMemberId());
        return ResponseDTO.ok();
    }

	/**
	 * 已用手机号登录，支付，再跳转微信登录
	 */
	@RequestMapping(value = "/member/login/checkWx" , method = RequestMethod.POST)
	public ResponseDTO loginCheckWx(@RequestParam("code") String code, @RequestParam("nickname") String nickname,
								   @RequestParam("avatar") String avatar){
		String openId = wechatService.getXcxOpenId(code); //查询openId:
		MemberTokenDTO memberTokenDTO = loginRegisterService.loginCheckWx(openId,nickname,avatar,getMemberIdByAccessToken(),1);

		// memberTokenDTO为null则跳绑定手机页面
		return ResponseDTO.ok().addAttribute("authorization",memberTokenDTO)
				.addAttribute("information",loginRegisterService.info(avatar,nickname,openId));
	}
}
