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.model.ParisResponse;
import com.example.quiz.data.model.ResponseError;
import com.example.quiz.data.model.dtos.paris.CancelParisPaylaod;
import com.example.quiz.data.model.dtos.paris.SoldeResponse;
import com.example.quiz.data.remote.ApiService;
import com.example.quiz.utils.Result;
import com.google.gson.Gson;

import java.util.Collections;
import java.util.List;
import java.util.Map;

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<ParisResponse>> createPari(Pari pari) {
        MutableLiveData<Result<ParisResponse>> pariResponse = new MutableLiveData<>();
        pariResponse.setValue(Result.loading());

        apiService.createPari(pari).enqueue(new Callback<ParisResponse>() {
            @Override
            public void onResponse(Call<ParisResponse> call, Response<ParisResponse> 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 error = response.errorBody().string();
                        Gson gson = new Gson();
                        ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
                        pariResponse.postValue(Result.error(errorResponse.getMessage()));
                    } catch (Exception e) {
                        pariResponse.postValue(Result.error("Erreur serveur"));
                    }
                }
            }

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

        return pariResponse;
    }

    public LiveData<Result<List<ParisResponse>>> derniersParis(String agentId){
        MutableLiveData<Result<List<ParisResponse>>> derniersParis = new MutableLiveData<>();
        derniersParis.setValue(Result.loading());
        apiService.derniersParis(agentId).enqueue(new Callback<List<ParisResponse>>() {
            @Override
            public void onResponse(Call<List<ParisResponse>> call, Response<List<ParisResponse>> response) {
                if(response.isSuccessful() && response.body() != null){
                    derniersParis.postValue(Result.success(response.body()));
                }else{
                    try{
                        String error = response.errorBody().string();
                        Gson gson = new Gson();
                        ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
                        derniersParis.postValue(Result.error(errorResponse.getMessage()));
                    }catch (Exception e){
                        derniersParis.postValue(Result.error(e.getMessage()));
                    }
                }
            }

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

        return derniersParis;
    }

    public LiveData<Result<ParisResponse>> payTicket(String numeroTicket){
        MutableLiveData<Result<ParisResponse>> pariResponse = new MutableLiveData<>();
        pariResponse.setValue(Result.loading());
        apiService.payerPari(numeroTicket).enqueue(new Callback<ParisResponse>(){
            @Override
            public void onResponse(Call<ParisResponse> call, Response<ParisResponse> response) {
                if(response.isSuccessful()){
                    pariResponse.postValue(Result.success(response.body()));
                }else{
                    try{
                        String error = response.errorBody().string();
                        Gson gson = new Gson();
                        ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
                        pariResponse.postValue(Result.error(errorResponse.getMessage()));
                    }catch (Exception e){
                        pariResponse.postValue(Result.error(e.getMessage()));
                    }
                }
            };
            @Override
            public void onFailure(Call<ParisResponse> call, Throwable throwable) {
                pariResponse.postValue(Result.error(throwable.getMessage()));
            }
        });
        return pariResponse;
    }

    public LiveData<Result<ParisResponse>> rembourserTicket(String numeroTicket){
        MutableLiveData<Result<ParisResponse>> pariResponse = new MutableLiveData<>();
        pariResponse.setValue(Result.loading());
        apiService.rembourserPari(numeroTicket).enqueue(new Callback<ParisResponse>(){
            @Override
            public void onResponse(Call<ParisResponse> call, Response<ParisResponse> response) {
                if(response.isSuccessful()){
                    pariResponse.postValue(Result.success(response.body()));
                }else{
                    try{
                        String error = response.errorBody().string();
                        Gson gson = new Gson();
                        ResponseError errorResponse = gson.fromJson(error, ResponseError.class);
                        pariResponse.postValue(Result.error(errorResponse.getMessage()));
                    }catch (Exception e){
                        pariResponse.postValue(Result.error(e.getMessage()));
                    }
                }
            }
            @Override
            public void onFailure(Call<ParisResponse> call, Throwable throwable) {
                pariResponse.postValue(Result.error(throwable.getMessage()));
            }
        });
        return pariResponse;
    }

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

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

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

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

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

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

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

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

}
