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

import java.util.Collection;
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.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
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.b2bextractor.ReloadB2BExtractCommand;
import com.eactive.eai.agent.b2bextractor.RemoveB2BExtractCommand;
import com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.common.util.StringUtil;
import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.login.SessionManager;
import com.eactive.eai.rms.common.spring.LocaleMessage;
import com.eactive.eai.rms.common.util.CommonUtil;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.onl.common.exception.BizException;
import com.eactive.eai.rms.onl.common.exception.EMSRuntimeException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class B2bExtractorController extends OnlBaseAnnotationController {

	@Autowired
	private B2bExtractorManService service;

	@Autowired
	@Qualifier("localeMessage")
	private LocaleMessage localeMessage;

	@Autowired
	private ObjectMapper objectMapper;

	@GetMapping(value = "/onl/admin/service/b2bExtractorMan.view")
	public String view() {
		return "/onl/admin/service/b2bExtractorMan";
	}

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

	@RequestMapping(value = "/onl/admin/service/b2bExtractorMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<B2bExtractorUI>> selectList(Pageable pageable, String searchEaiSvcName) {
		Page<B2bExtractorUI> page = service.selectList(pageable, searchEaiSvcName);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@RequestMapping(value = "/onl/admin/service/b2bExtractorMan.json", params = "cmd=DETAIL")
	public ResponseEntity<B2bExtractorUI> selectDetail(String eaiSvcName, Integer svcPrcssNo) {
		B2bExtractorUI ui = service.selectDetail(eaiSvcName, svcPrcssNo);
		return ResponseEntity.ok(ui);
	}

	@RequestMapping(value = "/onl/admin/service/b2bExtractorMan.json", params = "cmd=INSERT")
	public ResponseEntity<Void> insert(B2bExtractorUI b2bExtractorUI) throws Exception {

		service.insert(b2bExtractorUI);

		CommonCommand command = new CommonCommand(ReloadB2BExtractCommand.class.getName(),
				b2bExtractorUI.getEaiSvcName());
		agentUtilService.broadcast(command);

		return ResponseEntity.ok().build();

	}

	@RequestMapping(value = "/onl/admin/service/b2bExtractorMan.json", params = "cmd=UPDATE")
	public ResponseEntity<Void> update(B2bExtractorUI b2bExtractorUI) throws Exception {

		service.update(b2bExtractorUI);
		CommonCommand command = new CommonCommand(ReloadB2BExtractCommand.class.getName(),
				b2bExtractorUI.getEaiSvcName());
		agentUtilService.broadcast(command);
		return ResponseEntity.ok().build();

	}

	@RequestMapping(value = "/onl/admin/service/b2bExtractorMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(String eaiSvcName, Integer svcPrcssNo) throws Exception {

		service.delete(eaiSvcName, svcPrcssNo);
		CommonCommand command = new CommonCommand(RemoveB2BExtractCommand.class.getName(), eaiSvcName + svcPrcssNo);
		agentUtilService.broadcast(command);

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

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

		return ResponseEntity.ok(map);
	}

	@RequestMapping(value = "/onl/admin/service/b2bExtractorMan.json", params = "cmd=INITIALIZE")
	public ResponseEntity<Map<String, Object>> initialize(String eaiSvcName) throws Exception {

		CommonCommand command = new CommonCommand(ReloadB2BExtractCommand.class.getName(), eaiSvcName);
		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);

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

		return ResponseEntity.ok(map);
	}

	@RequestMapping(value = "/onl/admin/service/b2bExtractorMan.excel", params = "cmd=LIST_GRID_TO_EXCEL")
	public String listGridToExcel(String searchEaiSvcName, Model model) {
		Collection<Map<String, Object>> datas = service.selectListToExcel(searchEaiSvcName);
		model.addAttribute("datas", datas);
		return "excelView";
	}

	@RequestMapping(value = "/onl/admin/service/b2bExtractorMan.json", params = "cmd=CLONETOPROD")
	public ModelAndView cloneToProd(HttpServletRequest request, @RequestParam HashMap<String, Object> paramMap)
			throws Exception {
		HashMap<String, Object> resultMap = new HashMap<>();
		String userId = SessionManager.getUserId(request);
		String cmDate = StringUtil.getNullStr(paramMap.get("CmDate"));
		String cmTime = StringUtil.getNullStr(paramMap.get("CmTime"));
		String serviceType = StringUtil.getNullStr(paramMap.get("serviceType"));
		String listSvcName = StringUtil.getNullStr(paramMap.get("listSvcName"));

		try {
			service.extractB2BExt(userId, listSvcName, cmDate, cmTime, serviceType);
			resultMap.put("message", "적용이 완료되었습니다.");
		} catch (BizException be) {
			logger.error(be.getMessage());
			throw new EMSRuntimeException(be.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new EMSRuntimeException(localeMessage.getString("errors.checklog"));
		}

		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
	}
}