package com.eactive.eai.rms.onl.manage.serviceinfo.b2badaptermap;

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

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

import org.springframework.beans.factory.annotation.Autowired;
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.validation.BindingResult;
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.agent.b2badaptermapping.ReloadB2BAdapterMapCommand;
import com.eactive.eai.agent.b2badaptermapping.RemoveB2BAdapterMapCommand;
import com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.util.CommonUtil;
import com.eactive.eai.rms.common.util.ControllerValidationHelper;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.data.entity.onl.b2badaptermap.B2bAdapterMapSearch;
import com.google.gson.Gson;

@Controller
public class B2bAdapterMapController extends OnlBaseAnnotationController {

	private B2bAdapterMapManService service;
	
	@Autowired
	public  B2bAdapterMapController(B2bAdapterMapManService service) {
		this.service = service;
	}

	@GetMapping(value = "/onl/admin/service/b2bAdapterMapMan.view")
	public void viewList() {
	}

	@GetMapping(value = "/onl/admin/service/b2bAdapterMapMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/admin/service/b2bAdapterMapManDetail";
	}

	@PostMapping(value = "/onl/admin/service/b2bAdapterMapMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<B2bAdapterMapUI>> selectList(Pageable pageable, B2bAdapterMapSearch searchUI) {
		Page<B2bAdapterMapUI> uiPage = service.selectList(pageable, searchUI);
		return ResponseEntity.ok(new GridResponse<>(uiPage));
	}

	@PostMapping(value = "/onl/admin/service/b2bAdapterMapMan.json", params = "cmd=DETAIL")
	public ResponseEntity<B2bAdapterMapUI> selectDetail(B2bAdapterMapUI b2bAdapterMapUI) {
		B2bAdapterMapUI uiVo = service.selectDetail(b2bAdapterMapUI);
		return ResponseEntity.ok(uiVo);
	}

	@PostMapping(value = "/onl/admin/service/b2bAdapterMapMan.json", params = "cmd=INSERT")
	public ResponseEntity<?> insert(@Valid B2bAdapterMapUI uiVo, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			return ControllerValidationHelper.checkValidation(bindingResult);
		}
		service.insert(uiVo);
		String reloadKey = uiVo.getEaiSvcName();
		CommonCommand command = new CommonCommand(ReloadB2BAdapterMapCommand.class.getName(), reloadKey);
		HashMap<String, Object> commandResult = agentUtilService.broadcast(command);
		return ResponseEntity.ok(commandResult);
	}

	@RequestMapping(value = "/onl/admin/service/b2bAdapterMapMan.json", params = "cmd=UPDATE")
	public ResponseEntity<?> update(HttpServletRequest request, HttpServletResponse response, @Valid B2bAdapterMapUI uiVo, BindingResult bindingResult)
			throws Exception {
		if (bindingResult.hasErrors()) {
			return ControllerValidationHelper.checkValidation(bindingResult);
		}
		service.update(uiVo);
		String reloadKey = uiVo.getEaiSvcName();
		CommonCommand command = new CommonCommand(ReloadB2BAdapterMapCommand.class.getName(), reloadKey);
		HashMap<String, Object> commandResult = agentUtilService.broadcast(command);
		return ResponseEntity.ok(commandResult);
	}

	@RequestMapping(value = "/onl/admin/service/b2bAdapterMapMan.json", params = "cmd=DELETE")
	public ResponseEntity<?> delete(B2bAdapterMapUI b2bAdapterMapUI) throws Exception  {
		service.delete(b2bAdapterMapUI);
		CommonCommand command = new CommonCommand(RemoveB2BAdapterMapCommand.class.getName(), b2bAdapterMapUI.getEaiSvcName());
		HashMap<String, Object> commandResult = agentUtilService.broadcast(command);
		return ResponseEntity.ok(commandResult);
	}

	@RequestMapping(value = "/onl/admin/service/b2bAdapterMapMan.json", params = "cmd=LIST_DETAIL_COMBO")
	public ModelAndView selectListDetailCombo() {
		Map<String, List<?>> map = service.selectListDetailCombo();
		return new ModelAndView("jsonView", map);
	}

	@RequestMapping(value = "/onl/admin/service/b2bAdapterMapMan.json", params = "cmd=LIST_DYNAMIC_COMBO")
	public ModelAndView selectListDynamicCombo(String psvSysAdptrBzwkGroupName)  {
		Map<String, List<?>> map = service.selectListDynamicCombo(psvSysAdptrBzwkGroupName);
		return new ModelAndView("jsonView", map);
	}

	@RequestMapping(value = "/onl/admin/service/b2bAdapterMapMan.json", params = "cmd=INITIALIZES")
	public ModelAndView initializes(HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonCommand command = new CommonCommand(ReloadB2BAdapterMapCommand.class.getName(), "ALL");
		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);
		Map<String, String> map = new HashMap<>();
		map.put("message", CommonUtil.getMapString(returnMap, true));

		return  new ModelAndView("jsonView", map);
	}

	@RequestMapping(value = "/onl/admin/service/b2bAdapterMapMan.json", params = "cmd=INITIALIZE")
	public ModelAndView initialize(HttpServletRequest request, HttpServletResponse response, String eaiSevrInstncName,
			String eaiSvcName, String extnlInstiIdnfiName, String dmndDtalsBzwkDsticName) throws Exception {
		String reloadKey = eaiSvcName;
		CommonCommand command = new CommonCommand(ReloadB2BAdapterMapCommand.class.getName(), reloadKey);
		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);

		HashMap<String, String> map = new HashMap<>();
		map.put("message", CommonUtil.getMapString(returnMap, true));

		return  new ModelAndView("jsonView", map);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/onl/admin/service/b2bAdapterMapMan.excel", params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcel(B2bAdapterMapSearch uiSearch,
			@RequestParam Map<String, Object> paramMap) throws Exception {
		Map<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();
		Map<String, List<?>> list = service.selectListToExcel(uiSearch);

		String jsonStr = gson.toJson(list.get("list"));
		Map<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);

		Map<String, Object>[] lmerge = null;
		String merge = (String) paramMap.get("merge");
		if (merge != null) {
			lmerge = gson.fromJson(merge, HashMap[].class);
		}

		resultMap.put("headers", headers);
		resultMap.put("headerTitles", headerTitles);
		resultMap.put("datas", list2);
		resultMap.put("fileName", paramMap.get("fileName"));
		resultMap.put("merge", lmerge);

		return new ModelAndView("excelView", resultMap);
	}

}