package thyyxxk.webserver.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.aop.interfaces.NeedMenu;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.enums.authorize.MenuEnum;
import thyyxxk.webserver.enums.authorize.RuleId;
import thyyxxk.webserver.service.LoginService;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author dj
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    private final LoginService service;

    @Autowired
    public LoginController(LoginService service) {
        this.service = service;
    }

    @PassToken
    @PostMapping("/login")
    public ResultVo<UserInfo> login(@RequestBody UserInfo userInfo) {
        return service.login(userInfo, true);
    }

    @GetMapping("/simpleLogin")
    @PassToken
    public ResultVo<UserInfo> simpleLogin(@RequestParam("code") String code) {
        return service.simpleLogin(code);
    }

    @GetMapping("/getWards")
    public ResultVo<List<CodeName>> getWards() {
        return service.getWards();
    }

    @PassToken
    @GetMapping("/getAllWards")
    public ResultVo<List<CodeName>> getAllWards() {
        return service.getAllWards();
    }

    @GetMapping("/sendAVerificationCode")
    @PassToken
    public ResultVo<String> sendAVerificationCode(@RequestParam("codeRs") String codeRs) {
        return service.sendAVerificationCode(codeRs);
    }

    @GetMapping("/checkVerificationCode")
    @PassToken
    public ResultVo<String> checkVerificationCode(@RequestParam("codeRs") String codeRs,
                                                  @RequestParam("verificationCode") String verificationCode) {
        return service.checkVerificationCode(codeRs, verificationCode);
    }

    @PassToken
    @PostMapping("/checkVerificationCodeV2")
    public ResultVo<String> checkVerificationCodeV2(@RequestBody LoginService.Verification verification) {
        return service.checkVerificationCodeV2(verification);
    }

    @PostMapping("/getUserInfo")
    public ResultVo<UserInfo> getUserInfo() {
        return ResultVoUtil.success(service.getUserInfoByCode());
    }

    @PassToken
    @GetMapping("/getToken")
    public ResultVo<String> getToken(@RequestParam String code) {
        return ResultVoUtil.success(TokenUtil.getInstance().createToken(code, ""));
    }

}
