package com.eactive.eai.rms.onl.audit.adapter;

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

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.RequestParam;

import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.context.MonitoringContext;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.onl.audit.adapter.ui.AdapterGroupAuditUI;
import com.eactive.eai.rms.onl.audit.adapter.ui.AdapterGroupAuditUIChanges;
import com.eactive.eai.rms.onl.manage.adapter.adapter.ui.AdapterGroupUISearch;

/**
 * 어뎁터 관리 화면 컨트롤러
 */
@Controller
public class AdapterAuditController extends OnlBaseAnnotationController {

//	@Autowired
//	private LocaleMessage localeMessage;
//
//	@Autowired
//	private MonitoringContext monitoringContext;
	
	@Autowired
	private AdapterAuditService auditService;

	@Autowired
	private ComboService comboService;

	@Autowired
	private MonitoringContext monitoringContextImpl;

	@GetMapping(value = "/onl/admin/adapter/adapterAuditMan.view")
	public String view() {
		return "/onl/admin/audit/adapter/adapterAuditMan";
	}

	@GetMapping(value = "/onl/admin/adapter/adapterAuditMan.view", params = "cmd=DETAIL")
	public String viewDetail() {
		return "/onl/admin/audit/adapter/adapterAuditManDetail";
	}

	@GetMapping(value = "/onl/admin/adapter/adapterAuditMan.view", params = "cmd=DETAIL*POPUP")
	public String viewDetailPopup() {
		return "/onl/admin/adapter/adapterAuditManDetailpopup";
	}

	@PostMapping(value = "/onl/admin/adapter/adapterAuditMan.json", params = "cmd=LIST_SEARCH_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> searchInitCombo() {

		List<ComboVo> listBzwkCode = comboService.getFromBusinessType();
		
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("bzwkCodeRows", listBzwkCode);
		
		return ResponseEntity.ok(resultMap);
	}
	
	@PostMapping(value = "/onl/admin/adapter/adapterAuditMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo() {

		List<ComboVo> listBiz = comboService.getFromBusinessType();
		List<ComboVo> listAdapterType = comboService.getFromAdapterType();
		List<ComboVo> listMsgType = comboService.getFromCode("MSG_TYPES");
		List<ComboVo> listMsgEncode = comboService.getFromCode("MSG_ENCODES");
		List<ComboVo> listInstance = comboService.getFromEAIServer();

		List<ComboVo> listAdptrIoDstcd = comboService.getFromCode("ADPTR_IO_DSTCD");
		List<ComboVo> listAdptrMsgPtrnCd = comboService.getFromCode("ADPTR_MSG_PTRN_CD");
		List<ComboVo> listUseYn = comboService.getFromCode("USE_YN");
		List<ComboVo> listYn = comboService.getFromCode("YN");
		List<ComboVo> listMonicondiSet = comboService.getFromCode("MONICONDI_SET");

		List<ComboVo> listYnNum = comboService.getFromCode("YN_NUM");
		List<ComboVo> listYnType = comboService.getFromCode("YN_TYPE");
		List<ComboVo> listTestMasterYn = comboService.getFromCode("TESTMASTER_YN");
		List<ComboVo> listInoutTypeCd = comboService.getFromCode("ADPT_INOUT_TYPE");

		String refClsName = monitoringContextImpl.getStringProperty(MonitoringContext.RMS_ADAPTER_REFCLSNAME);

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("bizRows", listBiz);
		resultMap.put("adapterTypeRows", listAdapterType);
		resultMap.put("msgTypeRows", listMsgType);
		resultMap.put("msgEncodeRows", listMsgEncode);
		resultMap.put("instanceRows", listInstance);

		resultMap.put("adptrIoDstcdRows", listAdptrIoDstcd);
		resultMap.put("adptrMsgPtrnCdRows", listAdptrMsgPtrnCd);
		resultMap.put("useYnRows", listUseYn);
		resultMap.put("ynRows", listYn);
		resultMap.put("monicondiSetRows", listMonicondiSet);
		resultMap.put("ynNumRows", listYnNum);
		resultMap.put("ynTypeRows", listYnType);
		resultMap.put("refClsName", refClsName);
		resultMap.put("testMasterYnRows", listTestMasterYn);
		resultMap.put("inoutTypeRows", listInoutTypeCd);

		return ResponseEntity.ok(resultMap);
	}

//	@GetMapping(value = "/onl/admin/adapter/adapterAuditSnapshotInit.json", params = "cmd=UPDATE")
//	public ResponseEntity<List<AdapterGroupAuditUI>> init(@RequestParam String adptrbzwkgroupname) {
//		this.auditService.saveInitSnapshot(adptrbzwkgroupname);
//		return this.selectList(adptrbzwkgroupname);
//	}
//
//	@GetMapping(value = "/onl/admin/adapter/adapterAuditAllList.json", params = "cmd=LIST")
//	public ResponseEntity<List<AdapterGroupAuditUI>> selectList(@RequestParam String adptrbzwkgroupname) {
//		List<AdapterGroupAuditUI> group = this.auditService.getAuditList(adptrbzwkgroupname);
//		return ResponseEntity.ok(group);
//	}

	@PostMapping(value = "/onl/admin/adapter/adapterAuditMan.json", params = "cmd=LIST")
	@GetMapping(value = "/onl/admin/adapter/adapterAuditList.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<AdapterGroupAuditUI>> selectListView(@SortDefault("adptrbzwkgroupname") Pageable pageable,
			AdapterGroupUISearch adapterGroupUISearch) {
		Page<AdapterGroupAuditUI> page = this.auditService.getAuditHistoryPaged(pageable, adapterGroupUISearch);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/admin/adapter/adapterAuditMan.json", params = "cmd=DETAIL")
	@GetMapping(value = "/onl/admin/adapter/adapterAuditDetail.json", params = "cmd=DETAIL")
	public ResponseEntity<AdapterGroupAuditUI> selectDetailView(
			@RequestParam long revNo, @RequestParam String adptrbzwkgroupname) {
		AdapterGroupAuditUI group = this.auditService.getAuditSnapshotAtRevision(revNo, adptrbzwkgroupname);
		return ResponseEntity.ok(group);
	}

	@PostMapping(value = "/onl/admin/adapter/adapterAuditMan.json", params = "cmd=CHANGE")
	@GetMapping(value = "/onl/admin/adapter/adapterAuditChange.json", params = "cmd=CHANGE")
	public ResponseEntity<AdapterGroupAuditUIChanges> selectDetailChangeParts(@RequestParam long revNo, @RequestParam String adptrbzwkgroupname) {
		AdapterGroupAuditUIChanges groupChanges = this.auditService.getAuditAtRevisionWithRevisionInfo(revNo, adptrbzwkgroupname);
		return ResponseEntity.ok(groupChanges);
	}

	@PostMapping(value = "/onl/admin/adapter/adapterAuditMan.json", params = "cmd=NEXT")
	@GetMapping(value = "/onl/admin/adapter/adapterAuditNext.json", params = "cmd=NEXT")
	public ResponseEntity<AdapterGroupAuditUI> selectNext(@RequestParam long revNo, @RequestParam String adptrbzwkgroupname) {
		AdapterGroupAuditUI group = this.auditService.getNextAuditSnapshotAtRevision(revNo, adptrbzwkgroupname);
		return ResponseEntity.ok(group);
	}

	@PostMapping(value = "/onl/admin/adapter/adapterAuditMan.json", params = "cmd=PREV")
	@GetMapping(value = "/onl/admin/adapter/adapterAuditPrev.json", params = "cmd=PREV")
	public ResponseEntity<AdapterGroupAuditUI> selectPrev(@RequestParam long revNo, @RequestParam String adptrbzwkgroupname) {
		AdapterGroupAuditUI group = this.auditService.getPrevAuditSnapshotAtRevision(revNo, adptrbzwkgroupname);
		return ResponseEntity.ok(group);
	}


}
