 package com.dbs.mplus.controller.admin;

import java.util.Map;

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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dbs.mplus.exception.admin.AdminControllerException;
import com.dbs.mplus.framework.exception.MPlusServiceException;
import com.dbs.mplus.framework.service.admin.userAttr.UserAttrService;
import com.dbs.mplus.framework.service.admin.userAttr.repository.dao.UserAttr;
import com.dbs.mplus.service.api.MPlusApiCallService;
import com.dbs.mplus.service.api.MPlusApi;

/**
 * 사용자 속성 관리 화면 
 * @author 
 *
 */
@Controller
@RequestMapping("/admin/userAttr")
public class UserAttrController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired 
	private MPlusApiCallService apiCallService; 
	@Autowired
	private UserAttrService userAttrService;

	@RequestMapping(value = "/view.page", method = RequestMethod.GET)
	public Model user(Model model) throws AdminControllerException {
		return model; 
	}
	
	// 일반 사용자 목록임 
	@RequestMapping(value="/getList.json", method = RequestMethod.POST)
	@ResponseBody 
	public Map<String, Object> getUserList(@RequestBody Map<String, Object> map) throws AdminControllerException {
		try {
			if(this.logger.isDebugEnabled()) { 
				this.logger.debug("사용자 속성 관리 리스트 요청");
			} 
			final String searchText = (String)map.get("searchText");
			final String selectUserType = (String)map.get("selectUserType");
			
			if(searchText != null && !searchText.isEmpty()) {
				if(selectUserType.equals("userId")) {
					map.put("userId", searchText);
				}
				else if(selectUserType.equals("userName")) {
					map.put("userName", searchText);
				}
			}
			
			return this.userAttrService.findListByAttrCondiction(map);
		}
		catch(final MPlusServiceException ex) {
			throw new AdminControllerException(ex); 
		}
	}
	

	@RequestMapping(value = "/save.json", method = RequestMethod.POST)
	@ResponseBody
	public UserAttr save(@RequestBody UserAttr userAttr) throws AdminControllerException {

		try { 
			this.userAttrService.save(userAttr);;
		}
		catch(final MPlusServiceException ex) {
			this.logger.error("사용자 속성 저장실패. userAttr : " + userAttr, ex);
			throw new AdminControllerException(ex); 
		}
		
		this.logger.debug(userAttr.toString());
		
		return userAttr;
	}

	/**
	 * 
	 * @param userAttr
	 * @throws AdminControllerException
	 */
	@RequestMapping(value = "/remove.json", method = RequestMethod.POST)
	public void remove(@RequestBody UserAttr userAttr) throws AdminControllerException {
		try { 
			Map<String, Object> userAttrInfo = null;
			userAttrInfo = (Map<String, Object>) this.apiCallService.execute(MPlusApi.USER_USE_ATTR_GETUSEATTR, "userId", (String)userAttr.getUserId());
			//사용자가 선택한 속성
			final String useAttrOrder = String.valueOf(userAttrInfo.get("useAttrOrder"));
			//사용자가 삭제할 속성 순서
			final String userAttrOrder = String.valueOf(userAttr.getUserAttrOrder());
			if(useAttrOrder.equals(userAttrOrder)){
				throw new AdminControllerException("사용자가 선택한 속성입니다.");
			}
			this.userAttrService.removeUserAttr(userAttr);
			// 순서조정을 컨트롤러단에서 하게될지도 몰라 일단 구현..
			/*Map<String, Object> userMap = new HashMap<String, Object>();
			userMap.put("userId", userAttr.getUserId());
			userMap.put("useAttrOrder", 0);
			
			this.apiCallService.execute(MPlusApi.USER_USE_ATTR_ORDER_UPDATE, userMap);*/
		}
		catch(final MPlusServiceException ex) { 
			this.logger.error("사용자 속성 삭제실패. userAttr : " + userAttr);
			throw new AdminControllerException(ex); 
		}
		
		this.logger.debug(userAttr.toString());		
	}

	
	@RequestMapping(value="/getSeachAttr.json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getSearchUserAttr(@RequestBody Map<String, Object> map) throws AdminControllerException {
		try {
			if(this.logger.isDebugEnabled()) { 
				this.logger.debug("사용자 속성 관리 리스트 요청");
			} 
			return this.userAttrService.findListByAttrCondiction(map);
		}
		catch(final MPlusServiceException ex) {
			throw new AdminControllerException(ex); 
		}
	}
}
