package com.eactive.eai.rms.onl.manage.serviceinfo.eaimsg;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.common.util.StringUtil;
import com.eactive.eai.rms.common.spring.BaseController;
import com.eactive.eai.rms.common.spring.LocaleMessage;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.onl.common.exception.BizException;

@Controller
@SuppressWarnings({"rawtypes", "unchecked"})
public class EaiMsgCmFtpController extends BaseController {
	private static final Logger logger = Logger.getLogger(EaiMsgCmFtpController.class);
	
	@Autowired
	@Qualifier("localeMessage")
	private LocaleMessage localeMessage;
	
	@Autowired
	@Qualifier("eaiMsgCmFtpService")
	private EaiMsgCmFtpService service;
	
	@RequestMapping(value="/onl/admin/service/eaiMsgCmFtp.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/service/eaiMsgCmFtp.json", params = "cmd=LIST_LOCAL_FILES")
	public ModelAndView localFileList(HttpServletRequest request) throws Exception {
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		
		List<HashMap> cmList = new ArrayList<HashMap>();
		try {
			String[] fileList = service.getLocalFileList();
			if(fileList != null){ //취약점소스 수정 2020.08.27
				for (String name : fileList) {
					HashMap map = new HashMap();
					map.put("FILE_NM", name);
					cmList.add(map);
				}
			}
			resultMap.put("cmList", cmList);
		} catch (BizException be) {
			logger.error(be.getMessage(),be);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new Exception(localeMessage.getString("errors.checklog"));
		}
		
		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
	}
	
	@RequestMapping(value= "/onl/admin/service/eaiMsgCmFtp.json", params = "cmd=DETAIL_LOCAL_FILE")
	public ModelAndView detailLocalFile(HttpServletRequest request, @RequestParam HashMap paramMap) throws Exception {
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			String fileName = (String)paramMap.get("fileName");
			Map detail = service.getLocalFileDetail(fileName);
			resultMap.put("Detail", detail);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new Exception(localeMessage.getString("errors.checklog"));
		}
		
		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
	}
	
	@RequestMapping(value= "/onl/admin/service/eaiMsgCmFtp.json", params = "cmd=LIST_FTP_FILES")
	public ModelAndView ftpFileList(HttpServletRequest request) throws Exception {
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		
		List<HashMap> ftpList = new ArrayList<HashMap>();
		try {
			String[] fileList = service.getFtpFileList();
			if (fileList != null) {
				for (String name : fileList) {
					HashMap map = new HashMap();
					String[] s = name.split("_");
					Calendar cal = Calendar.getInstance();
					cal.setTime(new SimpleDateFormat("yyyyMMddHHmmss").parse(s[0]));
					
					map.put("FILE_NM", name);
					map.put("DATE", (cal.get(Calendar.YEAR) +"년  "
						+ String.format("%02d", cal.get(Calendar.MONTH)+1) +"월 "
						+ String.format("%02d", cal.get(Calendar.DAY_OF_MONTH)) +"일 "
						+ String.format("%02d", cal.get(Calendar.HOUR_OF_DAY)) +"시 "
						+ String.format("%02d", cal.get(Calendar.MINUTE)) +"분 "
						+ String.format("%02d", cal.get(Calendar.SECOND)) +"초"));
					map.put("USER", s[1]);
					map.put("SYSTEM", s[2]);
					ftpList.add(map);
				}
				resultMap.put("ftpList", ftpList);
			}
		} catch (BizException be) {
			logger.error(be.getMessage(),be);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new Exception(localeMessage.getString("errors.checklog"));
		}
		
		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
	}
	
	@RequestMapping(value= "/onl/admin/service/eaiMsgCmFtp.json", params = "cmd=DETAIL_FTP_FILE")
	public ModelAndView detailFtpFile(HttpServletRequest request, @RequestParam HashMap paramMap) throws Exception {
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			String fileName = (String)paramMap.get("fileName");
			Map detail = service.getFtpFileDetail(fileName);
			resultMap.put("Detail", detail);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new Exception(localeMessage.getString("errors.checklog"));
		}
		
		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
	}
	
	@RequestMapping(value= "/onl/admin/service/eaiMsgCmFtp.json", params = "cmd=DELETE_FILES")
	public ModelAndView deleteFiles(HttpServletRequest request, @RequestParam HashMap<String,Object> paramMap) throws Exception {
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		String localFiles = StringUtil.getNullStr(paramMap.get("LocalFiles"));
		String ftpFiles = StringUtil.getNullStr(paramMap.get("FtpFiles"));
		
		try {
			boolean ret = service.deleteFiles(localFiles, ftpFiles);
			if (!ret) {
				throw new BizException("파일 삭제 중 에러가 발생하였습니다.");
			}
			resultMap.put("message", "성공적으로 삭제 되었습니다.");
		} catch (BizException be) {
			throw new Exception(be.getMessage(),be);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new Exception(localeMessage.getString("errors.checklog"));
		}
		
		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
	}
	
	@RequestMapping(value= "/onl/admin/service/eaiMsgCmFtp.json", params = "cmd=UPLOAD_FTP")
	public ModelAndView ftpUpload(HttpServletRequest request, @RequestParam HashMap<String,Object> paramMap) throws Exception {
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		String localFiles = StringUtil.getNullStr(paramMap.get("LocalFiles"));
		
		try {
			service.ftpUpload(localFiles);
			resultMap.put("message", "성공적으로 업로드 되었습니다.");
		}  catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new Exception(e.getMessage(),e);
		}
		
		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
	}
	

	
}
