package com.its.vms.api.service;

import com.its.app.utils.ItsUtils;
import com.its.vms.api.dto.VmsControlDto;
import com.its.vms.domain.NET;
import com.its.vms.dto.TbVmsCtlrDto;
import com.its.vms.entity.TbVmsCtrlHs;
import com.its.vms.service.AppRepositoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@RequiredArgsConstructor
@Service
public class VmsControlService {

    private final AppRepositoryService repoService;
    //제어유형(V01:전광판ON,V02:전광판OFF,V03:제어기리셋,V04:함체환경설정,V05:전광판ON/OFF시각설정)
//    if      (sCtlType == "V01") sCtlType = lblV01->Caption;//"전광판ON";
//    else if (sCtlType == "V02") sCtlType = lblV02->Caption;//"전광판OFF";
//    else if (sCtlType == "V03") sCtlType = lblV03->Caption;//"제어기리셋";
//    else if (sCtlType == "V04") sCtlType = lblV04->Caption;//"함체환경설정";
//    else if (sCtlType == "V05") sCtlType = lblV05->Caption;//"전광판ON/OFF시각설정";
//    else if (sCtlType == "V06") sCtlType = lblV06->Caption;//"운영모드변경";
//    else if (sCtlType == "V07") sCtlType = lblV07->Caption;//"휘도제어";
//    else if (sCtlType == "V08") sCtlType = lblV08->Caption;//"기본메시지다운로드";
//    else if (sCtlType == "V09") sCtlType = lblV09->Caption;//"폼 표출주기";
//    else if (sCtlType == "V10") sCtlType = lblV10->Caption;//"폼 즉시 다운로드";

    /**
     * VMS 제어 이력 저장
     * @param dto
     */
    @Transactional
    public void saveCtrlHs(TbVmsCtrlHs dto) {
        //this.repoCtrlHs.insertHs(dto.getCtrlDt(), dto.getVmsCtlrNmbr(), dto.getCtrlType(), dto.getCtrlVal(), dto.getCtrlResult(), dto.getUserId());
    }

    /**
     * VMS 정보를 메모리에서 찾는다.
     * @param vmsCtlrNmbr
     * @return
     */
    public TbVmsCtlrDto findVms(Long vmsCtlrNmbr) {
        return this.repoService.getCtrlMap(vmsCtlrNmbr);
    }

    /**
     * VMS 제어기 리셋
     * @param vmsCtlrNmbr
     * @param req
     * @return
     */
    public VmsControlDto.VmsControlRes controlReset(Long vmsCtlrNmbr, VmsControlDto.VmsControlReq req) {
        log.warn("controlReset: VMS {}, controlReset {}.", vmsCtlrNmbr, req);
        TbVmsCtlrDto vmsObj = findVms(vmsCtlrNmbr);
        Integer error = 0;
        String message = "success";

        if (vmsObj == null) {
            error = 8;
            message = "알수 없는 제어기 입니다.";
        }
        else {
            if (vmsObj.getNetState() == NET.CLOSED) {
                error = 1;
                message = "통신이 연결되어 있지 않습니다.";
            }
            else {
                if (!vmsObj.reset()) {
                    error = 2;
                    message = "데이터 전송 중 오류가 발생 했습니다.";
                }
            }
        }

        saveCtrlHs(TbVmsCtrlHs.builder()
                .ctrlDt(ItsUtils.getSysTime())
                .vmsCtlrNmbr(vmsCtlrNmbr)
                .ctrlType("V03")
                .ctrlVal("Reset")
                .ctrlResult(error)
                .userId(req.getUserId())
                .build());
        return new VmsControlDto.VmsControlRes(error, message);
    }

}
