package com.eactive.eai.rms.onl.manage.rule.header;

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

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

import org.apache.log4j.Logger;
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.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
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.acl.bizKeyTran.BizKeyTranManService;
import com.eactive.eai.rms.common.acl.user.BizService;
import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.context.MonitoringContext;
import com.eactive.eai.rms.common.login.SessionManager;
import com.eactive.eai.rms.common.spring.LocaleMessage;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.common.vo.JsonPageVo;
import com.eactive.eai.rms.onl.manage.rule.header.vo.StandardMessageLayoutDeployVo;
import com.eactive.eai.rms.onl.manage.rule.header.vo.StandardMessageLayoutHistoryVo;
import com.eactive.eai.rms.onl.manage.rule.header.vo.StandardMessageLayoutItemVo;
import com.eactive.eai.rms.onl.manage.rule.header.vo.StandardMessageLayoutVo;
import com.eactive.eai.rms.onl.manage.rule.layoutsync.LayoutSyncService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class StandardLayoutController extends OnlBaseAnnotationController {

    private static final Logger logger = Logger.getLogger(StandardLayoutController.class);

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

    @Autowired
    @Qualifier("standardLayoutService")
    private StandardLayoutService service;
    
    @Autowired
    @Qualifier("layoutSyncService")
    private LayoutSyncService layoutSyncService;
	
	@Autowired
	@Qualifier("bizService")
    private BizService bizService;
	
	 @Autowired
    private ComboService comboService;      

	 @Autowired
	@Qualifier("bizKeyTranManService")
    private BizKeyTranManService bizKeyTranManService;
	 
	@RequestMapping(value={"/onl/admin/rule/standardLayoutMan.view", "/onl/admin/rule/standardLayoutHistoryMan.view"})
	private String viewList(HttpServletRequest request, @RequestParam(value = "cmd", required = false ) String cmd ) throws Exception {
		if (cmd == null || "LIST".equals(cmd.toUpperCase())){
			cmd = "";
		}else {
			cmd = CamelCaseUtil.converCamelCase(cmd);
		}
		String path = request.getServletPath();
		return path.split("[.]")[0]+cmd;
	}

	@RequestMapping(value={"/onl/admin/rule/standardLayoutMan.json"}, params = "cmd=LIST")
	private ModelAndView selectList(JsonPageVo pageVo, @RequestParam HashMap<String,Object> paramMap) throws Exception {
		Pageable pageable = PageRequest.of(pageVo.getPage() - 1, pageVo.getRows(), Sort.by("layoutName").ascending());
		Page<StandardMessageLayoutVo> page = service.selectList(pageable, paramMap);

		Map<String, Object> resultMap = new HashMap();
		resultMap.put("total", page.getTotalPages()); // 전체 페이지수
		resultMap.put("page", page.getNumber() + 1); // 현재 페이지수
		resultMap.put("records", page.getTotalElements());// 전체 레코드건수
		resultMap.put("rows", page.getContent());

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

	}

	@RequestMapping(value={"/onl/admin/rule/standardLayoutMan.json"}, params = "cmd=GET_DEPLOY")
	private ResponseEntity<StandardMessageLayoutDeployVo> selectDeploy() throws Exception {
		return ResponseEntity.ok(service.selectDeploy());
	}

	@RequestMapping(value={"/onl/admin/rule/standardLayoutHistoryMan.json"}, params = "cmd=LIST")
	private ModelAndView selectHistoryList(JsonPageVo pageVo , @RequestParam HashMap<String,Object> paramMap) throws Exception {
		Pageable pageable = PageRequest.of(pageVo.getPage() - 1, pageVo.getRows(), Sort.by("operationSeq").descending());

		Page<StandardMessageLayoutHistoryVo> page = service.selectHistoryList(pageable);

		Map resultMap = new HashMap();
		resultMap.put("total", page.getTotalPages()); // 전체 페이지수
		resultMap.put("page", page.getNumber() + 1); // 현재 페이지수
		resultMap.put("records", page.getTotalElements());// 전체 레코드건수
		resultMap.put("rows", page.getContent());

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

	}

	@RequestMapping(value={"/onl/admin/rule/standardLayoutMan.json"},params = "cmd=DETAIL")
	private ModelAndView selectDetail(HttpServletRequest request,
			HttpServletResponse response, String layoutName) throws Exception {
		
		Map map = service.selectDetail(layoutName);

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

	@RequestMapping(value={"/onl/admin/rule/standardLayoutHistoryMan.json"},params = "cmd=DETAIL")
	private ModelAndView selectHistoryDetail(HttpServletRequest request,
			HttpServletResponse response, String operationSeq) throws Exception {

		Map map = service.selectHistoryDetail(operationSeq);

		ModelAndView modelAndView = new ModelAndView("jsonView", map);

		return modelAndView;
	}

	@RequestMapping(value = "/onl/admin/rule/standardLayoutMan.json", params = "cmd=INSERT")
	private String insert(HttpServletRequest request, HttpServletResponse response,
			@RequestParam HashMap<String, Object> map, String gridData, StandardMessageLayoutVo standardMessageLayoutVo)
			throws Exception {
		
		List<StandardMessageLayoutItemVo> standardMessageLayoutItemVos = objectMapper.readValue(gridData,
				new TypeReference<List<StandardMessageLayoutItemVo>>() {
				});
		
		standardMessageLayoutVo.setStandardMessageLayoutItems(standardMessageLayoutItemVos);
		
		service.insert(map, SessionManager.getUserId(request), gridData, standardMessageLayoutVo);
		return null;

	}

	@Autowired
	private ObjectMapper objectMapper;

	@RequestMapping(value = "/onl/admin/rule/standardLayoutMan.json", params = "cmd=UPDATE")
	private String update(HttpServletRequest request, HttpServletResponse response,
			@RequestParam HashMap<String, Object> map, String gridData, StandardMessageLayoutVo standardMessageLayoutVo)
			throws Exception {

		List<StandardMessageLayoutItemVo> standardMessageLayoutItemVos = objectMapper.readValue(gridData,
				new TypeReference<List<StandardMessageLayoutItemVo>>() {
				});

		service.update(map, SessionManager.getUserId(request), gridData, standardMessageLayoutVo,
				standardMessageLayoutItemVos);
		return null;
	}

	@RequestMapping(value={"/onl/admin/rule/standardLayoutMan.json"},params = "cmd=DELETE")
	public String delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String,Object> map ) throws Exception {
		service.delete(map);
		return null;
	}

	@RequestMapping(value = { "/onl/admin/rule/standardLayoutMan.json" }, params = "cmd=DEPLOY")
	public String deploy(@RequestParam Map<String, Object> map, String layoutName) throws Exception {
		service.deploy(map, layoutName);
		return null;
	}
}
