package incheon.sgp.kfs.web;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.ResponseEntity;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import incheon.cmm.ahm.flight.service.G2fFlightService;
import incheon.cmm.ahm.flight.vo.G2fFlightVO;
import incheon.com.cmm.api.DefaultApiResponse;
import incheon.sgp.common.service.SgpCommonService;
import incheon.sgp.common.vo.SgpCmnVO;
import incheon.sgp.kfs.service.SgpKfsService;
import incheon.sgp.kfs.vo.SelectComboVO;
import incheon.sgp.kfs.vo.SgpKfs01VO;
import incheon.sgp.kfs.vo.SgpKfs02VO;
import incheon.sgp.kfs.vo.SgpKfs03VO;
import incheon.sgp.kfs.vo.SgpKfs04VO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author : JungHoon, Lee
 * @version 1.0
 * @Class Name  : SgpSampleController.java
 * @Description :
 * @Modification Information
 * 개정일자			     개정자                  개정내용
 * ------------------ ----------- --------------------------
 * 2025. 09. 23        JungHoon, Lee         최초생성
 * <p>
 * <p>
 * Copyright 2025. 올포랜드 INC.All rights reserved.
 * @since : 2025. 09. 23
 */

@Controller
@RequestMapping("/sgp/kfs")
@RequiredArgsConstructor
@Slf4j
public class SgpKfsController {

    private final SgpKfsService sgpKfsService;
    private final SgpCommonService sgpCommonService;
    private final G2fFlightService flightService;
    
    @RequestMapping(value = "/main.do")
	public String main01(HttpServletRequest request, @ModelAttribute("SgpKfs01VO") SgpKfs01VO sgpKfs01VO, ModelMap model)
	  throws Exception{
    	
    	
    	List<SelectComboVO> areaList = sgpKfsService.selectAreaList();
    	
    	List<SelectComboVO> eraList = sgpKfsService.selectEraList();
    	
    	model.addAttribute("areaList", areaList);
    	
    	model.addAttribute("eraList", eraList);
    	
    	SgpKfs01VO paramVO3 = new SgpKfs01VO();
    	
    	List<SgpKfs01VO> places = sgpKfsService.getPlaceList(paramVO3); 
    	
    	model.addAttribute("places", places);
    	
    	model.addAttribute("headerTitle", "인천 최초최고");
    	
        List<SgpKfs04VO> markerList = new ArrayList<SgpKfs04VO>();
    	int idx = 0;
    	
    	for (SelectComboVO combo : eraList) {
    		
    		SgpKfs04VO layer = new SgpKfs04VO();
    		layer.setId("places"+idx);
    		layer.setName(combo.getCodeNm());
    		layer.setType("places");
    		markerList.add(layer);
    		
    		idx++;
    	}
		model.addAttribute("markerList", markerList);
    	
        return "/sgp/kfs/kfsMapMain";
        
    }
    
    @RequestMapping(value = "/selectMain01.do", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
	public ResponseEntity<DefaultApiResponse<Map<String, Object>>> selectMain01(HttpServletRequest request, @RequestBody SgpKfs01VO sgpKfs01VO, ModelMap model)
	  throws Exception{
    	
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	
    	List<SelectComboVO> eraList = sgpKfsService.selectEraList();
    	resultMap.put("eraList",eraList);
    	
    	sgpKfs01VO.setArea(sgpKfs01VO.getArea10());
    	sgpKfs01VO.setEra(sgpKfs01VO.getEra10());
    	
    	List<SgpKfs01VO> sgpKfs01List = sgpKfsService.getPlaceList(sgpKfs01VO);
    	
    	resultMap.put("result",sgpKfs01List);
       
        return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
        
    }
    
    @RequestMapping(value = "/selectSub01.do", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
	public ResponseEntity<DefaultApiResponse<Map<String, Object>>> selectSub01(HttpServletRequest request, @RequestBody SgpKfs01VO sgpKfs01VO, ModelMap model)
	  throws Exception{
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	
    	List<SgpKfs01VO> sgpKfs01List = sgpKfsService.getPlaceList2(sgpKfs01VO);
    	
    	resultMap.put("result",sgpKfs01List);
       
        return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
        
    }
    
    
    @RequestMapping(value = "/selectMain02.do", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
	public ResponseEntity<DefaultApiResponse<Map<String, Object>>> selectMain02(HttpServletRequest request, @RequestBody SgpKfs02VO sgpKfs02VO, ModelMap model)
	  throws Exception{
    	
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	
    	 List<SelectComboVO> areaList = sgpKfsService.selectAreaList();
     	
     	List<SelectComboVO> eraList = sgpKfsService.selectEraList();
     	
     	resultMap.put("areaList", areaList);
     	
     	resultMap.put("eraList", eraList);
     	
    	SgpKfs01VO paramVO = new SgpKfs01VO();
    	paramVO.setEra(sgpKfs02VO.getEra20());
    	paramVO.setArea(sgpKfs02VO.getArea20());
    	paramVO.setEmdCd(sgpKfs02VO.getEmdCd());
    	
    	List<SgpKfs01VO> chartList = sgpKfsService.getPlaceList(paramVO); 
    	resultMap.put("chartList",chartList);
       
        return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
    }
    
    @RequestMapping(value = "/selectSub02.do", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
	public ResponseEntity<DefaultApiResponse<Map<String, Object>>> selectSub02(HttpServletRequest request, @RequestBody SgpKfs02VO sgpKfs02VO, ModelMap model)
	  throws Exception{
    	
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	
    	SgpKfs01VO paramVO = new SgpKfs01VO();
    	paramVO.setEra(sgpKfs02VO.getEra());
    	paramVO.setArea(sgpKfs02VO.getArea());
   	 	paramVO.setEmdCd(sgpKfs02VO.getEmdCd());
    	
    	List<SgpKfs02VO> chart01 = sgpKfsService.selectChart1(paramVO); 
    	
    	List<SgpKfs01VO> chartList = sgpKfsService.getPlaceList(paramVO); 
    	
    	
    	SgpKfs01VO paramVO2 = new SgpKfs01VO();
    	List<SgpKfs02VO> chart02 = new ArrayList<SgpKfs02VO>();
    	
    	 paramVO2.setArea(sgpKfs02VO.getArea());
    	 paramVO2.setEra(sgpKfs02VO.getEra());
    	 paramVO2.setEmdCd(sgpKfs02VO.getEmdCd());
    	 chart02 = sgpKfsService.selectChart2(paramVO2);
    	
    	
			/*
			 * if ("".equals(sgpKfs02VO.getArea20())) { chart02 =
			 * sgpKfsService.selectChart2(paramVO2); } else {
			 * paramVO2.setArea(sgpKfs02VO.getArea20()); chart02 =
			 * sgpKfsService.selectChart3(paramVO2); }
			 */
    	
    	List<SgpKfs01VO> chartList2 = sgpKfsService.getPlaceList(paramVO2); 
    	
    	resultMap.put("chart01", chart01);
    	resultMap.put("chart02", chart02);
     	resultMap.put("chartList",chartList);
     	resultMap.put("chartList2",chartList2);
     	
        return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
    }
    
    
    
    @RequestMapping(value = "/selectMain03.do", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
	public ResponseEntity<DefaultApiResponse<Map<String, Object>>> selectMain03(HttpServletRequest request, @RequestBody SgpKfs03VO sgpKfs03VO, ModelMap model)
	  throws Exception{
    	
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	
    	SgpKfs01VO paramVO = new SgpKfs01VO();
    	paramVO.setEra(sgpKfs03VO.getEra30());
    	
    	List<SgpKfs01VO> places1 = sgpKfsService.getPlaceList(paramVO); 
    	
    	SgpKfs01VO paramVO2 = new SgpKfs01VO();
    	paramVO2.setEra(sgpKfs03VO.getEra31());
    	
    	List<SgpKfs01VO> places2 = sgpKfsService.getPlaceList(paramVO2); 
    	
    	SgpKfs01VO paramVO3 = new SgpKfs01VO();
    	paramVO3.setEra(sgpKfs03VO.getEra32());
    	
    	List<SgpKfs01VO> places3 = sgpKfsService.getPlaceList(paramVO3);
    	
    	SgpKfs01VO paramVO4 = new SgpKfs01VO();
    	paramVO4.setEra(sgpKfs03VO.getEra33());
    	
    	List<SgpKfs01VO> places4 = sgpKfsService.getPlaceList(paramVO4);
    	
    	resultMap.put("places1",places1);
    	resultMap.put("places2",places2);
    	resultMap.put("places3",places3);
    	resultMap.put("places4",places4);
    	
        return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
        
    }
    
    @RequestMapping(value = "/selectEmdList.do", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
	public ResponseEntity<DefaultApiResponse<Map<String, Object>>> selectEmdList(HttpServletRequest request, @RequestBody SgpKfs01VO SgpKfs01VO, ModelMap model)
	  throws Exception{
    	
    	Map<String, Object> resultMap = new HashMap<String, Object>();

    	List<SgpKfs01VO> emdList = sgpKfsService.selectEmdList(SgpKfs01VO); 
    	
     	resultMap.put("emdList",emdList);
     	
        return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
    }
    
    @GetMapping(value = "/api/v1/flight", produces = "application/json")
    @ResponseBody
    public ResponseEntity<DefaultApiResponse<List<G2fFlightVO>>> pois() throws IOException {
        return ResponseEntity.ok(DefaultApiResponse.success(flightService.getList()));
    }
    
    
    
    /**
     * 좌표 기준 필지/시설 정보 조회
     */
    @Operation(summary = "좌표 기준 필지/시설 정보 조회", description = "주어진 경위도 좌표를 기준으로 필지 또는 시설 정보를 조회합니다.")
    @GetMapping("/api/parcelByPoint.do")
    public ResponseEntity<DefaultApiResponse> selectUrbTrgtParcelByPoint(
            @Parameter(description = "경위도 좌표 (lon, lat)")
            @RequestParam Map<String, Object> param) throws Exception {

    	SgpCmnVO resultVO = sgpCommonService.selectUrbTrgtParcelByPoint(param);
        return ResponseEntity.ok(DefaultApiResponse.success(resultVO, "좌표 기준 필지/시설 정보가 조회되었습니다."));
    }
}
