package incheon.res.mng.cd.ftrc.web;

import incheon.com.cmm.ComDefaultVO;
import incheon.com.cmm.ResponseCode;
import incheon.com.cmm.api.DefaultApiResponse;
import incheon.res.mng.cd.ftrc.service.MngCdFtrcService;
import incheon.res.mng.cd.ftrc.vo.MngCdFtrc;
import incheon.res.mng.cd.ftrc.vo.MngCdFtrcVO;
import org.apache.log4j.Logger;
import org.egovframe.rte.fdl.property.EgovPropertyService;
import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springmodules.validation.commons.DefaultBeanValidator;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@SessionAttributes(types = ComDefaultVO.class)
@RequestMapping("${api.rcs-path}")
public class MngCdFtrcController {

    @Resource
    private MngCdFtrcService mngCdFtrcService;

    @Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;

    @Autowired
    private DefaultBeanValidator beanValidator;
    Logger logger = Logger.getLogger(this.getClass());

    /**
     * @MethodDESC : 지형지물부호조회.
     */
    @RequestMapping(value = "/MngCdFtrcL010.do")
    public String selectFtrcList(@RequestParam(defaultValue = "1") int pageIndex, HttpServletRequest request, @ModelAttribute("mngCdFtrcVO") MngCdFtrcVO vo, ModelMap model) throws Exception{

        vo.setPageIndex(pageIndex);

        PaginationInfo paginationInfo = new PaginationInfo();
        paginationInfo.setCurrentPageNo(pageIndex);
        paginationInfo.setRecordCountPerPage(vo.getRecordCountPerPage());
        paginationInfo.setPageSize(vo.getPageSize());

        vo.setFirstIndex(paginationInfo.getFirstRecordIndex());
        vo.setLastIndex(paginationInfo.getLastRecordIndex());
        vo.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());

        int totCnt = mngCdFtrcService.countFtrc(vo);
        paginationInfo.setTotalRecordCount(totCnt);
        model.addAttribute("resultCnt", totCnt);
        model.addAttribute("paginationInfo", paginationInfo);

        /** 지형지물부호 리스트 */
        List resultList = mngCdFtrcService.selectFtrcList(vo);
        model.addAttribute("resultList", resultList);
        model.addAttribute("searchVo", vo);

        return "res/mng/cd/ftrc/MngCdFtrcL010";
    }

    /**
     * @MethodDESC : 지형지물부호조회 등록페이지 이동
     */
    @RequestMapping(value = "/MngCdFtrcC010.do")
    public String insertFtrcView(@ModelAttribute("mngCdFtrcVO") MngCdFtrcVO vo) throws Exception{
        return "res/mng/cd/ftrc/MngCdFtrcC010";
    }

    /**
     * @MethodDESC : 지형지물부호조회 등록
     */
    @RequestMapping(value = "/MngCdFtrcC011.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> insertFtrc(@ModelAttribute MngCdFtrcVO vo) throws Exception{

        Map<String, Object> resultMap = new HashMap<String, Object>();

        if(mngCdFtrcService.chkFtrCde(vo) > 0){
            resultMap.put("message", "이미 존재하는 지형지물부호 입니다.");
            return ResponseEntity.badRequest().body(DefaultApiResponse.error(500, "중복", "duplicated"));
        }

        int result = mngCdFtrcService.insertFtrc(vo);
        resultMap.put("result", result);
        return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
    }

    /**
     * @MethodDESC : 지형지물부호조회 수정페이지 이동
     */
    @RequestMapping(value = "/LayerPopMngCdFtrcU010.do")
    public String updateFtrcView(@ModelAttribute("mngCdFtrcVO") MngCdFtrcVO vo, ModelMap model) throws Exception{

        MngCdFtrc result = mngCdFtrcService.updateFtrcView(vo);
        model.addAttribute("result", result);
        return "res/mng/cd/ftrc/LayerPopMngCdFtrcU010";
    }

    /**
     * @MethodDESC : 지형지물부호조회 수정
     */
    @RequestMapping(value = "/MngCdFtrcU011.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> updateFtrc(@ModelAttribute MngCdFtrcVO vo) throws Exception{
        Map<String, Object> resultMap = new HashMap<String, Object>();

        int result = mngCdFtrcService.updateFtrc(vo);

        resultMap.put("result", result);

        DefaultApiResponse<Map<String, Object>> response = DefaultApiResponse.success(resultMap);

        return ResponseEntity.ok(response);
    }

    /**
     * @MethodDESC : 지형지물부호조회 삭제
     */
    @RequestMapping(value = "/MngCdFtrcD010.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> deleteFtrc(@ModelAttribute MngCdFtrcVO vo) throws Exception{
        Map<String, Object> resultMap = new HashMap<String, Object>();

        mngCdFtrcService.deleteFtrc(vo);
        resultMap.put("success", true);
        return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
    }
}
