package incheon.cmm.map.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import incheon.cmm.map.domain.CmmnMap;
import incheon.cmm.map.service.CmmnMapService;
import lombok.RequiredArgsConstructor;

/**
 * @ClassName : MapController.java
 * @Description : 지도 플랫폼 컨트롤러 - 업무별 패널 구성 관리
 *
 * @author : 관리자
 * @since : 2024. 12. 19
 * @version : 2.0
 *
 *          <pre>
 * << 개정이력(Modification Information) >>
 *
 *   수정일              수정자               수정내용
 *  -------------  ------------   ---------------------
 *   2023. 10. 10    관리자               최초 생성
 *   2024. 12. 19    관리자               패널 기반 플랫폼으로 전면 개편
 *          </pre>
 *
 */
@Controller
@RequestMapping("/cmmnMap")
@RequiredArgsConstructor
public class CmmnMapController {
	private final CmmnMapService cmmnMapService;

	/**
	 * 공통 업무 2D 지도 화면
	 * 
	 * @param model
	 * @return JSP 페이지
	 * @exception Exception
	 */
//	@GetMapping("/map.do")
//	public String ViewCmmnMap(ModelMap model,  @RequestParam(value = "page", defaultValue = "1") int page,
//			@RequestParam(value = "size", defaultValue = "10") int size,
//			@RequestParam(value = "search", required = false) String search) throws Exception {
//		// 지도 기본 설정
//		return "cmmnMap/MapBasic";
//	}
//	@GetMapping("/mapDraw.do")
//	public String ViewCmmnMapDraw(ModelMap model,  @RequestParam(value = "page", defaultValue = "1") int page,
//			@RequestParam(value = "size", defaultValue = "10") int size,
//			@RequestParam(value = "search", required = false) String search) throws Exception {
//		// 지도 기본 설정
//		return "cmmnMap/MapDraw";
//	}
//	@GetMapping("/mapSearch.do")
//	public String ViewCmmnMapSearch(ModelMap model,  @RequestParam(value = "page", defaultValue = "1") int page,
//			@RequestParam(value = "size", defaultValue = "10") int size,
//			@RequestParam(value = "search", required = false) String search) throws Exception {
//		// 지도 기본 설정
//		return "cmmnMap/SearchMap";
//	}

	@GetMapping("/selectLayer")
	@ResponseBody
	public Map<String, Object> selectLayer(
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "size", defaultValue = "10") int size,
			@RequestParam(value = "search", required = false) String search) {
		
		int total = cmmnMapService.selectLayerCnt(search);
		
		int offset = (page - 1) * size;
		List<CmmnMap> list = cmmnMapService.selectLayer(search, size, offset);
		
		Map<String, Object> result = new HashMap<>();
		result.put("list", list);
		result.put("total", total);
		result.put("page", page);
		result.put("size", size);
		result.put("search", search);
		result.put("totalPages", (int) Math.ceil((double) total / size));
		
		return result;
	}

}