package info.knacki.prometheusandroidexporter.collector;

import android.content.Context;

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

import info.knacki.prometheusandroidexporter.CollectorValue;
import info.knacki.prometheusandroidexporter.ICollector;

public class TestCollector implements ICollector {
    private final CollectorValue countCollector = new CollectorValue("test_counter", "A count that counts", CollectorValue.Type.COUNTER);
    private final CollectorValue timerCollector = new CollectorValue("test_timer", "A clock that counts", CollectorValue.Type.COUNTER);
    private final CollectorValue startedAtCollector = new CollectorValue("test_started", "A clock that counts", CollectorValue.Type.COUNTER);
    private final long serviceStart = System.currentTimeMillis();
    private int count =0;

    private CollectorValue ReadCounter() {
        CollectorValue val = new CollectorValue(countCollector);
        val.SetValue(count++);
        return val;
    }

    private CollectorValue ReadTimer() {
        CollectorValue val = new CollectorValue(timerCollector);
        Calendar cal = Calendar.getInstance();
        int minutes = cal.get(Calendar.MINUTE);
        val.SetValue(cal.get(Calendar.HOUR_OF_DAY) +(minutes < 10 ? "0" : "") +minutes);
        return val;
    }

    private CollectorValue ServiceStartedAt() {
        CollectorValue val = new CollectorValue(startedAtCollector);
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(serviceStart);
        int minutes = cal.get(Calendar.MINUTE);
        val.SetValue(cal.get(Calendar.HOUR_OF_DAY) +(minutes < 10 ? "0" : "") +minutes);
        return val;
    }

    @Override
    public Collection<CollectorValue> ReadValues(Context ctx) {
        ArrayDeque<CollectorValue> result = new ArrayDeque<>();
        result.add(ReadCounter());
        result.add(ReadTimer());
        result.add(ServiceStartedAt());
        return result;
    }
}
