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.model.PagedModel;
import com.example.quiz.data.remote.ApiService;
import com.example.quiz.utils.Result;

import javax.inject.Inject;

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

public class CourseRepository {
    private ApiService apiService;
    private final Course.Statut OPENED_STATUT = Course.Statut.OUVERT;

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

    public LiveData<Result<PagedModel<Course>>> getCourses(String reunionDate) {
        MutableLiveData<Result<PagedModel<Course>>> liveCourses = new MutableLiveData<Result<PagedModel<Course>>>();
        liveCourses.setValue(Result.loading());
        apiService.getCourses(reunionDate,String.valueOf(OPENED_STATUT)).enqueue(new Callback<PagedModel<Course>>() {
            @Override
            public void onResponse(Call<PagedModel<Course>> call, Response<PagedModel<Course>> response) {
                if(response.isSuccessful()){
                    liveCourses.postValue(Result.success(response.body()));
//                    PagedModel<Course> openedPagesCourses = new PagedModel<>();
//                    List<Course> listOfCourses = new ArrayList<>();
//                    for(Course course: response.body().getContent()){
//                        if(course.getStatut().equals(OPENED_STATUT)){
//                            listOfCourses.add(course);
//                        }
//                    }
////                    openedPagesCourses.setTotalPages(response.body().getTotalPages());
////                    openedPagesCourses.setTotalElements(response.body().getTotalElements());
////                    openedPagesCourses.setPageable(response.body().getPageable());
////                    openedPagesCourses.setNumberOfElements(response.body().getNumberOfElements());
////                    openedPagesCourses.setSize(response.body().getSize());
////                    openedPagesCourses.setContent(listOfCourses);
//                    liveCourses.postValue(Result.success(response.body()));
                }else{
                    liveCourses.postValue(Result.error(response.message()));
                }
            }

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