package com.base.core;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import com.base.core.handler.MappingCore;
import com.base.core.handler.StripExecuteHandler;
import com.base.core.model.enums.StripAccessTypeEnums;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

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

@Data
@Component
@Slf4j
public class StripExecuteInitialize implements InitializingBean {
    public Map<StripAccessTypeEnums, StripExecuteHandler> map = new HashMap<>();

    @Resource
    List<StripExecuteHandler> stripExecuteHandlerList;


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

    }
    private Boolean formatHandler() {
        //业务调用赋值
        if (CollectionUtils.isEmpty(stripExecuteHandlerList)) {
            log.error("stripExecute nothing to do");
            return Boolean.FALSE;
        }
        for (StripExecuteHandler stripExecuteHandler : stripExecuteHandlerList) {
            Class<?> beanClazz = stripExecuteHandler.getClass();
            MappingCore annotation = beanClazz.getAnnotation(MappingCore.class);
            if (annotation == null) {
                log.error("stripExecuteHandler {} not found mapping", beanClazz);
            }
            assert annotation != null;
            StripAccessTypeEnums stripAccessTypeEnums = annotation.stripAccessTypeEnums();
            map.put(stripAccessTypeEnums, stripExecuteHandler);
        }
        return Boolean.TRUE;
    }

}
