package com.example.quiz.data.repository;

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

import com.example.quiz.data.model.PagedModel;
import com.example.quiz.data.model.PointDeVente;
import com.example.quiz.data.model.ResponseError;
import com.example.quiz.data.remote.ApiService;
import com.example.quiz.utils.Result;
import com.google.gson.Gson;


import java.io.IOException;

import javax.inject.Inject;

import retrofit2.*;

public class PointDeVenteRepository {

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

    public LiveData<Result<PagedModel<PointDeVente>>> getPointsDeVente(String nom) {
        MutableLiveData<Result<PagedModel<PointDeVente>>> result = new MutableLiveData<>();
        result.setValue(Result.loading());

        apiService.getPointsDeVente(nom)
                .clone()
                .enqueue(new Callback<PagedModel<PointDeVente>>() {

            @Override
            public void onResponse(Call<PagedModel<PointDeVente>> call,
                                   Response<PagedModel<PointDeVente>> response) {

                if (response.isSuccessful() && response.body() != null) {
                    result.postValue(Result.success(response.body()));
                } else {
                    try {
                        String error = response.errorBody().string();
                        Gson gson = new Gson();
                        ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
                        result.postValue(Result.error(errorResponse.getMessage()));
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }

            @Override
            public void onFailure(Call<PagedModel<PointDeVente>> call, Throwable t) {
                result.postValue(Result.error(
                        t.getMessage() != null ? t.getMessage() : "Erreur réseau"
                ));
            }
        });

        return result;
    }


    public LiveData<Result<PointDeVente>> getPointDeVenteById(String id) {
        MutableLiveData<Result<PointDeVente>> result = new MutableLiveData<>();
        result.setValue(Result.loading());
        apiService.getPointDeVenteById(id).enqueue(new Callback<PointDeVente>() {
            @Override
            public void onResponse(Call<PointDeVente> call, Response<PointDeVente> response) {
                if(response.isSuccessful()){
                    result.postValue(Result.success(response.body()));
                }else{
                    try {
                        String error = response.errorBody().string();
                        Gson gson = new Gson();
                        ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
                        result.postValue(Result.error(errorResponse.getMessage()));
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }

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

}
