package com.fizz.business.comm.OPC;

import com.fizz.business.constants.enums.ExitCutTypeEnum;
import com.fizz.business.constants.enums.OpcMessageType;
import com.fizz.business.domain.msg.*;
import com.fizz.business.scheduled.BaseSchedule;
import com.fizz.business.service.hanle.OpcReceiverHandler;
import com.kangaroohy.milo.model.ReadWriteEntity;
import com.kangaroohy.milo.service.MiloService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.fizz.business.service.manager.OpcMessageIdsManager.*;

@Slf4j
@Component
@AllArgsConstructor
@ConditionalOnProperty(prefix = "kangaroohy.milo", name = "enabled", havingValue = "true")
public class MessageReceiveSchedule extends BaseSchedule {

    @Resource
    MiloService miloService;

    @Resource
    private OpcReceiverHandler opcReceiverHandler;
    @Scheduled(fixedDelay = 1000)
    public void L1L2LineMeasure() {
        try {

            AppMeasureEntryMessage entry = new AppMeasureEntryMessage();
            writeMessage(entry,entryLineMeasureIds);

            AppMeasureCoatMessage coat = new AppMeasureCoatMessage();
            writeMessage(coat,procLineMeasureIds);

            AppMeasureFurnaceMessage fur = new AppMeasureFurnaceMessage();
            writeMessage(fur,furLineMeasureIds);

            AppMeasureExitMessage exit = new AppMeasureExitMessage();
            writeMessage(exit,exitLineMeasureIds);

            AppMeasureMessage msg = new AppMeasureMessage();
            msg.setAppMeasureEntryMessage(entry);
            msg.setAppMeasureCoatMessage(coat);
            msg.setAppMeasureFurnaceMessage(fur);
            msg.setAppMeasureExitMessage(exit);
            opcReceiverHandler.onMessageReceived(OpcMessageType.APP_MEASURE,msg);

        } catch (Exception e) {
//            throw new RuntimeException(e);
        }
    }

    private void writeMessage(OpcMessage msg, Map<String,String> msgIds) throws Exception {
        List<String> ids = new ArrayList<>(msgIds.keySet());
        ids.remove("trigger");
        List<ReadWriteEntity> nodes =  miloService.readFromOpcUa(ids);
        nodes.forEach(item->{
            String fieldName = msgIds.get(item.getIdentifier());
            if (fieldName != null) {
                try {
                    Field field = msg.getClass().getDeclaredField(fieldName);
                    if (field.getType() == ExitCutTypeEnum.class) {
                        // 使用枚举类的自定义方法
                        field.setAccessible(true);
                        ExitCutTypeEnum enumValue = ExitCutTypeEnum.fromCode((int)item.getValue());
                        field.set(msg, enumValue);
                    } else {
                        // 其他类型正常处理
                        BeanUtils.setProperty(msg, fieldName, item.getValue());
                    }
                } catch (Exception e) {
                    log.error("BeanUtils设置属性失败: {}", fieldName, e);
                }
            }
        });
    }
}
