package com.tencent.trtc.event;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Events {
    private static final String PREFIX = "com.tencent.trtc.event";
    private static final Map<String, List<Listener>> events = new HashMap();
    public static void fireEvent(String event){
        fireEvent(event,null);
    }
    public static void fireEvent(String event, Extra extra){
        if(events.containsKey(PREFIX+event)){
            for(Listener listener : events.get(PREFIX+event)){
                Bundle bundle = extra == null ? Bundle.EMPTY: new Bundle();
                if(extra != null){
                    extra.extra(bundle);
                }
                listener.on(extra == null ? Bundle.EMPTY : bundle);
            }
        }
    }

    public static void addListener(String eventName,Listener listener){
        if(!events.containsKey(PREFIX+eventName)){
            events.put(PREFIX+eventName,new ArrayList<>());
        }
        List<Listener> list =  events.get(PREFIX+eventName);
        if(!list.contains(listener)){
            list.add(listener);
        }
    }

    public static void removeListener(String eventName,Listener listener){
        if(!events.containsKey(PREFIX+eventName)){
            events.put(PREFIX+eventName,new ArrayList<>());
        }
        List<Listener> list =  events.get(PREFIX+eventName);
        if(list.contains(listener)){
            list.remove(listener);
        }
    }


    public interface Extra{
        public void extra(Bundle extra);
    }
}
