package info.knacki.prometheusandroidexporter;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import info.knacki.prometheusandroidexporter.collector.TestCollector;

public class MainService extends Service {
    public MainService() {
    }

    @Override
    public void onCreate() {
        InitCollectors(CollectorManager.GetInstance());
        ScheduleCollection();
        try {
            HttpService.Register(getApplicationContext());
        }
        catch (IOException e) {
            Logger.getLogger(MainService.class.getName()).log(Level.SEVERE, "Cannot start server: ", e);
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void InitCollectors(CollectorManager manager) {
        manager.RegisterCollector(new TestCollector());
    }

    private void ScheduleCollection() {
        PeriodicWorkRequest job = new PeriodicWorkRequest.Builder(CollectorWorker.class, 15, TimeUnit.MINUTES).build();
        WorkManager.getInstance(this).enqueue(job);
    }
}
