package com.xunmei.auth.controller;

import com.xunmei.auth.form.LoginBody;
import com.xunmei.auth.form.RegisterBody;
import com.xunmei.auth.service.SysLoginService;
import com.xunmei.common.core.constant.Constants;
import com.xunmei.common.core.domain.R;
import com.xunmei.common.core.utils.JwtUtils;
import com.xunmei.common.core.utils.StringUtils;
import com.xunmei.common.security.auth.AuthUtil;
import com.xunmei.common.security.service.TokenService;
import com.xunmei.common.security.utils.SecurityUtils;
import com.xunmei.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * token 控制
 *
 * @author xunmei
 */
@RestController
public class TokenController {
    @Resource
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form) {

        try {
            if (!SecurityUtils.isApp()){
                //登录重放问题处理,待前端完成放开
                //sysLoginService.checkLogin(form.getAuthCode());
            }
            LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword(), Constants.LOGIN_TYPE_1);
            // 获取登录token
            return R.ok(tokenService.createToken(userInfo));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        // 用户登录
    }


    @PostMapping("loginApp")
    public R<?> loginApp(@RequestBody LoginBody form) {

        try {
            if (!SecurityUtils.isApp()){
                //登录重放问题处理,待前端完成放开
                //sysLoginService.checkLogin(form.getAuthCode());
            }
            LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword(), Constants.LOGIN_TYPE_0);
            // 获取登录token
            return R.ok(tokenService.createToken(userInfo));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        // 用户登录
    }


    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token)) {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username,Constants.LOGIN_TYPE_1);
        }
        return R.ok();
    }

    @DeleteMapping("logoutApp")
    public R<?> logoutApp(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token)) {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username,Constants.LOGIN_TYPE_0);
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser)) {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }
}
