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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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 org.springframework.web.servlet.ModelAndView;

import com.eactive.eai.rms.common.base.BaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class BizKeyTranController extends BaseAnnotationController {

	@Autowired
	private ObjectMapper objectMapper;

	@Autowired
	private BizKeyTranManService bizKeyTranManService;

	@Autowired
	@Qualifier("comboService")
	private ComboService comboService;

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

	@GetMapping(value = "/common/acl/bizKeyTran/bizKeyTranMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/common/acl/bizKeyTran/bizKeyTranManDetail";
	}

	@PostMapping(value = "/common/acl/bizKeyTran/bizKeyTranMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<BizKeyTranUI>> selectList(@SortDefault("eaibzwkdstcd") Pageable pageable,
			BizKeyTranUI bizKeyTranUI) {
		Page<BizKeyTranUI> page = bizKeyTranManService.selectList(pageable, bizKeyTranUI);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/common/acl/bizKeyTran/bizKeyTranMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo() {

		List<ComboVo> listEAICode = comboService.getFromBizCode("EAI");// 업무구분코드
		List<ComboVo> listApCode = comboService.getFromBizCode("APP");// AP코드

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("listEAICodeRows", listEAICode);
		resultMap.put("listApCodeRows", listApCode);
		return ResponseEntity.ok(resultMap);
	}

	@PostMapping(value = "/common/acl/bizKeyTran/bizKeyTranMan.json", params = "cmd=DETAIL")
	public ResponseEntity<BizKeyTranUI> selectDetail(String eaiBzwkDstcd) {
		BizKeyTranUI bizKeyTranUI = bizKeyTranManService.selectDetail(eaiBzwkDstcd);
		return ResponseEntity.ok(bizKeyTranUI);
	}

	@PostMapping(value = "/common/acl/bizKeyTran/bizKeyTranMan.json", params = "cmd=INSERT")
	public ResponseEntity<String> insert(BizKeyTranUI bizKeyTranUI) {
		bizKeyTranManService.insert(bizKeyTranUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/common/acl/bizKeyTran/bizKeyTranMan.json", params = "cmd=UPDATE")
	public ResponseEntity<String> update(BizKeyTranUI bizKeyTranUI) {
		bizKeyTranManService.update(bizKeyTranUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/common/acl/bizKeyTran/bizKeyTranMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(String eaiBzwkDstcd) {
		bizKeyTranManService.delete(eaiBzwkDstcd);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/common/acl/bizKeyTran/bizKeyTranMan.excel", params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcel(@RequestParam Map<String, Object> paramMap) throws JsonProcessingException {
		Map<String, Object> resultMap = new HashMap<>();

		String[] headers = objectMapper.readValue((String) paramMap.get("header"), String[].class);
		String[] headerTitles = objectMapper.readValue((String) paramMap.get("headerTitle"), String[].class);
		Map<String, Object>[] datas = bizKeyTranManService.selectListToExcel();
		Map<String, Object>[] lmerge = null;
		String merge = (String) paramMap.get("merge");

		if (merge != null) {
			lmerge = objectMapper.readValue(merge, new TypeReference<HashMap<String, Object>[]>(){
			});
		}
		resultMap.put("headers", headers);
		resultMap.put("headerTitles", headerTitles);
		resultMap.put("datas", datas);
		resultMap.put("fileName", paramMap.get("fileName"));
		resultMap.put("merge", lmerge);
		return new ModelAndView("excelView", resultMap);
	}
}
