package com.eactive.eai.rms.onl.manage.control.log;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.http.ResponseEntity;
import org.springframework.stereotype.Controller;
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.google.gson.Gson;

@Controller
public class ControlLogManController extends OnlBaseAnnotationController {

	@Autowired
	@Qualifier("bzwkGroupControlLogManService")
	private ControlLogManService bzwkGroupService;

	@Autowired
	@Qualifier("serviceGroupControlLogManService")
	private ControlLogManService serviceGroupService;

	@Autowired
	@Qualifier("instiGroupControlLogManService")
	private ControlLogManService instiGroupService;

	@RequestMapping(value = "/onl/admin/control/controlBzwkMan.view")
	public String viewListGroupByBzwk() {
		return "/onl/admin/control/controlBzwkMan";
	}

	@RequestMapping(value = "/onl/admin/control/controlBzwkMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<ControlLogManUI>> selectListGroupByBzwk(HttpServletRequest request,
			Pageable pageable, ControlLogManUISearch uiSearch) {
		Page<ControlLogManUI> pageUI = bzwkGroupService.selectListGroupBy(pageable, uiSearch);
		return ResponseEntity.ok(new GridResponse<ControlLogManUI>(pageUI));

	}

	@RequestMapping(value = "/onl/admin/control/controlBzwkMan.excel", params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcelGroupByBzwk(HttpServletRequest request, HttpServletResponse response,
			ControlLogManUISearch uiSearch, @RequestParam Map<String, Object> paramMap) {
		Map<String, Object> list = bzwkGroupService.selectListToExcelGroupBy(uiSearch);

		HashMap<String, Object> resultMap = setExcelResult(paramMap, list);
		return new ModelAndView("excelView", resultMap);
	}

	@RequestMapping(value = "/onl/admin/control/controlServiceMan.view")
	public String viewListGroupByService() {
		return "/onl/admin/control/controlServiceMan";
	}

	@RequestMapping(value = "/onl/admin/control/controlServiceMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<ControlLogManUI>> selectListGroupByService(HttpServletRequest request,
			Pageable pageable, ControlLogManUISearch uiSearch) {
		Page<ControlLogManUI> pageUI = serviceGroupService.selectListGroupBy(pageable, uiSearch);
		return ResponseEntity.ok(new GridResponse<ControlLogManUI>(pageUI));
	}

	@RequestMapping(value = "/onl/admin/control/controlServiceMan.excel", params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcelGroupByService(HttpServletRequest request, HttpServletResponse response,
			ControlLogManUISearch uiSearch, @RequestParam Map<String, Object> paramMap) {
		Map<String, Object> list = serviceGroupService.selectListToExcelGroupBy(uiSearch);

		HashMap<String, Object> resultMap = setExcelResult(paramMap, list);
		return new ModelAndView("excelView", resultMap);
	}

	@RequestMapping(value = "/onl/admin/control/controlInstiMan.view")
	public String viewListGroupByInsti() {
		return "/onl/admin/control/controlInstiMan";
	}

	@RequestMapping(value = "/onl/admin/control/controlInstiMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<ControlLogManUI>> selectListGroupByInsti(HttpServletRequest request,
			Pageable pageable, ControlLogManUISearch uiSearch) {
		Page<ControlLogManUI> pageUI = instiGroupService.selectListGroupBy(pageable, uiSearch);
		return ResponseEntity.ok(new GridResponse<ControlLogManUI>(pageUI));
	}

	@RequestMapping(value = "/onl/admin/control/controlInstiMan.excel", params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcelGroupByInsti(HttpServletRequest request, HttpServletResponse response,
			ControlLogManUISearch uiSearch, @RequestParam Map<String, Object> paramMap) {
		Map<String, Object> list = instiGroupService.selectListToExcelGroupBy(uiSearch);

		HashMap<String, Object> resultMap = setExcelResult(paramMap, list);
		return new ModelAndView("excelView", resultMap);
	}

	@SuppressWarnings("unchecked")
	private HashMap<String, Object> setExcelResult(Map<String, Object> paramMap, Map<String, Object> list) {
		HashMap<String, Object> resultMap = new HashMap<>();

		String headersStr = (String) paramMap.get("headersStr");
		headersStr = headersStr.substring(0, headersStr.length() - 1);
		paramMap.put("headersStr", headersStr);

		Gson gson = new Gson();

		String jsonStr = gson.toJson(list.get("list"));
		HashMap<String, Object>[] list2 = gson.fromJson(jsonStr, HashMap[].class);

		String[] headers = gson.fromJson((String) paramMap.get("header"), String[].class);
		String[] headerTitles = gson.fromJson((String) paramMap.get("headerTitle"), String[].class);

		resultMap.put("headers", headers);
		resultMap.put("headerTitles", headerTitles);
		resultMap.put("datas", list2);
		resultMap.put("fileName", paramMap.get("fileName"));
		return resultMap;
	}

}