package com.example.quiz.data.repository;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.example.quiz.data.model.Reunion;

import com.example.quiz.data.remote.ApiService;
import com.example.quiz.utils.Result;


import java.util.List;

import javax.inject.Inject;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ReunionRepository {
    private ApiService apiService;

    @Inject
    public ReunionRepository(ApiService apiService) {
        this.apiService = apiService;
    }

    public LiveData<Result<List<Reunion>>> getReunions(String date){
        MutableLiveData<Result<List<Reunion>>> liveReunionsData = new MutableLiveData<>();
        liveReunionsData.setValue(Result.loading());
        apiService.getReunions(date).enqueue(new Callback<List<Reunion>>() {
            @Override
            public void onResponse(Call<List<Reunion>> call, Response<List<Reunion>> response) {
                if(response.isSuccessful()){
                    liveReunionsData.postValue(Result.success(response.body()));
                }else{
                    liveReunionsData.postValue(Result.error(response.message()));
                }
            }

            @Override
            public void onFailure(Call<List<Reunion>> call, Throwable throwable) {
                liveReunionsData.postValue(Result.error(throwable.getMessage()));
            }
        });
        return liveReunionsData;
    }

}
