package com.eactive.eai.rms.onl.manage.comm.unifbwk;

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.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
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 UnifbwkController extends OnlBaseAnnotationController {

	@Autowired
	private ObjectMapper objectMapper;

	@Autowired
	private UnifbwkManService unifbwkManService;

	@Autowired
	private ComboService comboService;

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

	@GetMapping(value = "/onl/admin/common/unifbwkMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/admin/common/unifbwkManDetail";
	}

	@PostMapping(value = "/onl/admin/common/unifbwkMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<UnifbwkUI>> selectList(@SortDefault("eaibzwkdstcd") Pageable pageable,
			@ModelAttribute UnifbwkUISearch searchParam) {
		Page<UnifbwkUI> page = unifbwkManService.selectList(pageable, searchParam);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/admin/common/unifbwkMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo() {
		List<ComboVo> listDiscardYn = comboService.getFromCode("SEARCH_YN");
		List<ComboVo> listInputCd = comboService.getFromCode("INPUTDSTCD");

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("ynRows", listDiscardYn);
		resultMap.put("inputDstCdRows", listInputCd);

		return ResponseEntity.ok(resultMap);
	}

	@PostMapping(value = "/onl/admin/common/unifbwkMan.json", params = "cmd=DETAIL")
	public ResponseEntity<UnifbwkUI> selectDetail(String eaiBzwkDstcd) {
		UnifbwkUI unifbwkUI = unifbwkManService.selectDetail(eaiBzwkDstcd);
		return ResponseEntity.ok(unifbwkUI);
	}

	@PostMapping(value = "/onl/admin/common/unifbwkMan.json", params = "cmd=INSERT")
	public ResponseEntity<Void> insert(UnifbwkUI unifbwkUI, BindingResult bindingResult) {
		unifbwkManService.insert(unifbwkUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/common/unifbwkMan.json", params = "cmd=UPDATE")
	public ResponseEntity<Void> update(UnifbwkUI unifbwkUI) {
		unifbwkManService.update(unifbwkUI);
		return ResponseEntity.ok().build();

	}

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

	@PostMapping(value = "/onl/admin/common/unifbwkMan.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 = unifbwkManService.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);
	}
}