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

import com.its.op.dto.its.vms.TbVmsFormDto;
import com.its.op.dto.its.vms.TbVmsSymbLibDto;
import com.its.op.service.its.vms.TbVmsFormService;
import com.its.op.service.its.vms.TbVmsSymbLibSbt0Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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-2.폼관리-1.문자식/도형식 폼관리(CS-APP)")
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/cs-api/vms/form/manage")
public class CsVmsFormController {

    private final TbVmsFormService service;
    private final TbVmsSymbLibSbt0Service symbService;

    @ApiOperation(value = "VMS FORM 정보변경-폼정보만 업데이트(TB_VMS_FORM)", response = TbVmsFormDto.class)
    @PostMapping(value = "/update/{key}/{id}", produces = {"application/json; charset=utf8"})
    public TbVmsFormDto updateById(@PathVariable final String key, @PathVariable final Integer id, @RequestBody @Valid final TbVmsFormDto.TbVmsFormUpdReq req)
        throws NoSuchElementException {
        log.warn("Update Vms Form: {}, {}", key, id);
        if (!StringUtils.equals(key, "abcdefg1234567890x")) {
            throw new NoSuchElementException("Authentication key mismatched: " + id);
        }
        return this.service.updateById(id, req);
    }

    @ApiOperation(value = "VMS FORM 정보변경/생성/업데이트-개별-객체정보포함(TB_VMS_FORM)", response = TbVmsFormDto.class)
    @PostMapping(value = "/merge/{key}/{id}", produces = {"application/json; charset=utf8"})
    public TbVmsFormDto mergeInfo(@PathVariable final String key, @PathVariable("id") Integer id, @RequestBody @Valid final TbVmsFormDto.TbVmsFormUpdReq req)
        throws NoSuchElementException {
        log.warn("Merge Vms Form: {}, {}", key, id);
        if (!StringUtils.equals(key, "abcdefg1234567890x")) {
            throw new NoSuchElementException("Authentication key mismatched: " + id);
        }
        return this.service.mergeInfo(req);
    }

    @ApiOperation(value = "VMS FORM 정보삭제-개별(TB_VMS_FORM)", response = TbVmsFormDto.class)
    @PostMapping(value = "/delete/{key}/{id}", produces = {"application/json; charset=utf8"})
    public TbVmsFormDto deleteById(@PathVariable final String key, @PathVariable("id") Integer id)
        throws NoSuchElementException {
        log.warn("Delete Vms Form: {}, {}", key, id);
        if (!StringUtils.equals(key, "abcdefg1234567890x")) {
            throw new NoSuchElementException("Authentication key mismatched: " + id);
        }
        return this.service.deleteById(id);
    }

    @ApiOperation(value = "VMS FORM 복사 추가", response = TbVmsFormDto.class)
    @PostMapping(value = "/copy/{key}/{id}", produces = {"application/json; charset=utf8"})
    public TbVmsFormDto formCopyAdd(@PathVariable final String key, @PathVariable("id") Integer id, @RequestBody @Valid final TbVmsFormDto.TbVmsFormCopyReq req)
        throws NoSuchElementException {
        log.warn("Copy Vms Form: {}, {}, {}, {}", key, id, req.getVmsFormId(), req.getVmsFormNm());
        if (!StringUtils.equals(key, "abcdefg1234567890x")) {
            throw new NoSuchElementException("Authentication key mismatched: " + id);
        }
        return this.service.formCopyAdd(req);
    }

    @ApiOperation(value = "VMS 심벌이미지 정보변경/생성-개별(TB_VMS_SYMB_LIB)", response = TbVmsSymbLibDto.class)
    @PostMapping(value = "/symb/0/merge/{key}/{id}", produces = {"application/json; charset=utf8"})
    public TbVmsSymbLibDto mergeVmsSymbInfo(@PathVariable final String key, @PathVariable("id") Integer id, @RequestBody @Valid final TbVmsSymbLibDto.TbVmsSymbLibUpdReq req)
        throws NoSuchElementException {
        log.warn("Merge Vms Symbol0: {}, {}", key, id);
        if (!StringUtils.equals(key, "abcdefg1234567890x")) {
            throw new NoSuchElementException("Authentication key mismatched: " + id);
        }
        return this.symbService.mergeInfo(req);
    }

}
