package com.siginfo.controller;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.siginfo.common.AppUtils;
import com.siginfo.common.CommonUtil;
import com.siginfo.common.SEEDUtil;
import com.siginfo.controller.base.BaseController;
import com.siginfo.schedule.SigInfoRepository;
import com.siginfo.service.AdminService;
import com.siginfo.vo.OperBean;

@Controller
public class AdminController extends BaseController {
	
	@Autowired
	private AdminService adminService;
	
	
	public static final Logger log = LoggerFactory.getLogger(AdminController.class);
	public static final int[] SEED_KEY = SEEDUtil.getSeedRoundKey("1234567890123456");
	
	@RequestMapping(value = "login.do", method = RequestMethod.GET)
	public String login(Locale locale, Model model) throws SQLException {
		return "login";
	}
	
	@RequestMapping(value = "checkLogin.do")
	public void checkLogin(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String passwd = CommonUtil.checkNull(request.getParameter("OPER_PASSWD"),"");
		String USER_IP = request.getRemoteAddr();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("OPER_EMAIL", 		CommonUtil.checkNull(request.getParameter("OPER_EMAIL"), "") );
		paramMap.put("OPER_PASSWD", 	SEEDUtil.getSeedEncrypt(passwd, SEED_KEY));
		paramMap.put("OPER_IP", 		USER_IP	);
		paramMap.put("queryId", 		"login");
		
		List<Map<String,Object>> result = adminService.list(paramMap);
		String permission = "";
		
		if(result.size() == 1) {
			
			Map<String,Object> map = (HashMap<String,Object>)result.get(0);
			
			OperBean operBean = new OperBean();
			String top = CommonUtil.checkNull(map.get("TYPE_TOP_CD"),"");
			String mid = CommonUtil.checkNull(map.get("TYPE_MID_CD"),"");
			String bot = CommonUtil.checkNull(map.get("TYPE_BOT_CD"),"");
			
			operBean.setOperSeq(CommonUtil.checkNull(map.get("OPER_SEQ"),""));
			operBean.setOperTypeTopCd(top);
			operBean.setOperTypeTopNm(CommonUtil.checkNull(map.get("TYPE_TOP_NM"),""));
			operBean.setOperTypeMidCd(mid);
			operBean.setOperTypeMidNm(CommonUtil.checkNull(map.get("TYPE_MID_NM"),""));
			operBean.setOperTypeBotCd(bot);
			operBean.setOperTypeBotNm(CommonUtil.checkNull(map.get("TYPE_BOT_NM"),""));
			operBean.setOperRegionCd(CommonUtil.checkNull(map.get("OPER_REGION_CD"),""));
			operBean.setOperRegionNm(CommonUtil.checkNull(map.get("OPER_REGION_NM"),""));
			operBean.setOperGrd(CommonUtil.checkNull(map.get("OPER_GRD"),""));
			operBean.setOperGrdNm(CommonUtil.checkNull(map.get("OPER_GRD_NM"),""));
			operBean.setOperNm(CommonUtil.checkNull(map.get("OPER_NM"),""));
			operBean.setOperGr(CommonUtil.checkNull(map.get("OPER_GR"),""));
			operBean.setOperIp(CommonUtil.checkNull(map.get("OPER_IP"),""));
			operBean.setOperIp2(CommonUtil.checkNull(map.get("OPER_IP2"),""));
			operBean.setOperIp3(CommonUtil.checkNull(map.get("OPER_IP3"),""));
			operBean.setOperEmail(CommonUtil.checkNull(map.get("OPER_EMAIL"),""));
			operBean.setOperPhone(CommonUtil.checkNull(map.get("OPER_PHONE"),""));
			operBean.setOperPasswd(CommonUtil.checkNull(map.get("OPER_PASSWD"),""));
			operBean.setOperOrg(CommonUtil.checkNull(map.get("OPER_ORG"),""));
			operBean.setOperGubun(CommonUtil.checkNull(map.get("OPER_GUBUN"),""));
			
			
			if(!top.equals("") && !mid.equals("") && !bot.equals("")){
				permission = "PM";  			// POLICE MAN - 경찰(서), 고순대, TBN
			}else if(!top.equals("") && !mid.equals("") && bot.equals("")){
				if(top.equals("T03")){
					permission = "PS";  		// POLICE STATION - 지방 경찰청
				}
			}else if(!top.equals("") && mid.equals("") && bot.equals("")){
				if(top.equals("T02")){
					permission = "PA";			// POLICE AGENCY - 경찰청
				}else if(top.equals("T01")){
					permission = "RC";  		// ROAD CORPORATION - 공단
				}
			}else if(top.equals("") && mid.equals("") && bot.equals("")){
				permission = "SA";				// SUPER ADMIN - 관리자
			}
			operBean.setOperUserType(permission);
			
			// 세션에 담당자 정보 추가
			request.getSession().setAttribute("OPER_INFO", operBean);
			
			paramMap.put("OPER_NM", CommonUtil.checkNull(map.get("OPER_NM"),""));
			paramMap.put("TYPE_TOP_CD", CommonUtil.checkNull(map.get("TYPE_TOP_CD"),""));
			paramMap.put("TYPE_TOP_NM", CommonUtil.checkNull(map.get("TYPE_TOP_NM"),""));
			paramMap.put("TYPE_MID_CD", CommonUtil.checkNull(map.get("TYPE_MID_CD"),""));
			paramMap.put("TYPE_MID_NM", CommonUtil.checkNull(map.get("TYPE_MID_NM"),""));
			paramMap.put("OPER_GUBUN", CommonUtil.checkNull(map.get("OPER_GUBUN"),""));
			result.add(paramMap);
		}
		
		callXmlToBean(response, result);
		
	}
	
	@RequestMapping(value = "getIMS.do")
	public @ResponseBody List<Map<String,Object>> getIMS(HttpServletRequest request) throws Exception {
		Map<String,Object> paramMap = new HashMap<String,Object>();
		
		paramMap.put("MIN_X", CommonUtil.checkNull(request.getParameter("MIN_X")));
		paramMap.put("MIN_Y", CommonUtil.checkNull(request.getParameter("MIN_Y")));
		paramMap.put("MAX_X", CommonUtil.checkNull(request.getParameter("MAX_X")));
		paramMap.put("MAX_Y", CommonUtil.checkNull(request.getParameter("MAX_Y")));
		paramMap.put("INCIDENTID", CommonUtil.checkNull(request.getParameter("INCIDENTID")));
		paramMap.put("queryId", "getIMS");
		
		return adminService.list(paramMap);
	}
}
