package com.ycwl.basic.facebody.starter;

import com.ycwl.basic.facebody.FaceBodyFactory;
import com.ycwl.basic.facebody.adapter.IFaceBodyAdapter;
import com.ycwl.basic.facebody.starter.config.FaceBodyConfig;
import com.ycwl.basic.facebody.starter.config.OverallFaceBodyConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FaceBodyAutoConfiguration {
    private final OverallFaceBodyConfig config;
    public FaceBodyAutoConfiguration(OverallFaceBodyConfig config) {
        this.config = config;
        if (config != null) {
            if (config.getConfigs() != null) {
                loadConfig();
            }
            if (StringUtils.isNotBlank(config.getDefaultUse())) {
                FaceBodyFactory.setDefault(config.getDefaultUse());
            }
        }
    }

    private void loadConfig() {
        for (FaceBodyConfig item : config.getConfigs()) {
            IFaceBodyAdapter adapter = FaceBodyFactory.getAdapter(item.getType());
            adapter.loadConfig(item.getConfig());
            FaceBodyFactory.register(item.getName(), adapter);
        }
    }
}
