package com.base.plan.trans;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@Component
@Slf4j
public class DataTransMissInitialize implements InitializingBean {
    public Map<DataTransTypeEnums, DataTransmissHandler> map = new HashMap<>();

    @Resource
    List<DataTransmissHandler> dataTransmissHandlerList;

    @Override
    public void afterPropertiesSet() throws Exception {
        formatHandler();

    }

    private Boolean formatHandler() {
        if (CollectionUtils.isEmpty(dataTransmissHandlerList)) {
            log.error("dataTransmiss nothing to do");
            return Boolean.FALSE;
        }
        for (DataTransmissHandler dataTransmissHandler : dataTransmissHandlerList) {
            Class<?> beanClazz = dataTransmissHandler.getClass();
            DataTransCore annotation = beanClazz.getAnnotation(DataTransCore.class);
            if (annotation == null) {
                log.error("dataTransmissHandler {} not found mapping", beanClazz);
            }
            assert annotation != null;
            DataTransTypeEnums dataTransTypeEnums = annotation.dataTransTypeEnums();
            map.put(dataTransTypeEnums, dataTransmissHandler);
        }
        return Boolean.TRUE;
    }

}