package com.its.op.controller.its.vds;

import com.its.op.controller.its.LoginController;
import com.its.op.dto.its.vds.VdsControlDto;
import com.its.op.service.its.vds.VdsControlService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Slf4j
@Api(tags = "13.VDS-0.제어")
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/vds/control")
public class VdsControlController {

    private final VdsControlService service;
    private final LoginController loginController;

    @ApiOperation(value = "VDS 제어기 리셋", response = VdsControlDto.VdsControlRes.class)
    @PostMapping(value = "/reset/{id}", produces = {"application/json; charset=utf8"})
    public VdsControlDto.VdsControlRes controlReset(
            @ApiParam(name = "id", value = "제어기번호", example = "1001", required = true)
            @PathVariable("id") Long id,
            @ApiParam(name = "req", value = "제어정보", example = "", required = true)
            @RequestBody @Valid final VdsControlDto.VdsControlResetReq req,
            HttpServletRequest request) {
        String userId = this.loginController.getSessionUserId(request);
        log.info("UUID: {}", userId);
        if (!userId.equals("")) {
            req.setUserId(userId);
        }
        return this.service.controlReset(id, req);
    }

    @ApiOperation(value = "VDS 영상 이미지 요청", response = VdsControlDto.VdsControlRes.class)
    @PostMapping(value = "/stop-image/{id}", produces = {"application/json; charset=utf8"})
    public VdsControlDto.VdsControlRes requestStopImage(
            @ApiParam(name = "id", value = "제어기번호", example = "1001", required = true)
            @PathVariable("id") Long id,
            @ApiParam(name = "req", value = "VDS 영상정보 요청", example = "[1, 0, ADMIN]", required = true)
            @RequestBody @Valid final VdsControlDto.VdsControlStopImageReq req,
            HttpServletRequest request) {
        String userId = this.loginController.getSessionUserId(request);
        log.info("UUID: {}", userId);
        if (!userId.equals("")) {
            req.setUserId(userId);
        }
        if (req.getFrameNo() != 0) {
            req.setFrameNo(0);
        }
        return this.service.requestStopImage(id, req);
    }

}
