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 io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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 org.springframework.web.bind.annotation.RequestParam;

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;
    private final long tm = System.currentTimeMillis();

    @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, true));
        model.addAttribute("popup", this.popupService.findDisplayPopup());
        model.addAttribute("tm", tm);
        return "main/main";
    }

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

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

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

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

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

    @ApiOperation(value = "02.교통정보 - 07.주차정보")
    @GetMapping("/trafficMap/parking")
    public String trafficParking(Model model) {
        model.addAttribute("selected", "parking");
        model.addAttribute("type", "parking");
        model.addAttribute("tm", tm);
        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());
        model.addAttribute("ixr", statisticsService.findIntersectionName());
        model.addAttribute("tm", tm);
        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());
        model.addAttribute("tm", tm);
        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());
        model.addAttribute("tm", tm);
        return "statistics/congest-stat";
    }

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

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

    @ApiOperation(value = "05.공지사항 - 01.공지사항 목록")
    @GetMapping("/notice/list")
    public String noticeList(Model model,
                             @ApiParam(value = "페이지 번호", example = "1", required = false)
                             @RequestParam("page") @Nullable String page,
                             @ApiParam(value = "카테고리 종류", required = false)
                             @RequestParam("searchType") @Nullable String searchType,
                             @ApiParam(value = "검색어", required = false)
                             @RequestParam("searchText") @Nullable String searchText) {
        model.addAttribute("selected", "notice");
        model.addAttribute("list", this.noticeService.findAllList(page, searchType, searchText, true));
        model.addAttribute("tm", tm);
        return "notice/list";
    }

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

}
