package com.roma.romaapi.controller;


import com.roma.romaapi.service.ApiService;
import com.roma.romaapi.service.CaptchaService;
import com.roma.romaapi.utils.CustomResponse;
import com.roma.romaapi.utils.UUIDUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin
public class ApiController {
    private Integer timeout = 600;
    @Autowired
    CaptchaService captchaService;
    @Autowired
    ApiService apiService;
    @Autowired
    private UUIDUtil uuidUtil;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    // 获取验证码
    @RequestMapping("/login/verifyCode")
    public Map<String, Object> loginVerifyCode() throws IOException {
        return captchaService.captchaCreator();
    }

    // 登录系统
    @RequestMapping("/api/login")
    public Map login(HttpServletRequest request){
        Map<String, String[]> maps = request.getParameterMap();
        Map<String, Object> loginResult = apiService.login(maps);

        return CustomResponse.formatResponse(loginResult);
    }

    // 获取用户信息
    @RequestMapping("/api/userInfo")
    public Map userInfo(@RequestHeader("Authorization") String authorization){
        Map<String, Object> loginResult = apiService.userInfo(authorization);

        return CustomResponse.formatResponse(loginResult);
    }

    // 退出登录
    @RequestMapping("/api/logout")
    public Map logout(@RequestHeader("Authorization") String authorization){
        Map<String, Object> loginResult = apiService.logout(authorization);

        return CustomResponse.formatResponse(loginResult);
    }
}
