package com.its.web.controller.view;

import com.its.web.security.WebPasswordEncoder;
import com.its.web.service.common.CommonService;
import com.its.web.service.notice.NoticeService;
import com.its.web.service.popup.PopupService;
import com.its.web.service.statistics.StatisticsService;
import com.its.web.service.traffic.TrafficService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Nullable;

@Slf4j
@RequiredArgsConstructor
@Controller
@Api(tags = "99-1.사용자 화면 이동")
public class ViewController {

    private final TrafficService trafficService;
    private final StatisticsService statisticsService;
    private final CommonService commonService;
    private final NoticeService noticeService;
    private final PopupService popupService;

    @ApiOperation(value = "01.메인화면")
    @GetMapping("/")
    public String main(Model model) {
        WebPasswordEncoder encoder = new WebPasswordEncoder();
        model.addAttribute("selected", "");
        model.addAttribute("incident", this.trafficService.findMainIncident());
        model.addAttribute("notice", this.noticeService.findMainNotice(3));
        model.addAttribute("popup", this.popupService.findDisplayPopup());
        return "main/main";
    }

    @ApiOperation(value = "02.교통정보 - 01.CCTV")
    @GetMapping("/trafficMap/cctvMap")
    public String trafficCctv(Model model) {
        model.addAttribute("selected", "traffic");
        model.addAttribute("type", "cctv");
        return "traffic/cctv";
    }

    @ApiOperation(value = "02.교통정보 - 02.스마트교차로")
    @GetMapping("/trafficMap/intersections")
    public String trafficIntersection(Model model) {
        model.addAttribute("selected", "traffic");
        model.addAttribute("type", "intersection");
        return "traffic/intersection";
    }

    @ApiOperation(value = "02.교통정보 - 03.VMS")
    @GetMapping("/trafficMap/vms")
    public String trafficVms(Model model) {
        model.addAttribute("selected", "traffic");
        model.addAttribute("type", "vms");
        return "traffic/vms";
    }

    @ApiOperation(value = "02.교통정보 - 04.돌발")
    @GetMapping(value= {"/trafficMap/incidents/{incidentId}", "/trafficMap/incidents"})
    public String trafficIncident(Model model, @Nullable @PathVariable ("incidentId") String incidentId) {
        model.addAttribute("selected", "traffic");
        model.addAttribute("type", "incident");
        model.addAttribute("incidentId", incidentId);
        return "traffic/incident";
    }

    @ApiOperation(value = "02.교통정보 - 05.실시간 교통정보")
    @GetMapping("/trafficMap/realtimetraffic")
    public String trafficRealTimeTraffic(Model model) {
        model.addAttribute("selected", "traffic");
        model.addAttribute("type", "traffic");
        return "traffic/realtimetraffic";
    }

    @ApiOperation(value = "02.교통정보 - 07.주차정보")
    @GetMapping("/trafficMap/parking")
    public String trafficParking(Model model) {
        model.addAttribute("selected", "parking");
        model.addAttribute("type", "parking");
        return "traffic/parking";
    }

    @ApiOperation(value = "03.교통통계 - 01.교통량 통계")
    @GetMapping("/statistics/traffic01")
    public String trafficStatisticsAmount(Model model) {
        model.addAttribute("selected", "statistics");
        model.addAttribute("active", "traffic01");
        model.addAttribute("road", statisticsService.findIntersectionAtrdName());
        return "statistics/tfvl-stat-amount";
    }

    @ApiOperation(value = "03.교통통계 - 02.소통 통계")
    @GetMapping("/statistics/traffic02")
    public String trafficStatisticsSpeed(Model model) {
        model.addAttribute("selected", "statistics");
        model.addAttribute("active", "traffic02");
        model.addAttribute("road", trafficService.findAtrdNameList());
        return "statistics/tfvl-stat-speed";
    }

    @ApiOperation(value = "03.교통통계 - 03.정체 통계")
    @GetMapping("/statistics/congestion")
    public String trafficCongestStatistics(Model model) {
        model.addAttribute("selected", "statistics");
        model.addAttribute("active", "congest");
        model.addAttribute("list", commonService.findDayList());
        return "statistics/congest-stat";
    }

    @ApiOperation(value = "04.교통정보센터 - 01.센터 소개")
    @GetMapping("/center/center")
    public String center(Model model) {
        model.addAttribute("selected", "center");
        model.addAttribute("active", "center");
        return "center/center";
    }

    @ApiOperation(value = "04.교통정보센터 - 02.오시는 길")
    @GetMapping("/center/way")
    public String way(Model model) {
        model.addAttribute("selected", "center");
        model.addAttribute("active", "way");
        return "center/way";
    }

    @ApiOperation(value = "05.공지사항 - 01.공지사항 목록")
    @GetMapping("/notice/list")
    public String noticeList(Model model, @Nullable @Param("page") String page,
                         @Nullable @Param("searchType") String searchType,
                         @Nullable @Param("searchText")String searchText) {
        model.addAttribute("selected", "notice");
        model.addAttribute("list", this.noticeService.findAllList(page, searchType, searchText));
        return "notice/list";
    }

    @ApiOperation(value = "05.공지사항 - 02.공지사항 상세보기")
    @GetMapping("/notice/view/{boardNo}" )
    public String noticeView(Model model, @PathVariable("boardNo")String boardNo) {
        model.addAttribute("selected", "notice");
        model.addAttribute("notice", this.noticeService.findNotice(boardNo));
        return "notice/view";
    }

}
