package incheon.sgp.rst.web;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import incheon.ags.ias.comCd.vo.ComCdVO;
import incheon.com.cmm.context.RequestContext;
import incheon.sgp.rst.service.RstTaskLyrTreeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import lombok.RequiredArgsConstructor;

/**
 * @ClassName : RstComController.java
 * @Description : 부동산 통계 지도
 *
 * @author : 이재룡
 * @since : 2025. 10. 17
 * @version : 1.0
 *
 */
@Controller
@RequestMapping("/sgp/rst")
@RequiredArgsConstructor
public class RstComController {
    @Autowired private ObjectMapper objectMapper;

    private final RstTaskLyrTreeService rstTaskLyrTreeService;

    @Value("${gis.server.url:http://10.100.232.241/MapPrimeServer}")
    private String mapprimeUrl;

    @Value("${gis.server.prefix:incheon}")
    private String workspace;
    /**
     * 부동산 통계 지도 화면
     * @param model
     * @return JSP 페이지
     * @exception Exception
     */
    @GetMapping("/view.do")
    public String rstMapView(ModelMap model) throws Exception {

        // SGP/BAS와 동일하게: 로그인 여부를 isManager로 내려서 프론트(toolbox)에서 Inner/Outer 분기 가능하게 함
        // (RST의 window.IS_MANAGER는 mapToolBox.jsp에서 isManager 모델 값으로 세팅됨)
        model.addAttribute("isManager", RequestContext.getCurrentUserId() != null);

        // 접근 가능 레이어 카테고리 리스트
        List<ComCdVO> lclsfCdList = rstTaskLyrTreeService.getRootLclsfList();

        // 좌측 패널: 탭(레이어목록) 구성
        List<PanelConfig> leftPanels = Arrays.asList(
            new PanelConfig("layerList", "/WEB-INF/jsp/sgp/rst/map/panels/layerListPanel_new.jsp", "레이어목록", "left")
        );

        // 우측 패널: 통계도표
        List<PanelConfig> rightPanels = Arrays.asList(
            new PanelConfig("detailSearch", "/WEB-INF/jsp/sgp/rst/map/panels/detailSearchPanel.jsp", "통계도표", "left")
        );

        // 지도 기본 설정 (인천 시청)
        Map<String, Object> mapConfig = new HashMap<>();
        mapConfig.put("center", Map.of("lat", 37.4563, "lng", 126.7052));
        mapConfig.put("zoom", 11);
        mapConfig.put("domainType", "rstMap");

        // JSON 데이터로 변환하여 JavaScript에서 사용할 수 있도록 전달
        String lclsfCdListJson;
        //2025-10-24 seyoung : 사용자 접근 가능한 카테고리 리스트
        try {
            lclsfCdListJson = objectMapper.writeValueAsString(lclsfCdList);
        } catch (JsonProcessingException e) {
            lclsfCdListJson = "{}";
        }

        
        model.addAttribute("leftPanels", leftPanels);
        model.addAttribute("rightPanels", rightPanels);
        model.addAttribute("mapConfig", mapConfig);
        model.addAttribute("domainType", "rstMap");
        model.addAttribute("pageTitle", "지도로 보는 통계");
        model.addAttribute("lclsfCdList", lclsfCdList);
        model.addAttribute("lclsfCdListJson", lclsfCdListJson);
        model.addAttribute("mapprimeUrl", mapprimeUrl);
        model.addAttribute("workspace", workspace);

        return "sgp/rst/map/view";
    }
    
    /**
     * 패널 설정 클래스
     */
    public static class PanelConfig {
        private String id;
        private String componentPath;
        private String title;
        private String position;
        private Map<String, Object> data;

        // 기존 호환성을 위한 생성자
        public PanelConfig(String id, String componentPath, String title) {
            this(id, componentPath, title, "left"); // 기본값은 left
        }

        public PanelConfig(String id, String componentPath, String title, String position) {
            this.id = id;
            this.componentPath = componentPath;
            this.title = title;
            this.position = position;
            this.data = new HashMap<>();
        }

        // Getters and Setters
        public String getId() { return id; }
        public void setId(String id) { this.id = id; }

        public String getComponentPath() { return componentPath; }
        public void setComponentPath(String componentPath) { this.componentPath = componentPath; }

        public String getTitle() { return title; }
        public void setTitle(String title) { this.title = title; }

        public String getPosition() { return position; }
        public void setPosition(String position) { this.position = position; }

        public Map<String, Object> getData() { return data; }
        public void setData(Map<String, Object> data) { this.data = data; }
    }
}