package xyz.wbsite.frame.listener;

import xyz.wbsite.frame.base.LocalData;

import java.util.Map;

public class FrameListener {

    private static FrameListener ourInstance = new FrameListener();

    public static FrameListener getInstance() {
        return ourInstance;
    }

    private FrameListener() {
    }

    public void onRes(String res) {
        Map<String, IResListener> beansOfType = LocalData.getApplicationContext().getBeansOfType(IResListener.class);
        for (IResListener resListener : beansOfType.values()) {
            resListener.onRes(res);
        }
    }

    public void onError(Throwable throwable) {
        Map<String, IErrorListener> beansOfType = LocalData.getApplicationContext().getBeansOfType(IErrorListener.class);
        for (IErrorListener errorListener : beansOfType.values()) {
            errorListener.error(throwable);
        }
    }

    public void onError(String message) {
        onError(new RuntimeException(message));
    }

    public void onTaskError(Throwable error) {
        Map<String, IErrorListener> beansOfType = LocalData.getApplicationContext().getBeansOfType(IErrorListener.class);
        for (IErrorListener errorListener : beansOfType.values()) {
            errorListener.error(error);
        }
    }

    public void onTaskError(String message) {
        onTaskError(new RuntimeException(message));
    }
}
