package com.sonic.sdk.api.autoconfigure;

import com.sonic.sdk.api.annotation.EnableGatWayAuthScan;
import com.sonic.sdk.api.utils.GateWayAuthScanRunner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportAware;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;

@Slf4j
public class IgnoreAuthRegistConfig implements ImportAware {

    private String baseBackages;

    @Bean
    GateWayAuthScanRunner autoScanIgnoreAuth() {
        return new GateWayAuthScanRunner(baseBackages);
    }


    protected String getBasePackages(AnnotationMetadata importingClassMetadata) {
        Map<String, Object> attributes = importingClassMetadata
                .getAnnotationAttributes(EnableGatWayAuthScan.class.getCanonicalName());
        String pkg = (String) attributes.get("basePackages");
        return pkg;
    }


    @Override
    public void setImportMetadata(AnnotationMetadata annotationMetadata) {
        this.baseBackages = getBasePackages(annotationMetadata);
    }
}
