package com.eactive.eai.rms.onl.manage.adapter.adapterchange;

import java.util.HashMap;
import java.util.List;
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.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.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.common.spring.BaseController;
import com.eactive.eai.rms.onl.common.service.AgentUtilService;

@Controller
public class AdapterChangeCommandController extends BaseController {
	
	@Autowired
	@Qualifier("adapterChangeService")
	private AdapterChangeService service;
	
	@Autowired
    @Qualifier("onlAgentUtilService")
    private AgentUtilService agentUtilService;
	
	@RequestMapping(value = "/adapter/change.do", params = {"cmd=LIST"})
	public ModelAndView adapterList(@RequestParam HashMap<String, Object> paramMap) throws Exception { 
		
		String serviceType = (String) paramMap.get("serviceType");
		
		try {
			DataSourceContextHolder.setDataSourceType(DataSourceTypeManager.getDataSourceType(serviceType));
		} catch (Exception e) {
			logger.error("Datasource Type Exception : " + e.getMessage());
			throw e;
		}
		
		// TEST 어댑터 사용유무  리스트로 가져오기 
		List<HashMap<String, Object>> list = service.selectChgCmdAdapterList(paramMap);
		
		HashMap<String, List<HashMap<String, Object>>> resultMap = new HashMap<>();
		
		resultMap.put("response", list);
		
		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
	}
	
	@RequestMapping(value = "/adapter/change.do", params = {"cmd=STATUS"})
	public ModelAndView adapterStatus(HttpServletRequest request, @RequestParam HashMap<String, Object> paramMap) throws Exception { 
		
		String serviceType = (String) paramMap.get("serviceType");
		
		try {
			DataSourceContextHolder.setDataSourceType(DataSourceTypeManager.getDataSourceType(serviceType));
		} catch (Exception e) {
			logger.error("Datasource Type Exception : " + e.getMessage());
			throw e;
		}
		
		List<HashMap<String, Object>> test_statusList = service.getLocalStatusList(paramMap, "TEST");
		
		HashMap<String, List<HashMap<String, Object>>> resultMap = new HashMap<>();
		
		resultMap.put("response",  test_statusList);
		
		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
	}

	
	/**
	 * adapter change update & reload 
	 * 
	 * 1. DB update
	 * 2. send Command
	 */
	@RequestMapping(value = "/adapter/change.do", params = {"cmd=RELOAD_SEND_COMMAND"})
	public ModelAndView adapterChange(HttpServletRequest request, HttpServletResponse response, @RequestParam HashMap<String, String> paramMap) throws Exception {
		
		String serviceType = (String) paramMap.get("serviceType");
		
		try {
			DataSourceContextHolder.setDataSourceType(DataSourceTypeManager.getDataSourceType(serviceType));
		} catch (Exception e) {
			logger.error("Datasource Type Exception : " + e.getMessage());
			throw e;
		}
		
		Map<String, String> resultMap = new HashMap<>();
		String adapterGroupName = paramMap.get("ADPTRBZWKGROUPNAME");

		try {
			HashMap<String, String> queryParam = new HashMap<>();
			queryParam.put("ADPTRBZWKGROUPNAME", paramMap.get("ADPTRBZWKGROUPNAME"));
			queryParam.put("ADPTRUSEYN", paramMap.get("TESTUSEYN"));
			service.updateChangeAdapter(queryParam);
			
		} catch (Exception e) {
			String message = "fail TEST adapter change database update : " + e.getMessage();
			logger.error(message);
			resultMap.put("response", AdapterChangeConstants.FAIL_UPDATE);
			resultMap.put("message", message);
			
			ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
			return modelAndView;
		}
		
		try {
			
			String reloadAdapter = service.reloadAdapter(adapterGroupName);
			
		} catch (Exception e) {
			String message = "fail TEST adapter change send command : " + e.getMessage();
			logger.error(message);
			resultMap.put("response", AdapterChangeConstants.FAIL_RELOAD);
			resultMap.put("message", message);
			
			ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
			return modelAndView;
		}
		
		resultMap.put("response", AdapterChangeConstants.SUCCESS);
		resultMap.put("message", "adapter change success");
		
		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
		
	}
	
}
