package info.knacki.prometheusandroidexporter.collector;

import android.app.ActivityManager;
import android.content.Context;

import java.util.ArrayDeque;
import java.util.Collection;

import info.knacki.prometheusandroidexporter.CollectorType;
import info.knacki.prometheusandroidexporter.ICollector;

public class MemoryCollector implements ICollector {
    private final CollectorType usedMemoryCollector = new CollectorType("memory_used_bytes", "Used memory", CollectorType.Type.GAUGE);
    private final CollectorType freeMemoryCollector = new CollectorType("memory_free_bytes", "Available memory", CollectorType.Type.GAUGE);
    private final CollectorType totalMemoryCollector = new CollectorType("memory_total_bytes", "Total memory", CollectorType.Type.GAUGE);

    @Override
    public Collection<CollectorType.CollectorValue> ReadValues(Context ctx) {
        ArrayDeque<CollectorType.CollectorValue> result = new ArrayDeque<>();
        ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
        ActivityManager manager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        if (manager != null) {
            manager.getMemoryInfo(memInfo);

            result.push(usedMemoryCollector.new CollectorValue().SetValue(memInfo.totalMem - memInfo.availMem));
            result.push(freeMemoryCollector.new CollectorValue().SetValue(memInfo.availMem));
            result.push(totalMemoryCollector.new CollectorValue().SetValue(memInfo.totalMem));
        }
        return result;
    }
}
