package com.jxy.auth.controller;

import com.jxy.auth.entity.*;
import com.jxy.auth.service.SysLoginService;
import com.jxy.auth.service.impl.MaliServiceImpl;
import com.jxy.common.core.Result.R;
import com.jxy.common.core.utils.JwtUtils;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.security.auth.AuthUtil;
import com.jxy.common.security.service.TokenService;
import com.jxy.common.security.utils.SecurityUtils;
import com.jxy.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * @Description token 控制
 * @Date 2022/5/12 14:59
 * @Author 杜懿
 */
@RestController
@Api(tags = "token令牌")
public class TokenController {

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private MaliServiceImpl maliService;

    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody loginBody,@RequestHeader("User-Agent") String userAgent)
    {
        return sysLoginService.login(loginBody);
    }


    @PostMapping("emailcode")
    public R<?> emailCode(@RequestBody GetEmailCodeEntity entity)
    {
        return maliService.emailCode(entity);
    }

    @PostMapping("addCreditEmailCode")
    public R<?> addCreditEmailCode(@RequestBody GetEmailCodeEntity entity)
    {
        return maliService.AddCreditCode(entity);
    }

    @PostMapping("resetPwdCode")
    public R<?> resetPwdCode(@RequestBody GetEmailCodeEntity entity)
    {
        return maliService.resetPwdCode(entity);
    }

    @GetMapping("hello")
    public R<?> hello()
    {
        return R.success("浏览器安全认证已确认，请重新返回原页面进行登陆");
    }

    @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);
        }
        return R.success("用户登出");
    }

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

    @PostMapping("register")
    public R<?> register(@Valid() @RequestBody RegisterBody registerBody)
    {
        // 用户注册
        sysLoginService.register(registerBody);
        return R.success("用户"+registerBody.getUserName()+"注册成功");
    }

    @PostMapping("resetPwd")
    public R<?> resetPwd(@Valid @RequestBody ResetPwdBody resetPwdBody)
    {
        // 重置密码
        sysLoginService.resetPwd(resetPwdBody);
        return R.success("账号"+ resetPwdBody.getEmail()+"密码重置成功");
    }

    @PostMapping("sendTextMail")
    public R<?> sendTextMail(@Valid @RequestBody EmailEntity entity)
    {
        maliService.sendTextMailMessage(entity.getEmail(), entity.getSubject(),entity.getText());
        return R.success("邮件已发送");
    }


}
