package com.eactive.eai.rms.onl.common.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
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.context.MonitoringContext;
import com.eactive.eai.rms.common.interceptor.InterceptorSkipController;

@Controller
public class ContextRefreshController implements InterceptorSkipController {

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

    private String view = "/common/jsp/rmsRefresh";
    private static final String resultView ="common/loadResult";
    @Autowired
    @Qualifier("monitoringContext")
    MonitoringContext monitoringContext;

    @RequestMapping("/common/context/rms/refresh.do")
    public String rmsContextRefresh(HttpServletRequest request) {
        try {
            monitoringContext.refresh();
            request.getSession().setAttribute("After.Refresh.Properties",monitoringContext.getProperties());
        } catch ( Exception e ) {
            logger.error("monitoring context reload fail !" );
        }
        return view;
    }
    @RequestMapping("/refresh.do")
    public ModelAndView rmsContextRefreshing(HttpServletRequest request) {
    	HashMap<String,String> result = new HashMap<String,String>();
        try {
            monitoringContext.refresh();
          
        	result.put("status", "success");
        } catch ( Exception e ) {
            logger.error("monitoring context reload fail !" );
        	result.put("status", "error");
        }
        return new ModelAndView(resultView, "result", result.toString());
    }    
    
    //프로퍼티명에 따른 메모리 리로드
    @RequestMapping(value="/refresh.do",params = "cmd=RELOAD_PRPTY")
    public ModelAndView reloadPrpty(HttpServletRequest request, @RequestParam(required = true) String key) {
    	HashMap<String,String> result = new HashMap<String,String>();
        try {

        	monitoringContext.refresh(key);
        	result.put("status", "success");
        } catch ( Exception e ) {
            logger.error("monitoring context reload fail !" );
        	result.put("status", "error");
        }
        return new ModelAndView(resultView, "result", result.toString());
    }   
    
    //모니터링 프로퍼티 중 큐에 대한 정보 메모리 Reload
    @RequestMapping(value="/refresh.do",params = "cmd=RELOAD_QUEUE_PRPTY")
    public ModelAndView reloadQueuePrpty(HttpServletRequest request, String serviceType) {
    	HashMap<String,String> result = new HashMap<String,String>();
        try {
        	//프로퍼티에 저장된 큐 개수를 가져온다. rms.queue.cnt.FEP
        	int queueSize = monitoringContext.getIntProperty(monitoringContext.RMS_QUEUE_COUNT+"."+serviceType,0);
        	for(int i = 1; i<=queueSize; i++){
        		try{
        			String num = "000"+String.valueOf(i);
        			String key = monitoringContext.RMS_FLOWCONTROL_QUEUE+"."+ serviceType + "." +num.substring(num.length()-3,num.length());
        			monitoringContext.refresh(key);
        		}catch(Exception ex){
        			logger.error("monitoring context reload fail ! ] " + ex.getMessage() );
        		}
        	}
        	result.put("status", "success");
        } catch ( Exception e ) {
            logger.error("monitoring context reload fail !" );
        	result.put("status", "error");
        }
        return new ModelAndView(resultView, "result", result.toString());
    }      
}
