package com.its.web.controller.common;

import com.its.web.dto.common.ConnStatisticsDto;
import com.its.web.dto.common.TbWwwOrgDto;
import com.its.web.dto.common.YearDto;
import com.its.web.service.common.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/common")
@Api(tags = "00.공통")
public class CommonController {

    private final CommonService service;

    @ApiOperation(value = "공공기관 웹사이트 조회(TB_WWW_ORGAN)", response = TbWwwOrgDto.class, responseContainer = "ArrayList")
    @PostMapping(value = "/web-organ", produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public List<TbWwwOrgDto> findAllOrganization() { return this.service.findAllOrganization(); }

    @ApiOperation(value = "접속자 통계(TB_WWW_CONN_HS)", response = ConnStatisticsDto.class, responseContainer = "ArrayList")
    @PostMapping(value = "/conn-statistics", produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public List<ConnStatisticsDto> getConnStatistics(@ModelAttribute ConnStatisticsDto.ConnStatisticsDtoReq req) {return this.service.getConnStatistics(req);}

    @ApiOperation(value = "접속자 통계 년도 리스트(TB_WWW_CONN_HS)", response = YearDto.class, responseContainer = "ArrayList")
    @PostMapping(value = "/year-list", produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public List<YearDto> getYearList() {return this.service.getYearList();}

    @ApiOperation(value = "에디터 게시물 이미지 업로드")
    @PostMapping(value = "/image-upload")
    @ResponseBody
    public String noticeImageUpload(HttpServletRequest req) {
        return this.service.imageUpload(req);
    }

    @ApiOperation(value= "업로드 이미지 소스 가져오기")
    @GetMapping(value="/upload/{imageName}")
    @ResponseBody
    public byte[] getImage(@PathVariable("imageName") String imageName) {
        return this.service.upload(imageName);
    }

    @ApiOperation(value = "영상 가져오기")
    @GetMapping(value="/video/{videoName}")
    @ResponseBody
    public void getVideo(@PathVariable("videoName") String videoName, HttpServletRequest req, HttpServletResponse res) {
        this.service.getVideo(videoName, req, res);
    }

}
