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

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

import com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.agent.routing.ReloadRoutingCommand;
import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.data.entity.onl.routing.RoutingSearch;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class RoutingController extends OnlBaseAnnotationController {

	private RoutingManService routingManService;

	private ObjectMapper objectMapper;
	
	@Autowired
	public RoutingController(RoutingManService routingManService, ObjectMapper objectMapper) {
		this.routingManService = routingManService;
		this.objectMapper = objectMapper;
	}

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

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

	@PostMapping(value = "/onl/admin/common/routingMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<RoutingUI>> selectList(@SortDefault("routname") Pageable pageable,
			RoutingSearch routingSearch) {

		Page<RoutingUI> page = routingManService.selectList(pageable, routingSearch);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/admin/common/routingMan.json", params = "cmd=DETAIL")
	public ResponseEntity<RoutingUI> selectDetail(String routName) {
		RoutingUI routingUI = routingManService.selectDetail(routName);
		return ResponseEntity.ok(routingUI);
	}

	@PostMapping(value = "/onl/admin/common/routingMan.json", params = "cmd=INSERT")
	public ResponseEntity<Void> insert(RoutingUI routingUI) throws Exception {

		routingManService.insert(routingUI);
		CommonCommand command = new CommonCommand(ReloadRoutingCommand.class.getName(), routingUI.getRoutName());
		agentUtilService.broadcast(command);

		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/common/routingMan.json", params = "cmd=UPDATE")
	public ResponseEntity<Void> update(RoutingUI routingUI) throws Exception {

		routingManService.update(routingUI);
		CommonCommand command = new CommonCommand(ReloadRoutingCommand.class.getName(), routingUI.getRoutName());
		agentUtilService.broadcast(command);

		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/common/routingMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(RoutingUI routingUI) throws Exception {
		
		routingManService.delete(routingUI.getRoutName());
		CommonCommand command = new CommonCommand(ReloadRoutingCommand.class.getName(), routingUI.getRoutName());
		agentUtilService.broadcast(command);
		
		return ResponseEntity.ok().build();
	}

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