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

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.user.BizService;
import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
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.onl.manage.rule.upload.IoMapLoaderForMonitoringService;

@Controller
@SuppressWarnings({"rawtypes", "unchecked"})
public class IocmRstController extends OnlBaseAnnotationController {

	@Autowired
	@Qualifier("localeMessage")
	private LocaleMessage localeMessage;
	
	@Autowired
	@Qualifier("bizService")
    private BizService bizService;
	
	@Autowired
	@Qualifier("monitoringContext")
	private MonitoringContext monitoringContext;
	
    @Autowired
    @Qualifier("ioMapLoaderForMonitoringService") 
    private IoMapLoaderForMonitoringService serviceMonitoring; 
	
	@RequestMapping(value="/onl/admin/rule/iocmRstMan.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= "/onl/admin/rule/iocmRstMan.json", params = "cmd=LIST")
	public ModelAndView selectList(HttpServletRequest request, String searchApCode, String searchPkgNumber, String searchStartDate, String searchEndDate) throws Exception {
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		
		List cmList = new ArrayList();
		try {
			if (searchApCode != null && searchPkgNumber != null && searchStartDate != null && searchEndDate != null) {
				cmList = serviceMonitoring.selectIocmRst(searchApCode, searchPkgNumber, searchStartDate, searchEndDate);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new Exception(localeMessage.getString("errors.checklog"));
		}
		resultMap.put("cmList", cmList);
		
		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
	}
	
	@RequestMapping(value= "/onl/admin/rule/iocmRstMan.json", params = "cmd=LIST_FILE")
	public ModelAndView fileList(HttpServletRequest request, String apCode, String pkgNumber) throws Exception {
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		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");

		List fileList = new ArrayList();
		try {
//			AuroraUtil aurora = new AuroraUtil(auroraIni);
//			String groupCoCd = bizService.selectGroupCoCdByEAIBzwkDstCd(apCode);
//			if (userId != null && groupCoCd != null && apCode != null && pkgNumber != null) {
//				fileList = aurora.getCmFileList(userId, groupCoCd, apCode, pkgNumber);
//			}
		} catch (Exception e) {
			logger.error("aurora error "+e.getMessage(),e);
			throw new Exception(localeMessage.getString("errors.checklog"));
		}
		resultMap.put("fileList", fileList);
		
		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
	}
}
