package cokr.xit.ens.modules.example.api;

import cokr.xit.ens.core.utils.CmmnUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class ServerController {


	@PostMapping("/example/server/ok")
	private ResponseEntity<Map<String, Object>> ok(@RequestBody Map<String, Object> reqDTO ) {
		
		log.info(String.format("called %s.%s", getClass().getName(), new Object() {}.getClass().getEnclosingMethod().getName()));
		log.info(String.format("request parameters... %s", CmmnUtil.toJsonString(reqDTO)));
		
		ResponseEntity<Map<String, Object>> resp = new ResponseEntity<Map<String,Object>>(this.createBody(), HttpStatus.OK);

		return resp;
	}
	@PostMapping("/example/server/err400")
	private ResponseEntity<Map<String, Object>> err400(@RequestBody Map<String, Object> reqDTO ) {
		
		log.info(String.format("called %s.%s", getClass().getName(), new Object() {}.getClass().getEnclosingMethod().getName()));
		log.info(String.format("request parameters... \n%s", CmmnUtil.toJsonString(reqDTO)));
		
		ResponseEntity<Map<String, Object>> resp = new ResponseEntity<Map<String,Object>>(this.createBody(), HttpStatus.BAD_REQUEST);

		return resp;
		
	}
	@PostMapping("/example/server/err500")
	private ResponseEntity<Map<String, Object>> err500(@RequestBody Map<String, Object> reqDTO ) {
		
		log.info(String.format("called %s.%s", getClass().getName(), new Object() {}.getClass().getEnclosingMethod().getName()));
		log.info(String.format("request parameters... %s", CmmnUtil.toJsonString(reqDTO)));
		
		ResponseEntity<Map<String, Object>> resp = new ResponseEntity<Map<String,Object>>(this.createBody(), HttpStatus.INTERNAL_SERVER_ERROR);
		
		return resp;
		
	}
	
	private Map<String, Object> createBody() {
		Map<String, Object> data = new HashMap<>();
		data.put("param1", 123);
		data.put("param2", "abc");
		data.put("param3", 10L);

		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("data", data);
		return resultMap;
	}
}
