package incheon.ags.por.web;

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

import org.json.simple.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import incheon.ags.por.service.PorRtmsService;
import incheon.ags.por.util.PorConnectUtils;
import incheon.ags.por.vo.PorRtmsResultVO;
import incheon.ags.por.vo.PorRtmsVO;
import incheon.ags.por.vo.kb.PorRtmsInfoVO;
import incheon.ags.por.vo.kb.PorRtmsPriceVO;
import incheon.ags.por.vo.kb.PorRtmsPyongVO;
import lombok.RequiredArgsConstructor;

/**
 * @ClassName : PorRtmsController.java
 * @Description : 공간정보포털 컨트롤러
 *
 * @author : 관리자
 * @since : 2025. 07. 17
 * @version : 1.0
 *
 *          <pre>
 * << 개정이력(Modification Information) >>
 *
 *   수정일              수정자               수정내용
 *  -------------  ------------   ---------------------
 *   2023. 10. 10    관리자               최초 생성
 *   2024. 12. 19    관리자               패널 기반 플랫폼으로 전면 개편
 *   2025. 07. 17    관리자               공간정보포털로 전환
 *          </pre>
 *
 */
@Controller
@RequestMapping("/sgp/por")
@RequiredArgsConstructor
public class PorRtmsController {
	
	private final PorRtmsService porRtmsService;
	private final PorConnectUtils porConnectUtils;
	
    /**
     * 부동산정보조회 화면
     * @param model
     * @return JSP 페이지
     * @exception Exception
     */
    @GetMapping("/info/rtmsinfo.do")
    public String rtmsinfo(
    		@ModelAttribute PorRtmsVO porRtmsVO
    		, ModelMap model
		) throws Exception {
    	
    	List<HashMap<String, List<PorRtmsResultVO>>> resultList = new ArrayList<HashMap<String, List<PorRtmsResultVO>>>();
    	List<PorRtmsResultVO> rtmsCategoryList = new ArrayList<PorRtmsResultVO>();
    	List<PorRtmsResultVO> rtmsList = new ArrayList<PorRtmsResultVO>();
    	
    	JSONArray array = new JSONArray();
    	
    	if(porRtmsVO.getSearchFlag() != null) {
    		if(porRtmsVO.getDealP() != null) {
    			String dealP = porRtmsVO.getDealP();
    			int month = Integer.parseInt(dealP);
    			porRtmsVO.setDealP(month > 10 ? String.valueOf(month) : String.valueOf("0" + month));
    		}
    		
    		// 매매, 전월세 분기
    		if(("buy").equals(porRtmsVO.getDealType())) {
	    		if(!("silv").equals(porRtmsVO.getBldType())) {
	    			rtmsCategoryList = porRtmsService.selectRtmsBuyCategoryList(porRtmsVO);
	    		}
	    		rtmsList = porRtmsService.selectRtmsBuyList(porRtmsVO);
    		}else if(("rent").equals(porRtmsVO.getDealType())) {
    			rtmsCategoryList = porRtmsService.selectRtmsRentCategoryList(porRtmsVO);
    			rtmsList = porRtmsService.selectRtmsRentList(porRtmsVO);
    		}
    		
    		// list 분리
    		List<PorRtmsResultVO> titleList = new ArrayList<PorRtmsResultVO>();
    		List<PorRtmsResultVO> contentList = new ArrayList<PorRtmsResultVO>();
    		if(rtmsList != null || rtmsList.size() > 0) {
    			String pnu = "";
    			String bldgNm = "";
    			for(PorRtmsResultVO vo : rtmsList) {
    				if(("").equals(pnu) && ("").equals(bldgNm)) {
    					pnu = vo.getPnu();
    					bldgNm = vo.getBldgNm();
    					HashMap<String, List<PorRtmsResultVO>> tempMap = new HashMap<String, List<PorRtmsResultVO>>();
    					resultList.add(tempMap);
    					tempMap.put("titleList", titleList);
    					tempMap.put("contentList", contentList);
    					titleList.add(vo);
    					contentList.add(vo);
    				}
    				
    				if(pnu.equals(vo.getPnu()) && bldgNm.equals(vo.getBldgNm())) {
    					contentList.add(vo);
    				}else {
    					pnu = vo.getPnu();
    					bldgNm = vo.getBldgNm();
    					
    					HashMap<String, List<PorRtmsResultVO>> tempMap = new HashMap<String, List<PorRtmsResultVO>>();
    					resultList.add(tempMap);
    					titleList = new ArrayList<PorRtmsResultVO>();
    					contentList = new ArrayList<PorRtmsResultVO>();
    					
    					tempMap.put("titleList", titleList);
    					tempMap.put("contentList", contentList);
    					titleList.add(vo);
    					contentList.add(vo);
    				}
    			}
    		}
    		
    	}
		model.addAttribute("rtmsCategoryList", rtmsCategoryList);
    	// model.addAttribute("list", rtmsList);
		model.addAttribute("list", resultList);
		
		System.out.println(resultList);
    	
    	return "sgp/por/info/rtmsinfo";
    }
    
    /**
     * 부동산정보조회 화면
     * @param model
     * @return JSP 페이지
     * @exception Exception
     */
    @GetMapping("/info/kbrtmsinfo.do")
    public String kbrtmsinfoPage(
    		@ModelAttribute PorRtmsInfoVO porRtmsInfoVO
    		, ModelMap model
    		) throws Exception {
    	System.out.println("unqKey : " + porRtmsInfoVO.getUnqKey());
    	PorRtmsPriceVO priceVO = new PorRtmsPriceVO();
    	priceVO.setUnqKey(porRtmsInfoVO.getUnqKey());
    	
    	PorRtmsPyongVO pyongVO = new PorRtmsPyongVO();
    	pyongVO.setUnqKey(porRtmsInfoVO.getUnqKey());
    	
    	List<PorRtmsPriceVO> priceList = porRtmsService.selectKbRtmsPrice(priceVO);
    	if(priceList.size() > 0) {
    		String curprcUpdtDay = priceList.get(0).getCurprcUpdtDay();
    		curprcUpdtDay = curprcUpdtDay.substring(0, 4) + "." + curprcUpdtDay.substring(4, 6) + "." + curprcUpdtDay.substring(6, 8);
    		model.addAttribute("curprcUpdtDay", curprcUpdtDay);
    	}
    	
    	PorRtmsInfoVO rtmsInfo = porRtmsService.selectKbRtmsInfo(porRtmsInfoVO);
    	
    	List<PorRtmsPyongVO> pyongList = porRtmsService.selectKbRtmsPyong(pyongVO);
    	
    	model.addAttribute("priceList", priceList);
    	model.addAttribute("rtmsInfo", rtmsInfo);
    	model.addAttribute("pyongList", pyongList);
    	
    	return "sgp/por/info/kbrtmsinfo";
    }
    
    /**
     * 부동산정보조회 데이터
     * @param model
     * @return JSP 페이지
     * @exception Exception
     */
    @GetMapping("/info/kbrtmsinfodata.do")
    @ResponseBody
    public Map<String, Object> kbrtmsinfoData(
    		@ModelAttribute PorRtmsInfoVO porRtmsInfoVO
    		, ModelMap model
    		) throws Exception {
    	System.out.println("unqKey : " + porRtmsInfoVO.getUnqKey());
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	
    	PorRtmsPriceVO priceVO = new PorRtmsPriceVO();
    	priceVO.setUnqKey(porRtmsInfoVO.getUnqKey());
    	
    	PorRtmsPyongVO pyongVO = new PorRtmsPyongVO();
    	pyongVO.setUnqKey(porRtmsInfoVO.getUnqKey());
    	
    	List<PorRtmsPriceVO> priceList = porRtmsService.selectKbRtmsPrice(priceVO);
    	if(priceList.size() > 0) {
    		String curprcUpdtDay = priceList.get(0).getCurprcUpdtDay();
    		curprcUpdtDay = curprcUpdtDay.substring(0, 4) + "." + curprcUpdtDay.substring(4, 6) + "." + curprcUpdtDay.substring(6, 8);
    		resultMap.put("curprcUpdtDay", curprcUpdtDay);
    	}
    	
    	PorRtmsInfoVO rtmsInfo = porRtmsService.selectKbRtmsInfo(porRtmsInfoVO);
    	
    	List<PorRtmsPyongVO> pyongList = porRtmsService.selectKbRtmsPyong(pyongVO);
    	
    	resultMap.put("priceList", priceList);
    	resultMap.put("rtmsInfo", rtmsInfo);
    	resultMap.put("pyongList", pyongList);
    	
    	return resultMap;
    }
    
    /**
     * 부동산정보조회 estateuid 취득
     * @param model
     * @return JSP 페이지
     * @exception Exception
     */
    @GetMapping("/info/kbrtmsuid.do")
    @ResponseBody
    public Map<String, Object> kbrtmsuid(
    		@ModelAttribute PorRtmsInfoVO porRtmsInfoVO
    		, ModelMap model
    		) throws Exception {
    	System.out.println("unqKey : " + porRtmsInfoVO.getUnqKey());
    	
    	String estateuid = porRtmsService.selectKbRtmsUid(porRtmsInfoVO.getUnqKey());
    	
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	resultMap.put("estateuid", estateuid);
    	
    	return resultMap;
    }
}