package com.github.wxiaoqi.security.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.wxiaoqi.security.auth.service.AuthService;
import com.github.wxiaoqi.security.auth.util.user.JwtAuthenticationRequest;
import com.github.wxiaoqi.security.common.constant.RequestTypeConstants;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.util.process.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("jwt")
@Slf4j
public class AuthController {
    @Value("${jwt.token-header}")
    private String tokenHeader;

    @Autowired
    @Qualifier("baseAuthService")
    private AuthService authService;

    @Autowired
    @Qualifier("appAuthService")
    private AuthService appAuthService;

    @RequestMapping(value = "token", method = RequestMethod.POST)
    public ObjectRestResponse<String> createAuthenticationToken(
            @RequestBody JwtAuthenticationRequest authenticationRequest,
            HttpServletRequest request) throws Exception {
        log.info(authenticationRequest.getUsername()+" require logging...");
        // keliii 分请求类型处理token
        String requestType = request.getHeader(RequestTypeConstants.HEADER_KEY);
        if (requestType == null || requestType.trim().equals("")) {
            requestType = RequestTypeConstants.BASE;
        }
        final String token;
        if (RequestTypeConstants.APP.equals(requestType)) {
            token = appAuthService.login(authenticationRequest);
        } else {
            token = authService.login(authenticationRequest);
        }
        return new ObjectRestResponse<String>().data(token);
    }

    @RequestMapping(value = "refresh", method = RequestMethod.GET)
    public ObjectRestResponse<String> refreshAndGetAuthenticationToken(
            HttpServletRequest request) throws Exception {
        String token = request.getHeader(tokenHeader);
        String refreshedToken = authService.refresh(token);
        return new ObjectRestResponse<String>().data(refreshedToken);
    }

    @RequestMapping(value = "verify", method = RequestMethod.GET)
    public ObjectRestResponse<?> verify(String token) throws Exception {
        authService.validate(token);
        return new ObjectRestResponse<>();
    }
    @RequestMapping(value = "/sendsms", method = RequestMethod.POST)
    public JSONObject sendsms(@RequestParam(value="username",defaultValue="")String  username, @RequestParam(value="type",defaultValue="0")Integer type)throws Exception{
        log.info(username+"----require sendsms...");
        return appAuthService.sendsms(username,type);
    }
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public JSONObject register(@RequestParam(value="username",defaultValue="")String  username,
                              @RequestParam(value="mobilecode",defaultValue="")String mobilecode,
                              @RequestParam(value="password",defaultValue="")String  password)throws Exception{
        log.info(username+"----require register...");
        JSONObject data=appAuthService.register(username,mobilecode,password);
        if(data!=null&&data.getInteger("status")== ResultCode.SUCCESS_CODE){
            JwtAuthenticationRequest authenticationRequest=new JwtAuthenticationRequest();
            authenticationRequest.setUsername(username);
            authenticationRequest.setPassword(password);
            String token=appAuthService.login(authenticationRequest);
            data.put("token",token);
        }
        return data;
    }
    @RequestMapping(value = "/wxregister", method = RequestMethod.POST)
    public JSONObject wxregister(
            @RequestParam(value="username",defaultValue="")String  username,
            @RequestParam(value="mobilecode",defaultValue="")String mobilecode,
            @RequestParam(value="password",defaultValue="")String  password,
            @RequestParam(value="nickname",defaultValue="")String  nickname,
            @RequestParam(value="headimgurl",defaultValue="")String  headimgurl,
            @RequestParam(value="openid",defaultValue="")String  openid,
            @RequestParam(value="unionid",defaultValue="")String  unionid,
            @RequestParam(value="type",defaultValue="0")Integer  type,
            @RequestParam(value="isQQ",defaultValue="0")Integer  isQQ
    )throws Exception{
        log.info(username+"----require wxregister...");
        JSONObject data=appAuthService.wxregister( username, mobilecode, password, nickname,
                headimgurl, openid, unionid,type,isQQ);
        if(data!=null&&data.getInteger("status")== ResultCode.SUCCESS_CODE){
            JSONObject result=data.getJSONObject("data");
            if(result==null){
                data.put("status",1001);
            }else {
                String token=appAuthService.getToken(username,result.getInteger("userid"));
                data.put("token",token);
            }
        }
        return data;
    }
    @RequestMapping(value = "/checkBindWechat", method = RequestMethod.POST)
    public JSONObject checkBindWechat(@RequestParam(value="username",defaultValue="")String  username)throws Exception{
        log.info(username+"----require checkBindWechat...");
        return appAuthService.checkBindWechat(username);
    }
    @RequestMapping(value = "/wxlogin", method = RequestMethod.POST)
    public JSONObject wxlogin(@RequestParam(value="openid",defaultValue="")String  openid,@RequestParam(value="isQQ",defaultValue="0")Integer isQQ)throws Exception{
        log.info(openid+"----require wxlogin...");
        JSONObject data=appAuthService.wxlogin(openid,isQQ);
        if(data!=null&&data.getInteger("status")== ResultCode.SUCCESS_CODE){
            JSONObject result=data.getJSONObject("data");
            if(result==null){
                data.put("status",1001);
            }else {
                String token=appAuthService.getToken(result.getString("username"),result.getInteger("userid"));
                data.put("token",token);
            }
        }
        return data;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public JSONObject login(@RequestParam(value="username",defaultValue="")String  username,
                            @RequestParam(value="mobilecode",defaultValue="")String mobilecode,
                            @RequestParam(value="password",defaultValue="")String  password,
                            @RequestParam(value="type",defaultValue="1")Integer  type)throws Exception{
        log.info(username+"----require login...");
        JSONObject data=appAuthService.tlogin(username,password,mobilecode,type);
        if(data!=null&&data.getInteger("status")== ResultCode.SUCCESS_CODE){
            JSONObject result=data.getJSONObject("data");
            if(result==null){
                data.put("status",1001);
            }else {
                String token=appAuthService.getToken(username,result.getInteger("userid"));
                data.put("token",token);
            }
        }
        return data;
    }
    @RequestMapping(value = "/reset", method = RequestMethod.POST)
    public JSONObject reset(@RequestParam(value="username",defaultValue="")String  username,
                               @RequestParam(value="mobilecode",defaultValue="")String mobilecode,
                               @RequestParam(value="password",defaultValue="")String  password)throws Exception{
        log.info(username+"----require reset...");
        JSONObject data=appAuthService.reset(username,mobilecode,password);
        if(data!=null&&data.getInteger("status")== ResultCode.SUCCESS_CODE){
            JwtAuthenticationRequest authenticationRequest=new JwtAuthenticationRequest();
            authenticationRequest.setUsername(username);
            authenticationRequest.setPassword(password);
            String token=appAuthService.login(authenticationRequest);
            data.put("token",token);
        }
        return data;
    }


}
