package com.eactive.eai.rms.common.acl.monitoringCode;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.eactive.eai.rms.common.acl.monitoringCode.UI.MonitoringCodeGroupUI;
import com.eactive.eai.rms.common.acl.monitoringCode.UI.MonitoringCodeGroupUISearch;
import com.eactive.eai.rms.common.acl.monitoringCode.UI.MonitoringCodeSearchPOPUI;
import com.eactive.eai.rms.common.acl.monitoringCode.UI.MonitoringCodeUI;
import com.eactive.eai.rms.common.base.BaseAnnotationController;
import com.eactive.eai.rms.common.vo.GridResponse;

@Controller
public class MonitoringCodeManController extends BaseAnnotationController {

	@Autowired
	private MonitoringCodeManService monitoringCodeManService;

	@GetMapping(value = "/common/acl/monitoringCode/monitoringCodeMan.view")
	public void view() {
		// view
	}

	@GetMapping(value = "/common/acl/monitoringCode/monitoringCodeMan.view", params = "cmd=GROUP POPUP")
	public String viewGroupPopup() {
		return "/common/acl/monitoringCode/monitoringCodeManGroupPopup";
	}

	@GetMapping(value = "/common/acl/monitoringCode/monitoringCodeMan.view", params = "cmd=POPUP")
	public String viewPopup() {
		return "/common/acl/monitoringCode/monitoringCodeManPopup";
	}

	// adapterManDetail 대외기관 설정 검색 POPUP
	@GetMapping(value = "/common/acl/monitoringCode/monitoringCodeMan.view", params = "cmd=SEARCH POPUP")
	public String viewSelectPopList() {
		return "/common/acl/monitoringCode/monitoringCodeManSearchPopup";
	}

	@PostMapping(value = "/common/acl/monitoringCode/monitoringCodeMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<MonitoringCodeGroupUI>> selectList(
			MonitoringCodeGroupUISearch monitoringCodeGroupUISearch) {
		List<MonitoringCodeGroupUI> list = monitoringCodeManService.selectCodeGroupList(monitoringCodeGroupUISearch);
		return ResponseEntity.ok(new GridResponse<>(list));

	}

	@RequestMapping(value = "/common/acl/monitoringCode/monitoringCodeMan.json", params = "cmd=LIST_TREE")
	public ResponseEntity<GridResponse<MonitoringCodeUI>> selectCodeTreeList(
			@RequestParam(name = "nodeid", required = false) String codeGroup,
			MonitoringCodeGroupUISearch codeGroupUISearch) {

		if (!StringUtils.isBlank(codeGroup)) {
			return ResponseEntity.ok(new GridResponse<>(new ArrayList<>()));
		}
		List<MonitoringCodeUI> list = monitoringCodeManService.selectCodeTreeList(codeGroupUISearch);
		return ResponseEntity.ok(new GridResponse<>(list));
	}

	@PostMapping(value = "/common/acl/monitoringCode/monitoringCodeMan.json", params = "cmd=INSERT_GROUP")
	public ResponseEntity<String> insertGroup(MonitoringCodeGroupUI monitoringCodeGroupUI) {
		monitoringCodeManService.insertGroup(monitoringCodeGroupUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/common/acl/monitoringCode/monitoringCodeMan.json", params = "cmd=INSERT")
	public ResponseEntity<String> insert(MonitoringCodeUI monitoringCodeUI) {
		monitoringCodeManService.insert(monitoringCodeUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/common/acl/monitoringCode/monitoringCodeMan.json", params = "cmd=UPDATE_GROUP")
	public ResponseEntity<String> updateGroup(MonitoringCodeGroupUI monitoringCodeGroupUI) {
		monitoringCodeManService.updateGroup(monitoringCodeGroupUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/common/acl/monitoringCode/monitoringCodeMan.json", params = "cmd=UPDATE")
	public ResponseEntity<String> update(MonitoringCodeUI monitoringCodeUI) {
		monitoringCodeManService.update(monitoringCodeUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/common/acl/monitoringCode/monitoringCodeMan.json", params = "cmd=DELETE_GROUP")
	public ResponseEntity<String> deleteGroup(String cdGroup) {
		monitoringCodeManService.deleteGroup(cdGroup);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/common/acl/monitoringCode/monitoringCodeMan.json", params = "cmd=DELETE")
	public ResponseEntity<String> delete(MonitoringCodeUI monitoringCodeUI) {
		monitoringCodeManService.delete(monitoringCodeUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/common/acl/monitoringCode/monitoringCodeMan.json", params = "cmd=LIST_POP")
	public ResponseEntity<GridResponse<MonitoringCodeSearchPOPUI>> selectPopList(
			@SortDefault("id.code") Pageable pageable, MonitoringCodeSearchPOPUI monitoringCodePOPUI) {
		Page<MonitoringCodeSearchPOPUI> page = monitoringCodeManService.selectPopList(pageable, monitoringCodePOPUI);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

}
