package com.eactive.eai.rms.onl.manage.adapter.adaptertype;

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 org.springframework.web.servlet.ModelAndView;

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.vo.GridResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class AdapterTypeController extends OnlBaseAnnotationController {

	@Autowired
	private AdapterTypeManService adapterTypeManService;

	@Autowired
	private ComboService comboService;

	@Autowired
	private ObjectMapper objectMapper;

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

	@GetMapping(value = "/onl/admin/adapter/adapterTypeMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/admin/adapter/adapterTypeManDetail";
	}

	@PostMapping(value = "/onl/admin/adapter/adapterTypeMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<AdapterTypeUI>> selectList(@SortDefault("adptrcd") Pageable pageable,
			AdapterTypeUI adapterTypeUI) {
		Page<AdapterTypeUI> page = adapterTypeManService.selectList(pageable, adapterTypeUI);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/admin/adapter/adapterTypeMan.json", params = "cmd=DETAIL")
	public ResponseEntity<AdapterTypeUI> selectDetail(String adptrCd) {
		AdapterTypeUI adapterTypeUI = adapterTypeManService.selectDetail(adptrCd);
		return ResponseEntity.ok(adapterTypeUI);
	}

	@PostMapping(value = "/onl/admin/adapter/adapterTypeMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo() {
		List<ComboVo> listAdptrIoDstcd = comboService.getFromCode("ADPTR_IO_DSTCD");
		List<ComboVo> domainName = comboService.getFromDomainNm();

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("adptrIoDstcdRows", listAdptrIoDstcd);
		resultMap.put("domainName", domainName);
		return ResponseEntity.ok(resultMap);
	}

	@PostMapping(value = "/onl/admin/adapter/adapterTypeMan.json", params = "cmd=INSERT")
	public ResponseEntity<Void> insert(AdapterTypeUI adapterTypeUI) {
		adapterTypeManService.insert(adapterTypeUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/adapter/adapterTypeMan.json", params = "cmd=UPDATE")
	public ResponseEntity<Void> update(AdapterTypeUI adapterTypeUI) {
		adapterTypeManService.update(adapterTypeUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/adapter/adapterTypeMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(String adptrCd) {
		adapterTypeManService.delete(adptrCd);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/adapter/adapterTypeMan.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 = adapterTypeManService.selectListToExcel();

		HashMap<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);
	}

}
