/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.core.controller;

import com.crunii.micro.common.dto.Result;
import com.crunii.micro.core.dto.ApiDemo;
import com.crunii.micro.core.dto.ApiUserDemo;
import com.crunii.micro.core.service.Resilience4jTestService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author 田平 create 2023-02-15 02:20:24
 */
@Api(tags = "TestApi")
@RestController
@RequestMapping("/TestController")
@Slf4j
public class TestController {

	@Value("${spring.cloud.nacos.discovery.metadata.apiVersion}")
	private String apiVersion;

	@Autowired
	private Resilience4jTestService resilience4jTestService;

	@Resource(name = "noLogApiRestTemplate")
	private RestTemplate apiRestTemplate;

	@GetMapping(path = "/apiVersion")
	@ResponseBody
	public Result<String> apiVersion() {
		Result<String> result = new Result<>();
		result.setData(apiVersion);
		return result;
	}

	@GetMapping(path = "/test")
	@ResponseBody
	public Result<String> test() {
		Result<String> result = new Result<>();
		result.setData("CoreTest");
		return result;
	}

	@GetMapping(path = "/testFeignClient")
	@ResponseBody
	public Result<String> testFeignClient(@RequestHeader HttpHeaders headers) {
		log.info("{}", headers);
		Result<String> result = new Result<>();
		try {
			TimeUnit.SECONDS.sleep(2);
		} catch (Exception e) {
		}
		result.setData("testFeignClient sleep 2s!");
		return result;
	}

	/**
	 * <pre>
	 *     短路器测试
	 * </pre>
	 *
	 * @param headers
	 * @return
	 */
	@GetMapping(path = "/testRate")
	@ResponseBody
	public Result<String> testRate(@RequestHeader HttpHeaders headers) {
		log.info("{}", headers);
		Result<String> result = new Result<>();
		String data = resilience4jTestService.testRate();
		result.setData(data);
		return result;
	}

	/**
	 * <pre>
	 *     短路器测试
	 * </pre>
	 *
	 * @param headers
	 * @return
	 */
	@GetMapping(path = "/testBreaker")
	@ResponseBody
	public Result<String> testBreaker(@RequestHeader HttpHeaders headers) {
		log.info("{}", headers);
		Result<String> result = new Result<>();
		String data = resilience4jTestService.testBreaker();
		result.setData(data);
		return result;
	}

	/**
	 * <pre>
	 *     TimeLimiter 测试
	 * </pre>
	 *
	 * @param headers
	 * @return
	 * @throws Exception
	 */
	@GetMapping(path = "/testTimeout")
	@ResponseBody
	public Result<String> testTimeout(@RequestHeader HttpHeaders headers) throws Exception {
		log.info("{}", headers);
		Result<String> result = new Result<>();
		String data = resilience4jTestService.testTimeout().toCompletableFuture().get();
		result.setData(data);
		return result;
	}

	@PostMapping(path = "/testBody")
	@ResponseBody
	public Result<String> testBody(@RequestBody @ApiParam ApiUserDemo user) {
		log.info("{}", user);
		Result<String> result = new Result<>();
		result.setData("CoreTest");
		return result;
	}

	@GetMapping(path = "/logTest")
	@ResponseBody
	public Result<String> logTest() {
		for (int i = 0; i < 1; i++) {
			apiRestTemplate.getForObject("http://core/TestController/test", String.class);
		}
		Result<String> result = new Result<>();
		result.setData("CoreTest");
		return result;
	}

	@GetMapping(path = "/test1")
	@ResponseBody
	public Result<Integer> test1(int id) {
		Result<Integer> result = new Result<>();
		result.setData(id);
		result.setMsg("asdjklllllllllllllllllllllllffffffffffffffffffffffffffffff"
				+ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
				+ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
				+ "fffffffffffffffffffffffffffffffffffffffffffl");
		return result;
	}

	@ApiOperation(value = "参数例子")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "username", value = "用户名"),
			@ApiImplicitParam(name = "password", value = "密码")
	})
	@RequestMapping(path = "/paramDemo", method = RequestMethod.POST)
	@ResponseBody
	public Result<ApiDemo> paramDemo(HttpServletResponse rsp, String username, String password) {
		rsp.addHeader("ticket", "abc123");
		Result<ApiDemo> result = new Result<>();
		return result;
	}

	@ApiOperation(value = "参数例子1")
	@PostMapping(path = "/paramDemo1")
	@ResponseBody
	public Result<ApiUserDemo> paramDemo1(@RequestBody @ApiParam ApiUserDemo user) {
		Result<ApiUserDemo> result = new Result<>();
		ApiUserDemo userDemo = new ApiUserDemo();
		userDemo.setUsername(user.getUsername());
		userDemo.setPassword("密码");
		userDemo.setLoginTime(new Date());
		result.setData(userDemo);
		return result;
	}

	@PostMapping(path = "/testNoBody")
	@ResponseBody
	public void testNoBody(@RequestBody @ApiParam ApiUserDemo user) {
		log.info("{}", user);
	}

	public static void main(String[] args) {
		System.out.println(1231);
	}
}
