package com.shanghaichengdi.ghjgitem.controller;

import com.alibaba.fastjson.JSONObject;
import com.shanghaichengdi.ghjgitem.constant.Constants;
import com.shanghaichengdi.ghjgitem.vo.response.ReturnMsg;
import com.shanghaichengdi.ghjgitem.service.RedisService;
import com.shanghaichengdi.ghjgitem.util.Encodes;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@RequestMapping("/test")
@Slf4j
public class TestController {

  @Resource
  private RedisService redisService;

  @GetMapping("/test/{token}")
  public ReturnMsg test(@PathVariable("token") String token) {
    String o = redisService.getValue(Constants.TOKEN + Constants.SPLIT, token);
    return ReturnMsg.success(o);
  }

  @PostMapping("/list")
  public ReturnMsg getList() {
    List<String> stringList = new ArrayList<>();
    stringList.add("test");
    stringList.add("test1");
    stringList.add(Constants.ENCODES_KEY);
    String jsonString = JSONObject.toJSONString(stringList);
    String encodedData = Encodes.encodeBase64(
        Encodes.aesEncrypt(jsonString.getBytes(StandardCharsets.UTF_8),
            Constants.ENCODES_KEY.getBytes(),
            Constants.ENCODES_KEY.getBytes()));
    return ReturnMsg.success("获取成功", encodedData);
  }
}
