package com.atmob.voiceai.sdk.firebase;

import android.annotation.SuppressLint;
import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.atmob.common.logging.AtmobLog;
import com.atmob.voiceai.data.api.AtmobApi;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.FirebaseApp;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.messaging.FirebaseMessaging;

import java.util.ArrayList;

import javax.inject.Inject;
import javax.inject.Singleton;

import atmob.reactivex.rxjava3.internal.functions.Functions;
import atmob.rxjava.utils.RxJavaUtil;

@Singleton
public class FirebaseHelper {

    private static final String TAG = FirebaseHelper.class.getSimpleName();
    private final FirebaseApp firebaseApp;
    private final FirebaseAnalytics firebaseAnalytics;
    private final ArrayList<OnMessagingTokenChangeListener> onMessagingTokenChangeListeners;

    @Inject
    public FirebaseHelper(Application application) {
        firebaseApp = FirebaseApp.initializeApp(application);
        onMessagingTokenChangeListeners = new ArrayList<>(2);
        firebaseAnalytics = FirebaseAnalytics.getInstance(application);
        getMessagingToken(this::uploadFirebaseToken);
    }


    private void uploadFirebaseToken(String token) {

    }

    public void getMessagingToken(OnSuccessListener<String> onSuccessListener) {
        FirebaseMessaging.getInstance().getToken()
                .addOnSuccessListener(s -> {
                    if (onSuccessListener != null) {
                        onSuccessListener.onSuccess(s);
                    }
                });
    }

    void onMessagingTokenChanged(String newToken) {
        for (OnMessagingTokenChangeListener onMessagingTokenChangeListener : onMessagingTokenChangeListeners) {
            onMessagingTokenChangeListener.onNewToken(newToken);
        }
        uploadFirebaseToken(newToken);
    }

    public void reportEvent(@NonNull String eventName, @Nullable Bundle params) {
        firebaseAnalytics.logEvent(eventName, params);
    }

    public void addOnMessagingTokenChangeListener(OnMessagingTokenChangeListener listener) {
        if (listener != null) {
            onMessagingTokenChangeListeners.add(listener);
        }
    }

    public void removeOnMessagingTokenChangeListener(OnMessagingTokenChangeListener listener) {
        onMessagingTokenChangeListeners.remove(listener);
    }

    public void unsubscribeTopic(String topic) {
        if (topic == null || TextUtils.isEmpty(topic.trim())) {
            return;
        }
        FirebaseMessaging.getInstance().unsubscribeFromTopic(topic)
                .addOnSuccessListener(unused -> AtmobLog.d(TAG, "unsubscribeTopic(%s) onSuccess.", topic))
                .addOnFailureListener(e -> AtmobLog.e(TAG, "unsubscribeTopic(%s) onFailure.", e, topic));
    }

    public void subscribeTopic(String topic) {
        if (topic == null || TextUtils.isEmpty(topic.trim())) {
            return;
        }
        FirebaseMessaging.getInstance().subscribeToTopic(topic)
                .addOnSuccessListener(unused -> AtmobLog.d(TAG, "subscribeTopic(%s) onSuccess.", topic))
                .addOnFailureListener(e -> AtmobLog.e(TAG, "subscribeTopic(%s) onFailure.", e, topic));
    }

    public interface OnMessagingTokenChangeListener {
        void onNewToken(String token);
    }
}