package incheon.product.geoview3d.theme.web;

import incheon.product.geoview3d.theme.service.ThemeService;
import incheon.product.geoview3d.theme.vo.ThemeCategoryVO;
import incheon.product.geoview3d.theme.vo.ThemeLayerVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * GeoView-3D 주제도 뷰 컨트롤러. 3D 지도 메인 페이지 JSP를 렌더링한다.
 */
@Slf4j
@Controller
@RequestMapping("/product/g3d/theme")
public class ThemeViewController {

    @Resource(name = "productThemeService")
    private ThemeService themeService;

    @GetMapping("/main.do")
    public String main(@RequestParam(defaultValue = "none") String name, Model model) {
        List<ThemeCategoryVO> categories = themeService.getCategories();
        List<ThemeLayerVO> layers = themeService.getLayers();

        Map<String, List<ThemeLayerVO>> themeMap = layers.stream()
                .collect(Collectors.groupingBy(l -> l.getCategoryName() != null ? l.getCategoryName() : "미분류"));

        model.addAttribute("categories", categories);
        model.addAttribute("themeMap", themeMap);
        model.addAttribute("headerTitle", "none".equals(name) ? "3D 주제도" : name);
        model.addAttribute("pageTitle", "GeoView-3D");
        return "product/geoview3d/map";
    }
}
