package com.its.api.its.controller.management;

import com.its.api.its.model.dto.ifsc.TbIfscDto;
import com.its.api.its.model.dto.link.TbLinkDto;
import com.its.api.its.model.dto.node.TbNodeDto;
import com.its.api.its.model.dto.road.TbRoadDto;
import com.its.api.its.service.ifsc.TbIfscService;
import com.its.api.its.service.link.TbLinkService;
import com.its.api.its.service.node.TbNodeService;
import com.its.api.its.service.road.TbRoadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "00.교통상황관리-0.메인-5.지도검색")
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/manage/main/search")
public class GisSearchController {

    private final TbNodeService nodeService;
    private final TbLinkService linkService;
    private final TbIfscService ifscService;
    private final TbRoadService roadService;

    @ApiOperation(value = "노드검색(TB_NODE)", response = TbNodeDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/node", produces = {"application/json; charset=utf8"})
    public List<TbNodeDto> findNodeByFilter(
            @ApiParam(name = "filter", value = "노드명/아이디", example = "교차로", required = false)
            @RequestParam final String filter) {
        return this.nodeService.findByFilter(filter);
    }

    @ApiOperation(value = "링크검색(TB_LINK)", response = TbLinkDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/link", produces = {"application/json; charset=utf8"})
    public List<TbLinkDto> findLinkByFilter(
            @ApiParam(name = "filter", value = "도로명/아이디", example = "지방도333호선", required = false)
            @RequestParam final String filter) {
        return this.linkService.findByFilter(filter);
    }

    @ApiOperation(value = "정보제공구간검색(TB_IFSC)", response = TbLinkDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/ifsc", produces = {"application/json; charset=utf8"})
    public List<TbIfscDto> findIfscByFilter(
            @ApiParam(name = "filter", value = "구간명/아이디", example = "신갈우회도로", required = false)
            @RequestParam final String filter) {
        return this.ifscService.findByFilter(filter);
    }

    @ApiOperation(value = "도로검색(TB_ROAD)", response = TbLinkDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/road", produces = {"application/json; charset=utf8"})
    public List<TbRoadDto> findRoadByFilter(
            @ApiParam(name = "filter", value = "도로명/아이디", example = "신수로", required = false)
            @RequestParam final String filter) {
        return this.roadService.findByFilter(filter);
    }

    /**
     * 데이터베이스-노드관리의 노드정보개별조회와 동일함
     * 위의 링크/정보제공구간/도로 조회후 지도 이동시에 이동할 좌표를 구하기 위해 지도검색 컨트롤러에 기능을 추가함.
     * 시작 또는 종료노드 아이디로 해당 노드의 위치정보를 조회한 후에 지도로 이동한다.
     * @param id
     * @return
     */
    @ApiOperation(value = "노드정보 개별조회-위치이동(TB_NODE)", response = TbNodeDto.class)
    @GetMapping(value = "/pos/{id}", produces = {"application/json; charset=utf8"})
    public TbNodeDto findNodeById(
            @ApiParam(name = "id", value = "노드아이디", example = "2280022000", required = true)
            @PathVariable final Long id) {
        return this.nodeService.findById(id);
    }

}
