package com.its.op.controller.its.vms;

import com.its.op.controller.its.LoginController;
import com.its.op.dto.its.vms.VmsControlDto;
import com.its.op.service.its.vms.VmsControlService;
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.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.NoSuchElementException;

@Slf4j
@Api(tags = "12.VMS-0.제어(CS-APP)")
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/cs-api/vms/control")
public class CsVmsControlController {

    private final VmsControlService service;
    private final LoginController loginController;

    @ApiOperation(value = "VMS 제어기 리셋", response = VmsControlDto.VmsControlRes.class)
    @PostMapping(value = "/reset/{key}/{ctlrNmbr}", produces = {"application/json; charset=utf8"})
    public VmsControlDto.VmsControlRes controlReset(
            @PathVariable final String key,
            @ApiParam(name = "ctlrNmbr", value = "제어기번호", example = "10021", required = true)
            @PathVariable("ctlrNmbr") Long ctlrNmbr,
            @ApiParam(name = "req", value = "VMS 제어기 제어 정보", example = "[ADMIN]", required = true)
            @RequestBody @Valid final VmsControlDto.VmsControlReq req) throws NoSuchElementException {
        log.warn("Vms reset: {}, {}, {}", key, ctlrNmbr, req);
        if (!StringUtils.equals(key, "abcdefg1234567890x")) {
            throw new NoSuchElementException("Authentication key mismatched: " + ctlrNmbr);
        }
        return this.service.controlReset(ctlrNmbr, req);
    }

    @ApiOperation(value = "VMS 제어기 전광판 ON/OFF 제어", response = VmsControlDto.VmsControlRes.class)
    @PostMapping(value = "/power/{key}/{ctlrNmbr}", produces = {"application/json; charset=utf8"})
    public VmsControlDto.VmsControlRes controlPower(
            @PathVariable final String key,
            @ApiParam(name = "id", value = "제어기번호", example = "10021", required = true)
            @PathVariable("ctlrNmbr") Long ctlrNmbr,
            @ApiParam(name = "req", value = "VMS 제어기 제어 정보", example = "[ADMIN]", required = true)
            @RequestBody @Valid final VmsControlDto.VmsControlReq req) throws NoSuchElementException {
        log.warn("Vms power: {}, {}, {}", key, ctlrNmbr, req);
        if (!StringUtils.equals(key, "abcdefg1234567890x")) {
            throw new NoSuchElementException("Authentication key mismatched: " + ctlrNmbr);
        }
        return this.service.controlPower(ctlrNmbr, req);
    }

    @ApiOperation(value = "VMS 제어기 휘도 제어", response = VmsControlDto.VmsControlRes.class)
    @PostMapping(value = "/luminance/{key}/{ctlrNmbr}", produces = {"application/json; charset=utf8"})
    public VmsControlDto.VmsControlRes controlLuminance(
            @PathVariable final String key,
            @ApiParam(name = "id", value = "제어기번호", example = "10021", required = true)
            @PathVariable("ctlrNmbr") Long ctlrNmbr,
            @ApiParam(name = "req", value = "VMS 제어기 제어 정보", example = "[ADMIN]", required = true)
            @RequestBody @Valid final VmsControlDto.VmsControlReq req) throws NoSuchElementException {
        log.warn("Vms luminance: {}, {}, {}", key, ctlrNmbr, req);
        if (!StringUtils.equals(key, "abcdefg1234567890x")) {
            throw new NoSuchElementException("Authentication key mismatched: " + ctlrNmbr);
        }
        return this.service.controlLuminance(ctlrNmbr, req);
    }

    @ApiOperation(value = "VMS 제어기 파라미터 설정", response = VmsControlDto.VmsControlRes.class)
    @PostMapping(value = "/param/{key}/{ctlrNmbr}", produces = {"application/json; charset=utf8"})
    public VmsControlDto.VmsControlRes controlLuminance(
            @PathVariable final String key,
            @ApiParam(name = "ctlrNmbr", value = "제어기번호", example = "10021", required = true)
            @PathVariable("ctlrNmbr") Long ctlrNmbr,
            @ApiParam(name = "req", value = "VMS 제어기 파라미터 설정 정보", example = "[ADMIN]", required = true)
            @RequestBody @Valid final VmsControlDto.VmsControlParamReq req) throws NoSuchElementException {
        log.warn("Vms param: {}, {}, {}", key, ctlrNmbr, req);
        if (!StringUtils.equals(key, "abcdefg1234567890x")) {
            throw new NoSuchElementException("Authentication key mismatched: " + ctlrNmbr);
        }
        return this.service.controlParam(ctlrNmbr, req);
    }

    @ApiOperation(value = "VMS 제어기 전광판 ON-OFF 시각 설정", response = VmsControlDto.VmsControlRes.class)
    @PostMapping(value = "/on-off-time/{key}/{ctlrNmbr}", produces = {"application/json; charset=utf8"})
    public VmsControlDto.VmsControlRes controlOnOffTime(
            @PathVariable final String key,
            @ApiParam(name = "ctlrNmbr", value = "제어기번호", example = "10021", required = true)
            @PathVariable("ctlrNmbr") Long ctlrNmbr,
            @ApiParam(name = "req", value = "VMS 제어기 전광판 ON-OFF 시각 설정 정보", example = "[ADMIN]", required = true)
            @RequestBody @Valid final VmsControlDto.VmsControlOnOffTimeReq req) throws NoSuchElementException {
        log.warn("Vms on-off-time: {}, {}, {}", key, ctlrNmbr, req);
        if (!StringUtils.equals(key, "abcdefg1234567890x")) {
            throw new NoSuchElementException("Authentication key mismatched: " + ctlrNmbr);
        }
        return this.service.controlOnOffTime(ctlrNmbr, req);
    }

    @ApiOperation(value = "VMS 제어기 폼 시나리오 즉시 다운로드", response = VmsControlDto.VmsControlRes.class)
    @PostMapping(value = "/imm-scnr-dnld/{key}/{ctlrNmbr}", produces = {"application/json; charset=utf8"})
    public VmsControlDto.VmsControlRes controlImmScnrDnld(
            @PathVariable final String key,
            @ApiParam(name = "ctlrNmbr", value = "제어기번호", example = "10021", required = true)
            @PathVariable("ctlrNmbr") Long ctlrNmbr,
            @ApiParam(name = "req", value = "VMS 제어기 폼 시나리오 즉시 다운로드 사용자 정보", example = "[ADMIN]", required = true)
            @RequestBody @Valid final VmsControlDto.VmsControlReq req) throws NoSuchElementException {
        log.warn("Vms opr-mode: {}, {}", key, ctlrNmbr);
        if (!StringUtils.equals(key, "abcdefg1234567890x")) {
            throw new NoSuchElementException("Authentication key mismatched: " + ctlrNmbr);
        }
        return this.service.controlImmScnrDnld(ctlrNmbr, req);
    }

    @ApiOperation(value = "VMS 제어기 운영모드 설정", response = VmsControlDto.VmsControlRes.class)
    @PostMapping(value = "/opr-mode/{key}/{ctlrNmbr}", produces = {"application/json; charset=utf8"})
    public VmsControlDto.VmsControlRes controlOprMode(
            @PathVariable final String key,
            @ApiParam(name = "ctlrNmbr", value = "제어기번호", example = "10021", required = true)
            @PathVariable("ctlrNmbr") Long ctlrNmbr,
            @ApiParam(name = "req", value = "VMS 제어기 운영모드 설정 정보(A:자동, F:고정, B:기본)", example = "[A]", required = true)
            @RequestBody @Valid final VmsControlDto.VmsControlOprModeReq req) throws NoSuchElementException {
        log.warn("Vms opr-mode: {}, {}, {}", key, ctlrNmbr, req);
        if (!StringUtils.equals(key, "abcdefg1234567890x")) {
            throw new NoSuchElementException("Authentication key mismatched: " + ctlrNmbr);
        }
        return this.service.controlOprMode(ctlrNmbr, req);
    }

    @ApiOperation(value = "VMS 이미지 심볼 재로딩 요청", response = VmsControlDto.VmsControlRes.class)
    @PostMapping(value = "/notify/image-symbol", produces = {"application/json; charset=utf8"})
    public VmsControlDto.VmsControlRes controlNotifyImageSymbol() {
        return this.service.controlNotifyImageSymbol();
    }

    @ApiOperation(value = "VMS 이미지 트레픽 재로딩 요청", response = VmsControlDto.VmsControlRes.class)
    @PostMapping(value = "/notify/image-traffic", produces = {"application/json; charset=utf8"})
    public VmsControlDto.VmsControlRes controlNotifyImageTraffic() {
        return this.service.controlNotifyImageTraffic();
    }

    @ApiOperation(value = "VMS 폼 재로딩 요청", response = VmsControlDto.VmsControlRes.class)
    @PostMapping(value = "/notify/vms-form", produces = {"application/json; charset=utf8"})
    public VmsControlDto.VmsControlRes controlNotifyVmsForm() {
        return this.service.controlNotifyVmsForm();
    }

    @ApiOperation(value = "VMS 정보제공구간 재로딩 요청", response = VmsControlDto.VmsControlRes.class)
    @PostMapping(value = "/notify/vms-ifsc", produces = {"application/json; charset=utf8"})
    public VmsControlDto.VmsControlRes controlNotifyVmsIfsc() {
        return this.service.controlNotifyVmsIfsc();
    }
}
