package cokr.xit.fims.crdn.web;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cokr.xit.base.web.ApplicationController;
import cokr.xit.fims.crdn.service.ImportService;

@Controller("fimsInterface")
@RequestMapping(value = "/interface/smg", name = "국민 신문고 민원")
public class InterfaceController extends ApplicationController {
	@Resource(name = "importService")
	private ImportService importService;

	/**국민 신문고에서 받은 민원을 등록했음을 통보한다.
	 * @param interfaceSequences 민원의 인터페이스 키 목록
	 * @return jsonView
	 * <ul><li>received - 통보 수신 여부(true || false)</li>
	 *     <li>saved - 저장 여부(true || false)</li>
	 * </ul>
	 */
	@PostMapping(value = "/petition/receive.do", name = "국민 신문고 민원 수신")
	public ModelAndView importSmgPetition(@RequestBody List<String> intfSeqs) {
		boolean empty = isEmpty(intfSeqs);
		log().debug("interfaceSequences received: {}", empty ? "none" : String.join(", ", intfSeqs));

		int affected = importService.createCrdns(intfSeqs);
		return new ModelAndView("jsonView")
			.addObject("received", !empty)
			.addObject("affected", affected)
			.addObject("saved", true);
	}
}