package com.its.pis.xnetudp.service;

import com.its.app.AppUtils;
import com.its.pis.entity.TbVdsCtlr;
import com.its.pis.entity.TbVdsCtlrStts;
import com.its.pis.process.DbmsJobData;
import com.its.pis.process.DbmsJobProcess;
import com.its.pis.process.DbmsJobType;
import com.its.pis.xnetudp.protocol.CENTER_VDS_RES_STATE;
import com.its.pis.xnetudp.protocol.voVdsState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class CenterCommResponseService {

    public void notifyStatus(TbVdsCtlr vds, boolean isConnect) {

        CENTER_VDS_RES_STATE stts = new CENTER_VDS_RES_STATE((byte)0x00);
        stts.setTotal((short)1);
        stts.setError((short)(isConnect ? 0 : 1));
        stts.setNormal((short)(isConnect ? 1 : 0));
        stts.setModule((short)0);

        List<TbVdsCtlrStts> ctlrSttsList = Collections.synchronizedList(new ArrayList<>());

        voVdsState ss = vds.getVdsStts();
        stts.getObjList().add(ss);

        ctlrSttsList.add(vds.getStts());

        // 제어기 상태정보 DB 업데이트
        DbmsJobProcess dbmsJobProcess = (DbmsJobProcess) AppUtils.getBean(DbmsJobProcess.class);
        if (ctlrSttsList.size() > 0) {
            dbmsJobProcess.add(new DbmsJobData(DbmsJobType.DATA_TYPE_CTLR_STTS, false, ctlrSttsList));
        }

        // 제어기상태정보 UDP 전송(모든 운영단말로 전송)
//        CenterCommClientSender notify = (CenterCommClientSender) AppUtils.getBean(CenterCommClientSender.class);
//        notify.run(null, 0, stts.getBuffer());
    }

}
