package com.icontrols.oauth.controller;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.icontrols.oauth.utils.JWTUtils;

@RestController
@RequestMapping(value = "/api/oauth2")
public class ViewController {
	private static final Logger logger = LoggerFactory.getLogger(ViewController.class);

	@Autowired
	RedisTemplate<String, Object> redisTemplate;

	// redis 테스트
	@RequestMapping(value = "/check", method = RequestMethod.GET)
	public String validTest(@RequestParam(value = "token", required = false) String token)
			throws UnsupportedEncodingException, NoSuchAlgorithmException, GeneralSecurityException {

		ValueOperations<String, Object> vop = redisTemplate.opsForValue();
		Random generator = new Random();
		String wallpadCode = "";
		for (int i = 0; i < 6; i++) {
			wallpadCode += Integer.toString(generator.nextInt(10));
		}
		while (!vop.setIfAbsent(wallpadCode, "value")) {
			wallpadCode = "";
			for (int i = 0; i < 6; i++) {
				wallpadCode += Integer.toString(generator.nextInt(10));
			}
		}

		return "";
	}
}
