package com.eactive.eai.rms.onl.apim.portalnotice;

import com.eactive.eai.rms.common.base.BaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.login.SessionManager;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.data.entity.onl.apim.portalnotice.PortalNoticeUISearch;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequiredArgsConstructor
public class PortalNoticeManController extends BaseAnnotationController {

	private final PortalNoticeManService portalNoticeManService;
	private final ComboService comboService;

	@GetMapping(value = "/onl/apim/portalnotice/portalNoticeMan.view")
	public void view() {
		// view
	}

	@GetMapping(value = "/onl/apim/portalnotice/portalNoticeMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/apim/portalnotice/portalNoticeManDetail";
	}


	@PostMapping(value = "/onl/apim/portalnotice/portalNoticeMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<PortalNoticeUI>> selectList(@SortDefault(sort = "createdDate", direction = Sort.Direction.DESC) Pageable pageable,
																   PortalNoticeUISearch portalNoticeUISearch) {
		Page<PortalNoticeUI> page = portalNoticeManService.selectList(pageable, portalNoticeUISearch);
		return ResponseEntity.ok(new GridResponse<>(page));
	}
	
	@PostMapping(value = "/onl/apim/portalnotice/portalNoticeMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo() {
		List<ComboVo> noticeTypeList = comboService.getMonitoringCodeSortedBySeq("NOTICE_TYPE");
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("noticeTypeList", noticeTypeList);
		return ResponseEntity.ok(resultMap);
	}

	@PostMapping(value = "/onl/apim/portalnotice/portalNoticeMan.json", params = "cmd=DETAIL")
	public ResponseEntity<PortalNoticeUI> selectDetail(String id) {
		PortalNoticeUI PortalNoticeUI = portalNoticeManService.selectDetail(id);
		return ResponseEntity.ok(PortalNoticeUI);
	}

	@PostMapping(value = "/onl/apim/portalnotice/portalNoticeMan.json", params = "cmd=INSERT")
	public ResponseEntity<String> insert(PortalNoticeUI portalNoticeUI, HttpServletRequest request) throws IOException {
		portalNoticeUI.setInquirerName(SessionManager.getUserName(request));
		portalNoticeManService.insert(portalNoticeUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/apim/portalnotice/portalNoticeMan.json", params = "cmd=UPDATE")
	public ResponseEntity<String> update(PortalNoticeUI portalNoticeUI) throws IOException {
		portalNoticeManService.update(portalNoticeUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/apim/portalnotice/portalNoticeMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(String id) {
		portalNoticeManService.delete(id);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/apim/portalnotice/portalNoticeMan.json", params = "cmd=DELETE_BULK")
	public ResponseEntity<String> deleteBulk(String ids) {
		portalNoticeManService.deleteBulk(ids);
		return ResponseEntity.ok().build();
	}

}
