package com.dbs.mplus.controller.mobile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.HandlerMapping;

import com.dbs.mplus.exception.mobile.ApiCallException;
import com.dbs.mplus.exception.mobile.MobileControllerException;
import com.dbs.mplus.framework.FrameworkConstants;
import com.dbs.mplus.framework.exception.service.LoginException;
import com.dbs.mplus.framework.service.admin.userAttr.UserAttrService;
import com.dbs.mplus.framework.service.admin.userAttr.repository.dao.UserAttr;
import com.dbs.mplus.framework.service.mobile.user.UserAuthService;
import com.dbs.mplus.framework.service.mobile.user.repository.dao.UserAuth;
import com.dbs.mplus.framework.util.SessionUtil;
import com.dbs.mplus.service.mobile.user.repository.dao.MPlusUser;

/**
 * 사용자 속성 목록
 * @author heesungkim
 *
 */
@Controller
@RequestMapping("/mobile/auth/")
public class UserAuthController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private UserAuthService userAuthService;
	@Autowired
	private UserAttrService userAttrService;

	
	/**
	 * 사용자 속성 목록 반환
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 * @throws MobileControllerException
	 */
	@RequestMapping(value = "/getList.json", method = RequestMethod.POST)
	@ResponseBody
	public List<UserAttr> getList(HttpServletRequest request, HttpServletResponse response, 
			@RequestBody(required = false) MPlusUser user) throws MobileControllerException {
		if(this.logger.isDebugEnabled()) { 
			this.logger.debug("request : " + request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
			this.logger.debug("권한 목록 요청 사용자 : " + user);
		}
		
		List<UserAttr> userAttrList = null;
		UserAttr userInfo = new UserAttr();
		userInfo.setUserId(user.getUserId());
		try { 
			userAttrList = userAttrService.findUserAttrList(userInfo);
		} 
		catch(ApiCallException ex) { 
			throw new LoginException("mplus.error.if.system", "시스템 호출 오류가 발생했습니다 (IF)", null);			
		}
		
		return userAttrList;
	}
	
	/**
	 * 사용자 속성 변경
	 * @param request
	 * @param response
	 * @param paramMap
	 * @throws MobileControllerException
	 */
	@RequestMapping(value = "/changeUserAttr.json", method = RequestMethod.POST) 
	@ResponseBody
	public void changeUserAttr(HttpServletRequest request, HttpServletResponse response, 
			@RequestBody Map<String, Object> paramMap) throws MobileControllerException {
		if(this.logger.isDebugEnabled()) { 
			this.logger.debug("request : " + request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
		}
		
		//선택한 사용자 속성 순서 가져오고
		Long attrOrder = ((Integer)paramMap.get("userAttrOrder")).longValue();
		String userId = (String) paramMap.get("userId");
		
		if(userId == null) {
			userId = SessionUtil.getUserId(request);
		}
		
		HttpSession session  = request.getSession();
		//선택한 사용자 속성 순서에 해당하는 권한 목록 가져오기
		List<UserAuth> userAuths = this.userAuthService.getAuthAttrListByAuthId(userId, attrOrder);
		//session에 사용자 권한들 추가하고
		session.setAttribute(FrameworkConstants.SESSION.USER_AUTH_LIST.getKey(), userAuths);
		
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("userId", userId);
		userMap.put("useAttrOrder", attrOrder);
		
		userAttrService.modifyUserUseAttrOrder(userMap);
		
	}
}