package info.knacki.prometheusandroidexporter;

import android.content.Context;

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

public class CollectorManager {
    private static CollectorManager sInstance = null;
    private final HashMap<String, ICollector> fCollectors;
    private HashMap<String, Collection<CollectorType.CollectorValue>> fValues;

    private CollectorManager(){
        fCollectors = new HashMap<>();
    }

    public void RegisterCollector(ICollector collector) {
        fCollectors.put(collector.getClass().getName(), collector);
    }

    public void tick(Context ctx) {
        HashMap<String, Collection<CollectorType.CollectorValue>> values = new HashMap<>();

        for (HashMap.Entry<String, ICollector> i : fCollectors.entrySet()) {
            values.put(i.getKey(), i.getValue().ReadValues(ctx));
        }
        fValues = values;
    }

    public static CollectorManager GetInstance() {
        synchronized (CollectorManager.class) {
            if (sInstance == null) {
                synchronized (CollectorManager.class) {
                    return sInstance = new CollectorManager();
                }
            }
            return sInstance;
        }
    }

    public interface Runnable {
        void Run(CollectorType.CollectorValue value);
    }

    public void forEach(Runnable action) {
        for (Map.Entry<String, Collection<CollectorType.CollectorValue>> i: fValues.entrySet()) {
            for (CollectorType.CollectorValue j: i.getValue())
                action.Run(j);
        }
    }
}
