package com.app.controller.platform.auth;

import com.app.annotation.platform.OperationLog;
import com.app.controller.platform.request.WebAdminAccountLoginDto;
import com.app.controller.platform.response.AccountInfoResponse;
import com.app.controller.platform.response.LoginResponse;
import com.app.dto.Result;
import com.app.service.platform.auth.AuthenticationWebAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 功能描述：
 *
 * @Author 梁展鹏
 */
@Api(tags = "管理后台认证")
@Slf4j
@Validated
@RestController("PlatformAuthenticationController")
@RequestMapping("/platform/auth")
public class AuthenticationController {

    @Autowired
    private AuthenticationWebAdminService service;

    @ApiOperation(value = "获取图片验证码")
    @GetMapping("/img-code")
    public ResponseEntity<Result<?>> imgCode() {
        return ResponseEntity.ok(service.imgCode());
    }

    @ApiOperation(value = "管理后台登录")
    @PostMapping("/web-login")
    public ResponseEntity<Result<LoginResponse>> login(@Valid @RequestBody WebAdminAccountLoginDto dto, HttpServletRequest request) {
        return ResponseEntity.ok(service.login(dto, request));
    }

    @ApiOperation(value = "获取当前登录人信息")
    @GetMapping("/info")
    public ResponseEntity<Result<AccountInfoResponse>> info() {
        return ResponseEntity.ok(service.info());
    }

    /**
     * 功能描述：刷新token
     *
     * @param refreshToken
     * @return
     */
    @ApiOperation(value = "刷新token")
    @GetMapping("/refresh")
    public ResponseEntity<Result> refresh(@RequestParam String refreshToken) {
//        String username = (String) redisTemplate.opsForHash().get(refreshToken, "username");
//        if (StringUtils.isEmpty(username)) {
//            return new ResponseEntity<>(new Result(HttpStatus.UNAUTHORIZED, "refreshToken error", null), HttpStatus.UNAUTHORIZED);
//        }

        /* 生成新的token */
//        UserDetails userDetails = null;
//        Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, "", null);
//        String newToken = jwtUtils.generateToken(authentication); // JWT签名
//        redisTemplate.opsForHash().put(refreshToken, "token", newToken);

//        newToken, refreshToken
        return ResponseEntity.ok(null);
    }

    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public ResponseEntity<?> logout(HttpServletRequest request) {
        service.logout();
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

}
