package com.ycwl.basic.notify;

import com.ycwl.basic.notify.adapters.INotifyAdapter;
import com.ycwl.basic.notify.adapters.ServerChanNotifyAdapter;
import com.ycwl.basic.notify.adapters.WxMpSrvNotifyAdapter;
import com.ycwl.basic.notify.enums.NotifyType;

import java.util.HashMap;
import java.util.Map;

public class NotifyFactory {
    public static INotifyAdapter get(NotifyType type) {
        return switch (type) {
            case SERVER_CHAN -> new ServerChanNotifyAdapter();
            case WX_MP_SRV -> new WxMpSrvNotifyAdapter();
            default -> throw new RuntimeException("不支持的通知类型");
        };
    }

    public static INotifyAdapter get(NotifyType type, Map<String, String> config) {
        INotifyAdapter adapter = get(type);
        adapter.loadConfig(config);
        return adapter;
    }

    protected static Map<String, INotifyAdapter> namedNotifier = new HashMap<>();
    protected static INotifyAdapter defaultNotifier = null;

    public static void register(String name, INotifyAdapter adapter) {
        namedNotifier.put(name, adapter);
    }

    public static INotifyAdapter via(String name) {
        INotifyAdapter adapter = namedNotifier.get(name);
        if (adapter == null) {
            throw new RuntimeException("未定义的通知方式："+name);
        }
        return adapter;
    }

    public static INotifyAdapter via() {
        if (defaultNotifier == null) {
            throw new RuntimeException("未定义默认通知方式");
        }
        return defaultNotifier;
    }

    public static void setDefault(String defaultStorage) {
        NotifyFactory.defaultNotifier = via(defaultStorage);
    }
}
