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

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
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.seed.Seed;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.onl.manage.comm.property.PropertyGroupUI;
import com.eactive.eai.rms.onl.manage.comm.property.PropertyManService;
import com.eactive.eai.rms.onl.manage.comm.property.PropertyUI;

@Controller
public class EmsScpEncController {

	@Autowired
	private PropertyManService propertyManService;

	@RequestMapping(value = "/common/acl/scpEnc/emsScpEnc.view")
	private 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/scpEnc/emsScpEnc.json", params = "cmd=UPDATE_ENCODE")
	public ModelAndView detail(HttpServletRequest request, String command, String orgText, String module)
			throws Exception {

		String resultTxt = "";
		if ("scp".equals(module)) {
			// 프라퍼티 조회
			String prptyGroupName = "EXT_MODULE";
			String path = "";
			String key = "";

			PropertyGroupUI propertyGroupForm = propertyManService.selectDetail(prptyGroupName);

			if (propertyGroupForm != null) {
				for (PropertyUI propertyForm : propertyGroupForm.getGridData()) {
					if ("SCP_PATH".equals(propertyForm.getPrptyName())) {
						path = propertyForm.getPrpty2Val();
					} else if ("SCP_KEY".equals(propertyForm.getPrptyName())) {
						key = propertyForm.getPrpty2Val();
					}
				}
			}

			resultTxt = encode(orgText, path, key);
		} else if ("seed".equals(module) && orgText != null) {
			resultTxt = Seed.encrypt(orgText);
		}
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("resultTxt", resultTxt);

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

	}

	public String encode(String data, String path, String key) {
		String encData = null;

		return encData;
	}

	public String decode(String data, String path, String key) {
		String decData = null;
//       ScpDbAgent agt = new ScpDbAgent();
//       decData = agt.ScpDecB64(path, key, data);
		return decData;
	}

}
