package com.example.quiz.data.repository;

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

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

import javax.inject.Inject;

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


public class TpeRepository {

    private ApiService apiService;

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

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

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


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

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