package com.its.web.controller.admin;

import com.its.web.service.notice.NoticeService;
import com.its.web.service.popup.PopupService;
import com.its.web.service.traffic.IntersectionService;
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 org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Nullable;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Slf4j
@RequiredArgsConstructor
@Controller
@RequestMapping(value="/phits")
@Api(tags = "99-2.운영자 화면 이동")
public class AdminController {
    private final NoticeService noticeService;
    private final PopupService popupService;
    private final TrafficService trafficService;
    private final IntersectionService ixrService;

    @ApiOperation(value = "00.공통 - 01.로그인")
    @GetMapping("")
    public String login(HttpSession session,
                        Model model,
                        HttpServletResponse res,
                        @Nullable @Param("LoginFail") String loginFail) throws IOException {
        model.addAttribute("loginFail", loginFail);
        return "admin/login";
    }

    @ApiOperation(value = "00.공통 - 02.로그아웃")
    @GetMapping("/logout")
    public void logout(HttpSession session, HttpServletResponse res) {
        try {
            session.invalidate();

            res.sendRedirect("/phits");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "00.공통 - 03.메인화면")
    @GetMapping("/main")
    public String adminMain(Model model) {
        model.addAttribute("notice", this.noticeService.findMainNotice(5));
        model.addAttribute("popup", this.popupService.findMainPopup(5));
        return "admin/main";
    }

    @ApiOperation(value = "01.공지사항 - 01.공지사항 목록")
    @GetMapping("/notice-list")
    public String adminNoticeList(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 "admin/notice-list";
    }

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

    @ApiOperation(value = "01.공지사항 - 03.공지사항 글쓰기")
    @GetMapping("/notice-write")
    public String adminNoticeWrite(Model model) {
        model.addAttribute("selected", "notice");
        return "admin/notice-write";
    }

    @ApiOperation(value = "02.접속자통계")
    @GetMapping("/conn-statistics")
    public String adminConnStatistics(Model model) {
        model.addAttribute("selected", "statistics");
        return "admin/conn-statistics";
    }

    @ApiOperation(value = "03.팝업공지 - 02.팝업 상세내용")
    @GetMapping(value= {"/popup-list","/popup-list/{page}"})
    public String adminPopupList(Model model, @Nullable @PathVariable("page") String page) {
        model.addAttribute("selected", "popup");
        model.addAttribute("list", this.popupService.findAllPopup(page));
        return "admin/popup-list";
    }

    @ApiOperation(value = "03.팝업공지 - 02.팝업 상세내용")
    @GetMapping("/popup-view/{popupId}")
    public String adminPopupView(Model model, @PathVariable("popupId") String popupId) {
        model.addAttribute("selected", "popup");
        model.addAttribute("popup", this.popupService.findPopup(popupId));
        return "admin/popup-view";
    }

    @ApiOperation(value = "03.팝업공지 - 03.팝업 글쓰기")
    @GetMapping("/popup-write")
    public String adminPopupWrite(Model model) {
        model.addAttribute("selected", "popup");
        return "admin/popup-write";
    }

    @ApiOperation(value = "04.CCTV 관리")
    @GetMapping("/cctv-list")
    public String adminCctvManagement(Model model) {
        model.addAttribute("selected", "cctv");
        model.addAttribute("cctv", trafficService.findAllCctv());
        model.addAttribute("intersection", ixrService.findAllIntersection());
        return "admin/cctv";
    }

}
