package com.example.vdcscore.data;

import androidx.annotation.NonNull;

import com.example.vdcscore.models.Club;
import com.example.vdcscore.models.Jornada;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class FirebaseRepository {

    private final DatabaseReference mDatabase;
    private static FirebaseRepository instance;

    private FirebaseRepository() {
        mDatabase = FirebaseDatabase.getInstance().getReference();
    }

    public static synchronized FirebaseRepository getInstance() {
        if (instance == null) {
            instance = new FirebaseRepository();
        }
        return instance;
    }

    // --- Clubs ---

    public void addClub(Club club, OnCompleteListener<Void> onCompleteListener) {
        String key = String.valueOf(club.getId());
        if (key == null || key.isEmpty()) {
            key = mDatabase.child("clubs").push().getKey();
            club.setId(Integer.parseInt(key));
        }
        mDatabase.child("clubs").child(key).setValue(club).addOnCompleteListener(onCompleteListener);
    }

    public void getClubs(final DataCallback<List<Club>> callback) {
        mDatabase.child("clubs").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                List<Club> clubs = new ArrayList<>();
                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    Club club = postSnapshot.getValue(Club.class);
                    if (club != null) {
                        club.setId(Integer.parseInt(postSnapshot.getKey()));
                        clubs.add(club);
                    }
                }
                callback.onSuccess(clubs);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                callback.onFailure(error.toException());
            }
        });
    }

    // --- Jornadas ---

    public void addJornada(Jornada jornada, OnCompleteListener<Void> onCompleteListener) {
        String key = jornada.getId();
        if (key == null || key.isEmpty()) {
            key = mDatabase.child("jornadas").push().getKey();
            jornada.setId(key);
        }
        mDatabase.child("jornadas").child(key).setValue(jornada).addOnCompleteListener(onCompleteListener);
    }

    public void getJornadas(final DataCallback<List<Jornada>> callback) {
        mDatabase.child("jornadas").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                List<Jornada> jornadas = new ArrayList<>();
                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    Jornada jornada = postSnapshot.getValue(Jornada.class);
                    if (jornada != null) {
                        jornada.setId(postSnapshot.getKey());
                        jornadas.add(jornada);
                    }
                }
                callback.onSuccess(jornadas);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                callback.onFailure(error.toException());
            }
        });
    }

    public interface DataCallback<T> {
        void onSuccess(T data);

        void onFailure(Exception e);
    }
}
