package com.example.quiz.data.repository;

import android.util.Log;

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

import com.example.quiz.data.model.Course;
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 CourseRepository {
    private ApiService apiService;

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

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

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