package com.example.quiz.data.repository;

import android.util.Log;
import android.widget.Toast;

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

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

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

    public LiveData<Result<Pari>> createPari(Pari pari) {
        MutableLiveData<Result<Pari>> pariResponse = new MutableLiveData<>();
        pariResponse.setValue(Result.loading());

        apiService.createPari(pari).enqueue(new Callback<Pari>() {
            @Override
            public void onResponse(Call<Pari> call, Response<Pari> response) {

                if (response.isSuccessful() && response.body() != null) {
                    pariResponse.postValue(Result.success(response.body()));
                } else {
                    // On récupère l'erreur exacte envoyée par le backend
                    try {
                        String errorBody = response.errorBody() != null ?
                                response.errorBody().string() : "Erreur inconnue";

                        pariResponse.postValue(Result.error(errorBody));

                    } catch (Exception e) {
                        pariResponse.postValue(Result.error("Erreur serveur"));
                    }
                }
            }

            @Override
            public void onFailure(Call<Pari> call, Throwable t) {
                pariResponse.postValue(Result.error(t.getMessage()));
            }
        });

        return pariResponse;
    }

    public LiveData<Result<List<Pari>>> derniersParis(String createdBy){
        MutableLiveData<Result<List<Pari>>> derniersParis = new MutableLiveData<>();
        derniersParis.setValue(Result.loading());
        apiService.derniersParis(createdBy).enqueue(new Callback<List<Pari>>() {
            @Override
            public void onResponse(Call<List<Pari>> call, Response<List<Pari>> response) {
                if(response.isSuccessful() && response.body() != null){
                    derniersParis.postValue(Result.success(response.body()));
                }else{
                    try{
                        String errorBody = response.errorBody() != null ?
                                response.errorBody().string() : "Erreur inconnue";

                        derniersParis.postValue(Result.error(errorBody));
                    }catch (Exception e){
                        derniersParis.postValue(Result.error(e.getMessage()));
                    }
                }
            }

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

        return derniersParis;
    }

    public LiveData<Result<Pari>> annulerPari(String numeroTicket){
        MutableLiveData<Result<Pari>> pariResponse = new MutableLiveData<>();
        pariResponse.setValue(Result.loading());
        apiService.annulerPari(numeroTicket).enqueue(new Callback<Pari>(){
            @Override
            public void onResponse(Call<Pari> call, Response<Pari> response) {
                if(response.isSuccessful()){
                    pariResponse.postValue(Result.success(response.body()));
                }else{
                    try{
                        String errorBody = response.errorBody() != null ?
                                response.errorBody().string() : "Erreur inconnue";

                        pariResponse.postValue(Result.error(errorBody));
                    }catch (Exception e){
                        pariResponse.postValue(Result.error(e.getMessage()));
                    }
                }
            }

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

    public LiveData<Result<Double>> getSoldeByCourse(String createdBy, String courseId){
        MutableLiveData<Result<Double>> solde = new MutableLiveData<>();
        solde.setValue(Result.loading());
        apiService.getSoldeByCourse(createdBy, courseId).enqueue(new Callback<Double>() {
            @Override
            public void onResponse(Call<Double> call, Response<Double> response) {
                if(response.isSuccessful()){
                    solde.postValue(Result.success(response.body()));
                }else{
                    try{
                        solde.postValue(Result.error(response.errorBody().string()));
                    }catch (Exception e){
                        solde.postValue(Result.error(e.getMessage()));
                    }
                }
            }

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

    public LiveData<Result<Double>> getSoldeByDay(String createdBy, String day){
        MutableLiveData<Result<Double>> solde = new MutableLiveData<>();
        solde.setValue(Result.loading());
        apiService.getSoldeByDay(createdBy, day).enqueue(new Callback<Double>() {
            @Override
            public void onResponse(Call<Double> call, Response<Double> response) {
                if(response.isSuccessful()){
                    solde.postValue(Result.success(response.body()));
                }else{
                    try {
                        solde.postValue(Result.error(response.errorBody().string()));
                    }catch (Exception e){
                        solde.postValue(Result.error(e.getMessage()));
                    }
                }
            }

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

}
