package cn.lihongjie.coal.session.controller;

import cn.lihongjie.coal.annotation.Anonymous;
import cn.lihongjie.coal.annotation.SysLog;
import cn.lihongjie.coal.base.controller.BaseController;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.common.Ctx;
import cn.lihongjie.coal.rabbitmq.RabbitMQService;
import cn.lihongjie.coal.session.dto.CaptchaDto;
import cn.lihongjie.coal.session.dto.LoginDto;
import cn.lihongjie.coal.session.service.SessionService;
import cn.lihongjie.coal.trustDevice.dto.TrustDeviceDto;
import cn.lihongjie.coal.trustDevice.service.TrustDeviceService;
import cn.lihongjie.coal.user.dto.UserDto;
import cn.lihongjie.coal.user.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class LoginController extends BaseController {

    @Autowired
    SessionService service;

    @Autowired UserService userService;

    @Autowired RabbitMQService rabbitMQService;

    @Autowired TrustDeviceService trustDeviceService;

    @PostMapping("/login")
    @SysLog(action = "登录")
    @Anonymous
    public UserDto login(@RequestBody LoginDto dto) {
        this.service.login(dto);







        return userService.getById(Ctx.getUserId());
    }




    @PostMapping("/trustDeviceBind")
    @SysLog(action = "信任设备")
    public Boolean trustDevice(@RequestBody TrustDeviceDto dto) {

        trustDeviceService.trustDevice(dto);

        return true;
    }




    @PostMapping("/genCaptcha")
    @Anonymous
    public CaptchaDto genCaptcha() {
        return this.service.genCaptcha();
    }


    @PostMapping("/loginSmsCode")
    @Anonymous
    public CaptchaDto loginSmsCode(@RequestBody IdRequest request) {
        return this.service.loginSmsCode(request);
    }


    @PostMapping("/logout")
    @SysLog(action = "退出")
    public Object logout() {
        if (Ctx.isLoggedIn()) {

            this.service.userLogout(
);
        }
        return true;
    }

    @PostMapping("/isValid")
    @Anonymous
    public Boolean isValid() {

        return Ctx.isLoggedIn();
    }
}
