 package com.dbs.mplus.controller.admin;

import java.util.HashMap;
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.transaction.annotation.Transactional;
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.util.ResponseUtil;
import com.dbs.mplus.service.mobile.user.MPUserService;
import com.dbs.mplus.service.mobile.user.repository.dao.MPlusUser;

/**
 * 관리자 화면의 사용자 관리 
 * @author 
 *
 */
@Controller
@RequestMapping("/admin/user")
public class AdminUserController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass()); 
	
	@Autowired 
	private MPUserService userService; 

	@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 { 
			//return this.userService.getPagingData(map);
			return this.userService.selectUserList(map); 
		}
		catch(final MPlusServiceException ex) {
			throw new AdminControllerException(ex); 
		}
	}
	
	// block 된거 해제하기 
	@RequestMapping(value="/clear.json", method = RequestMethod.POST)
	@ResponseBody 
	public MPlusUser clear(@RequestBody MPlusUser user) throws AdminControllerException {
		
		// 수정 
		try { 
			this.userService.clear(user);
			if(this.logger.isDebugEnabled()) { 
				this.logger.debug("blocking 해제 : seq = " + user.getUserSeq());
			} 
		}
		catch(final MPlusServiceException ex) { 
			throw new AdminControllerException(ex); 
		}
		
		return user; 
	}
	
	// 탈퇴처리
	@RequestMapping(value="/remove.json", method = RequestMethod.POST)
	@Transactional
	@ResponseBody 
	public Map<String,Object> remove(@RequestBody Map<String,String> map) throws AdminControllerException {
		Map<String, String> resultMap = new HashMap<String,String>(); 
		// 삭제 
		try { 
			for( String key : map.keySet() ) {
				 String userId = map.get(key);
				 this.userService.removeUser( userId );
				 if(this.logger.isDebugEnabled()) {
					this.logger.debug( "탈퇴처리 : userId = " + userId );
				 }
				 this.logger.debug( "탈퇴처리 : userId = " + userId );
			}
			
			resultMap.put("type", "success");
			resultMap.put("msg", "사용자 탈퇴가 처리되었습니다.");
		} 
		catch(final MPlusServiceException ex) { 
			resultMap.put("type", "fail");
			resultMap.put("msg", "사용자 탈퇴처리에 실패하였습니다.");
			throw new AdminControllerException(ex); 
		}
			
		return ResponseUtil.wrapResultMap(resultMap); 
	}
}
