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.util.StringUtils;
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.push.PushFormService;
import com.dbs.mplus.framework.service.admin.push.repository.dao.PushForm;

@Controller
@RequestMapping("/admin/pushform")
public class PushFormController {
	private final  Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired 
	private PushFormService pushFormService; 

	@RequestMapping(value = "/getList.json", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getList(@RequestBody Map<String, Object> map) throws AdminControllerException {
    	try { 
    		return this.pushFormService.getPagingData(map);
    	}
    	catch(final MPlusServiceException ex) {
    		this.logger.error("가져오기 실패", ex);
    		throw new AdminControllerException(ex); 
    	}
    }
	@RequestMapping(value = "/save.json", method = RequestMethod.POST)
	@ResponseBody
	public PushForm save(@RequestBody PushForm pushForm) throws AdminControllerException {
		
		this.checkPushFormData(pushForm);
		
		try { 
			this.pushFormService.save(pushForm);
		}
		catch(final MPlusServiceException ex) {
			this.logger.error("추가 실패. auth : " + pushForm, ex);
			throw new AdminControllerException(ex); 
		}
		
		return pushForm;
	}
	
	/**
	 * 메시지 형식 데이터 체크
	 * @param pushForm
	 * @throws AdminControllerException
	 */
	private void checkPushFormData(PushForm pushForm) throws AdminControllerException {
		
		if(StringUtils.isEmpty(pushForm.getFormId())) { 
			throw new AdminControllerException("이름을 입력해주세요."); 
		}
		
		if(StringUtils.isEmpty(pushForm.getFormType())) {
			throw new AdminControllerException("타입을 선택해주세요."); 
		}
	}
}
