package com.eactive.eai.rms.common.comDomain;

import java.util.HashMap;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
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 CommonDomainController extends OnlBaseAnnotationController {

	@Autowired
	private CommonDomainManService service;

	@Autowired
	private ObjectMapper objectMapper;

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

	@GetMapping(value = "/onl/admin/common/comDomainMan.view", params = "cmd=DETAIL")
	public String viewList() {
		return "/onl/admin/common/comDomainManDetail";
	}

	@PostMapping(value = "/onl/admin/common/comDomainMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<CommonDomainUI>> selectList(@SortDefault("domainNm") Pageable pageable,
			CommonDomainUI commonDomainUI) {
		Page<CommonDomainUI> page = service.selectList(pageable, commonDomainUI);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/admin/common/comDomainMan.json", params = "cmd=DETAIL")
	public ResponseEntity<CommonDomainUI> selectDetail(String domainNM) {
		CommonDomainUI commonDomainUI = service.selectDetail(domainNM);
		return ResponseEntity.ok(commonDomainUI);
	}

	@PostMapping(value = "/onl/admin/common/comDomainMan.json", params = "cmd=INSERT")
	public ResponseEntity<Void> insert(CommonDomainUI commonDomainUI) {
		service.insert(commonDomainUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/common/comDomainMan.json", params = "cmd=UPDATE")
	public ResponseEntity<Void> update(CommonDomainUI commonDomainUI) {
		service.update(commonDomainUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/common/comDomainMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(String domainNM) {
		service.delete(domainNM);
		return ResponseEntity.ok().build();
	}

	@RequestMapping(value = "/onl/admin/common/comDomainMan.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 = service.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);
	}

}
