package com.its.op.controller.its.cctv;

import com.its.op.controller.its.LoginController;
import com.its.op.dto.its.cctv.*;
import com.its.op.service.its.cctv.CctvControlService;
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 = "11.CCTV-0.제어")
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/cctv/control")
public class CctvControlController {

    private final CctvControlService service;
    private final LoginController loginController;

    @ApiOperation(value = "CCTV 환경 파라미터 조회", response = CctvParamControlDto.CctvParamValueRes.class)
    @GetMapping(value = "/param-qry/{id}", produces = {"application/json; charset=utf8"})
    public CctvParamControlDto.CctvParamValueRes requestParam(
            @ApiParam(name = "id", value = "제어기번호", example = "1001", required = true)
            @PathVariable("id") Long id) {
        return this.service.requestParam(id);
    }

    @ApiOperation(value = "CCTV 상태 조회", response = CctvStatusDto.CctvStatusDtoRes.class)
    @GetMapping(value = "/status/{id}", produces = {"application/json; charset=utf8"})
    public CctvStatusDto.CctvStatusDtoRes requestStatus(
            @ApiParam(name = "id", value = "제어기번호", example = "1001", required = true)
            @PathVariable("id") Long id) {
        return this.service.requestStatus(id);
    }

    @ApiOperation(value = "CCTV 현재위치정보 조회", response = CctvPresetControlDto.CctvPresetValueRes.class)
    @GetMapping(value = "/preset-value/{id}", produces = {"application/json; charset=utf8"})
    public CctvPresetControlDto.CctvPresetValueRes requestPresetValue(
            @ApiParam(name = "id", value = "제어기번호", example = "1001", required = true)
            @PathVariable("id") Long id) {
        return this.service.requestPresetValue(id);
    }

    @ApiOperation(value = "CCTV 환경 파라미터 설정", response = CctvParamControlDto.CctvParamControlRes.class)
    @PostMapping(value = "/param-set/{id}", produces = {"application/json; charset=utf8"})
    public CctvParamControlDto.CctvParamControlRes controlParam(
            @ApiParam(name = "id", value = "제어기번호", example = "1001", required = true)
            @PathVariable("id") Long id,
            @ApiParam(name = "req", value = "CCTV PARAMETER 설정 정보", example = "[ADMIN]", required = true)
            @RequestBody @Valid final CctvParamControlDto.CctvParamControlReq req,
            HttpServletRequest request) {
        String userId = this.loginController.getSessionUserId(request);
        log.info("UUID: {}", userId);
        if (!userId.equals("")) {
            req.setUserId(userId);
        }
        return this.service.controlParam(id, req);
    }

    @ApiOperation(value = "CCTV PTZ 제어", response = CctvPtzControlDto.CctvPtzControlRes.class)
    @PostMapping(value = "/ptz/{id}", produces = {"application/json; charset=utf8"})
    public CctvPtzControlDto.CctvPtzControlRes controlPtz(
            @ApiParam(name = "id", value = "제어기번호", example = "1001", required = true)
            @PathVariable("id") Long id,
            @ApiParam(name = "req", value = "CCTV PTZ 제어 정보", example = "[tilt-up, start, 20, ADMIN]", required = true)
            @RequestBody @Valid final CctvPtzControlDto.CctvControlPtzReq req,
            HttpServletRequest request) {
        String userId = this.loginController.getSessionUserId(request);
        log.info("UUID: {}", userId);
        if (!userId.equals("")) {
            req.setUserId(userId);
        }
        return this.service.controlPtz(id, req);
    }

    @ApiOperation(value = "CCTV Preset 제어", response = CctvPresetControlDto.CctvPresetControlRes.class)
    @PostMapping(value = "/preset/{id}", produces = {"application/json; charset=utf8"})
    public CctvPresetControlDto.CctvPresetControlRes controlPreset(
            @ApiParam(name = "id", value = "제어기번호", example = "1001", required = true)
            @PathVariable("id") Long id,
            @ApiParam(name = "req", value = "CCTV Preset 제어 정보", example = "[preset, call, 1, 0, 0, ADMIN]", required = true)
            @RequestBody @Valid final CctvPresetControlDto.CctvPresetControlReq req,
            HttpServletRequest request) {
        String userId = this.loginController.getSessionUserId(request);
        log.info("UUID: {}", userId);
        if (!userId.equals("")) {
            req.setUserId(userId);
        }
        return this.service.controlPreset(id, req);
    }

    @ApiOperation(value = "CCTV 가변 문자 설정", response = CctvVarCharControlDto.CctvVarCharControlRes.class)
    @PostMapping(value = "/var-char-set/{id}", produces = {"application/json; charset=utf8"})
    public CctvVarCharControlDto.CctvVarCharControlRes controlVarCharSet(
            @ApiParam(name = "id", value = "제어기번호", example = "1001", required = true)
            @PathVariable("id") Long id,
            @ApiParam(name = "req", value = "CCTV 가변문자 설정 정보", example = "[preset, call, 1, 0, 0, ADMIN]", required = true)
            @RequestBody @Valid final CctvVarCharControlDto.CctvVarCharControlSetReq req,
            HttpServletRequest request) {
        String userId = this.loginController.getSessionUserId(request);
        log.info("UUID: {}", userId);
        if (!userId.equals("")) {
            req.setUserId(userId);
        }
        return this.service.controlVarCharSet(id, req);
    }

    @ApiOperation(value = "CCTV 가변 문자 삭제", response = CctvVarCharControlDto.CctvVarCharControlRes.class)
    @PostMapping(value = "/var-char-del/{id}", produces = {"application/json; charset=utf8"})
    public CctvVarCharControlDto.CctvVarCharControlRes controlVarCharDel(
            @ApiParam(name = "id", value = "제어기번호", example = "1001", required = true)
            @PathVariable("id") Long id,
            @ApiParam(name = "req", value = "CCTV 가변문자 삭제 정보", example = "[preset, call, 1, 0, 0, ADMIN]", required = true)
            @RequestBody @Valid final CctvVarCharControlDto.CctvVarCharControlDelReq req,
            HttpServletRequest request) {
        String userId = this.loginController.getSessionUserId(request);
        log.info("UUID: {}", userId);
        if (!userId.equals("")) {
            req.setUserId(userId);
        }
        return this.service.controlVarCharDel(id, req);
    }

    @ApiOperation(value = "CCTV RESET", response = CctvControlDto.CctvControlResetRes.class)
    @PostMapping(value = "/reset/{id}", produces = {"application/json; charset=utf8"})
    public CctvControlDto.CctvControlResetRes controlReset(
            @ApiParam(name = "id", value = "제어기번호", example = "1001", required = true)
            @PathVariable("id") Long id,
            @ApiParam(name = "req", value = "CCTV 제어기 리셋", example = "[0/1, ADMIN]", required = true)
            @RequestBody @Valid final CctvControlDto.CctvControlResetReq 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 = "CCTV PTZ 제어", response = CctvControlDto.CctvControlRes.class)
//    @PostMapping(value = "/ptz/{id}", produces = {"application/json; charset=utf8"})
//    public CctvControlDto.CctvControlRes controlPtz(
//            @ApiParam(name = "id", value = "제어기번호", example = "1", required = true)
//            @PathVariable("id") Long id,
//            @ApiParam(name = "req", value = "CCTV PTZ 제어 정보", example = "[tilt-up, start, 20, ADMIN]", required = true)
//            @RequestBody @Valid final CctvControlDto.CctvControlPtzReq req,
//            HttpServletRequest request) {
//        String userId = this.loginController.getSessionUserId(request);
//        log.info("UUID: {}", userId);
//        if (!userId.equals("")) {
//            req.setUserId(userId);
//        }
//        return this.service.controlPtz(id, req);
//    }
//
//    @ApiOperation(value = "CCTV Preset 제어", response = CctvControlDto.CctvControlRes.class)
//    @PostMapping(value = "/preset/{id}", produces = {"application/json; charset=utf8"})
//    public CctvControlDto.CctvControlRes controlPreset(
//            @ApiParam(name = "id", value = "제어기번호", example = "1", required = true)
//            @PathVariable("id") Long id,
//            @ApiParam(name = "req", value = "CCTV Preset 제어 정보", example = "[preset, call, 1, 0, 0, ADMIN]", required = true)
//            @RequestBody @Valid final CctvControlDto.CctvControlPresetReq req,
//            HttpServletRequest request) {
//        String userId = this.loginController.getSessionUserId(request);
//        log.info("UUID: {}", userId);
//        if (!userId.equals("")) {
//            req.setUserId(userId);
//        }
//        return this.service.controlPreset(id, req);
//    }
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}
