package egovframework.controller;

import egovframework.service.impl.CommonServiceImpl;
import egovframework.service.impl.MainServiceImpl;
import egovframework.service.impl.NoticeServiceImpl;
import egovframework.utill.PageCriteria;
import egovframework.vo.NoticeVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Controller
public class MainController {

    private final NoticeServiceImpl noticeService;
    private final MainServiceImpl mainService;
    private final CommonServiceImpl commonService;

    private PageCriteria criteria;

    @PostConstruct
    void init() {
        this.criteria = new PageCriteria();
    }

    @RequestMapping(value = "/main/Main.do", method = RequestMethod.GET)
    public String Main(ModelMap model, HashMap<String, String> paramMap) {
        return main(model, paramMap);
    }

    @RequestMapping(value = "/main/main.do", method = RequestMethod.GET)
    public String main(ModelMap model, HashMap<String, String> paramMap) {
//        log.error("/main/main.do, paramMap: {} EA.", paramMap.size());
        List<NoticeVO> noticeList = this.noticeService.getNoticeList(this.criteria);
        SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat("yy.MM.dd");
        for (NoticeVO vo : noticeList) {
            Date date;
            try {
                date = transFormat.parse(vo.getRegDate());
                vo.setRegDate(format.format(date));
            } catch (ParseException e) {
                throw new RuntimeException();
            }
        }
        model.addAttribute("siteSubInfo", "main");
        model.addAttribute("subTitle", "HOME");
        model.addAttribute("noticeList", noticeList);
        model.addAttribute("possiblePopupList", this.mainService.getPossiblePopupList());
        model.addAttribute("atrdNmList", this.commonService.getAtrdNm());
        //return "main/main.tiles";
        return "main/mainNew.tiles";
    }

    @RequestMapping(value = "/intro/location.do", method = RequestMethod.GET)
    public String location(Model model) {
        model.addAttribute("siteSubInfo", "sub info");
        model.addAttribute("subTitle", "찾아오시는길");
        return "intro/locationNew.tiles";
    }

    @RequestMapping(value = "/intro/service.do", method = RequestMethod.GET)
    public String service(Model model) {
        model.addAttribute("siteSubInfo", "sub info");
        model.addAttribute("subTitle", "서비스 소개");
        return "intro/serviceNew.tiles";
    }

    @RequestMapping(value = "/intro/center.do", method = RequestMethod.GET)
    public String center(Model model) {
        model.addAttribute("siteSubInfo", "sub info");
        model.addAttribute("subTitle", "센터 소개");
        return "intro/centerNew.tiles";
    }

    @RequestMapping(value = "/relate/relateSite.do", method = RequestMethod.GET)
    public String relateSite(Model model) {
        model.addAttribute("siteSubInfo", "sub site");
        model.addAttribute("subTitle", "교통관련사이트");
        return "relate/relateSiteNew.tiles";
    }

    @RequestMapping(value = "/relate/siteMap.do", method = RequestMethod.GET)
    public String siteMap(Model model) {
        model.addAttribute("siteSubInfo", "sub sitemap");
        model.addAttribute("subTitle", "사이트 맵");
        return "relate/siteMap.tiles";
    }

    @RequestMapping(value = "/main/popup.do", method = RequestMethod.GET)
    public String popup(Model model, @RequestParam Map<String, Object> paramMap) {
        model.addAttribute("siteSubInfo", "sub sitemap");
        model.addAttribute("popupObject", this.mainService.getPopupObject(paramMap));
        return "main/popup";
    }

}