package com.ycwl.basic.notify.starter;

import com.ycwl.basic.notify.NotifyFactory;
import com.ycwl.basic.notify.adapters.INotifyAdapter;
import com.ycwl.basic.notify.starter.config.NotifyConfigItem;
import com.ycwl.basic.notify.starter.config.OverallNotifyConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;

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

    private void loadConfig() {
        for (NotifyConfigItem item : config.getConfigs()) {
            INotifyAdapter adapter = NotifyFactory.get(item.getType());
            adapter.loadConfig(item.getConfig());
            NotifyFactory.register(item.getName(), adapter);
        }
    }
}
