package com.eactive.eai.rms.common.acl.iocm;

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

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.http.ResponseEntity;
import org.springframework.stereotype.Controller;
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.rms.common.acl.user.BizService;
import com.eactive.eai.rms.common.base.BaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.context.MonitoringContext;
import com.eactive.eai.rms.common.login.SessionManager;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.common.vo.JsonPageVo;
//import com.eactive.eai.rms.onl.manage.rule.aurora.AuroraUtil;
import com.google.gson.Gson;


@Controller
public class IocmProcController extends BaseAnnotationController {

	@Autowired
	@Qualifier("iocmService")
	private IocmProcService service;

	@Autowired
    @Qualifier("monitoringContext")
    private MonitoringContext monitoringContext;
	
	@Autowired
	private ComboService comboService;

	@Autowired
	@Qualifier("bizService")
    private BizService bizService;
	
	
	@RequestMapping(value = "/common/acl/iocm/iocmProcMan.view")
	public 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 = "/common/acl/iocm/iocmProcMan.json", params = "cmd=LIST")
	private ModelAndView selectList(HttpServletRequest request,
			JsonPageVo pageVo ,@RequestParam HashMap<String, Object> paramMap
			) throws Exception {
		HashMap map = service.selectList( pageVo.getStartNum(), pageVo.getEndNum(), paramMap);
		
		pageVo.setTotalCount((Integer)map.get("totalCount"));
		
		Map resultMap = new HashMap();
		resultMap.put("total"  , pageVo.getTotal());  //전체 페이지수
		resultMap.put("page"   , pageVo.getPage());   //현재 페이지수
		resultMap.put("records", pageVo.getTotalCount());//전체 레코드건수	
		resultMap.put("rows"   , map.get("list"));
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;
	}
	
	@PostMapping(value= "/common/acl/iocm/iocmProcMan.json",params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<ComboVo> chmnErrcdList = comboService.getMonitoringCodeSortedByIdCode("IOCM_CHMNERRCD");
		Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("chmnErrcdRows"		, chmnErrcdList);
		return ResponseEntity.ok(resultMap);
    }
	
	@RequestMapping(value= "/common/acl/iocm/iocmProcMan.excel",params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam HashMap<String,Object> paramMap) throws Exception {
		HashMap<String,Object> resultMap = new HashMap<String,Object>();

		String headersStr = (String) paramMap.get("headersStr");
		headersStr = headersStr.replace("CMDATE", "MODFIMGTMODFIYMS");
		headersStr = headersStr.substring(0, headersStr.length()-1);
		paramMap.put("headersStr", headersStr);
		
    	Gson gson = new Gson();
    	@SuppressWarnings("unchecked")
    	HashMap<String, Object> list = service.selectListToExcel(paramMap);
    	
    	String jsonStr = gson.toJson(list.get("list"));
    	HashMap<String, String>[] 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);
		
		HashMap<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", (String)paramMap.get("fileName"));
    	resultMap.put("merge", lmerge);
		ModelAndView modelAndView = new ModelAndView("excelView",resultMap);	
        return modelAndView;
	}	
	
	@RequestMapping(value= "/common/acl/iocm/iocmProcMan.json", params = "cmd=LIST_CM")
	public ModelAndView cmFileList(HttpServletRequest request, HttpServletResponse response,String eaiBzwkDstCd,String pkgNumber) throws Exception {
		String userId = SessionManager.getUserId(request);
		Properties prop = monitoringContext.getProperties();
		if (prop == null || prop.size()==0) {
			throw new Exception("프라퍼티 정보가 없습니다. - CM_CONFIG"); 
		}
		String auroraIni = (String)prop.getProperty(MonitoringContext.CM_AURORA_INI_PATH,"/fshome/greai/eaiadm/aurora/auroraApi_config.ini");

		Map resultMap = new HashMap();
//		try {
//			String groupCoCd = bizService.selectGroupCoCdByEAIBzwkDstCd(eaiBzwkDstCd);
//			AuroraUtil aurora = new AuroraUtil(auroraIni);
//			//List cmList = aurora.getCMList(userId, groupCoCd, eaiBzwkDstCd);
//			List cmFileList = new ArrayList();
//			if (userId != null && groupCoCd != null && eaiBzwkDstCd != null && pkgNumber != null) {
//				cmFileList = aurora.getCmFileList(userId, groupCoCd, eaiBzwkDstCd, pkgNumber);
//				
//			}
//			if (cmFileList == null || cmFileList.size() == 0) {
//				throw new BizException("해당 업무에 대한 파일 리스트가 없습니다.");
//			}
//			//resultMap.put("cmList", cmList);
//			resultMap.put("cmFileList", cmFileList);
//		} catch (BizException be) {
//			throw new Exception(be.getMessage());
//		} catch (Exception e) {
//			logger.error(e.getMessage());
//			throw new Exception();
//		}

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

}
