package com.cool.bang.modules.system.controller;

import cn.shuibo.config.SecretKeyConfig;
import com.cool.bang.common.base.BaseController;
import com.cool.bang.common.constant.ControllerConstant;
import com.cool.bang.common.web.response.Result;
import com.wf.captcha.SpecCaptcha;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * login controller
 *
 * @author Peizhi Guo
 * 2021-2-8 14:58
 */
@RestController
@RequestMapping(ControllerConstant.GUEST)
public class LoginController extends BaseController {

    @Resource
    private SecretKeyConfig secretKeyConfig;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("me")
    public Authentication me(Authentication authentication) {
        return authentication;
    }

    @GetMapping("captcha")
    public Result captcha() {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        String verCode = specCaptcha.text().toLowerCase();
        String key = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(key, verCode, 5, TimeUnit.MINUTES);
        Map<String, Object> resultData = new HashMap<>();
        resultData.put("key", key);
        resultData.put("image", specCaptcha.toBase64());
        return success(resultData);
    }

    @GetMapping("pki")
    public Result pki() {
        return success("success", secretKeyConfig.getPublicKey());
    }

}
