package com.eactive.eai.rms.kakao.rolling;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.kakao.rolling.RollingSuffix;

@Profile("rolling")
@Controller
public class RollingSuffixController extends OnlBaseAnnotationController {

	@Autowired
	private RollingSuffixManService rollingSuffixManService;

	@GetMapping(value = "/onl/admin/rule/rollingSuffixMan.view")
	public void view() {
		// view
	}

	@PostMapping(value = "/onl/admin/rule/rollingSuffixMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<RollingSuffix>> selectList(
			@SortDefault(sort = { "id", "createdDate" }, direction = Sort.Direction.DESC) Pageable pageable) {
		Page<RollingSuffix> page = rollingSuffixManService.selectList(pageable);
		return ResponseEntity.ok(new GridResponse<>(page));
	}
	
	@PostMapping(value={"/onl/admin/rule/rollingSuffixMan.json"}, params = "cmd=FIND_CURRENT_COMMITID")
	public ResponseEntity<Map<String, String>> findCommitId() throws Exception {
		String commitId = rollingSuffixManService.findCommitId();
		Map<String, String> response = new HashMap<>();
		response.put("commitId", commitId);
		return ResponseEntity.ok(response);
	}
	
	@PostMapping(value={"/onl/admin/rule/rollingSuffixMan.json"}, params = "cmd=UPDATE")
    public ResponseEntity<Map<String, String>> insertAndUpdateCommitId(@RequestParam String newCommitId) {
		rollingSuffixManService.insertAndUpdateCommitId(newCommitId);
        Map<String, String> response = new HashMap<>();
        response.put("message", "rollingSuffix and current commitId change successful");
        return ResponseEntity.ok(response);
    }
	
	@PostMapping(value={"/onl/admin/rule/rollingSuffixMan.json"}, params = "cmd=UPDATE_SUFFIX")
	public ResponseEntity<Map<String, String>> insertRollingSuffix(@RequestParam String newCommitId) {
		String suffix = rollingSuffixManService.insertRollingSuffix(newCommitId);
		Map<String, String> response = new HashMap<>();
		response.put("suffix", suffix);
		return ResponseEntity.ok(response);
	}
	
	@PostMapping(value={"/onl/admin/rule/rollingSuffixMan.json"}, params = "cmd=UPDATE_CURRENT_COMMITID")
    public ResponseEntity<Map<String, String>> changeCurrentCommitId(@RequestParam String newCommitId) {
		rollingSuffixManService.changeCurrentCommitId(newCommitId);
        Map<String, String> response = new HashMap<>();
        response.put("message", "current commitId change successful");
        return ResponseEntity.ok(response);
    }
}