package com.atmob.voiceai.sdk.kochava;

import android.app.Application;
import android.os.Handler;
import android.os.Looper;

import com.atmob.common.runtime.ContextUtil;
import com.atmob.common.runtime.ProcessUtil;
import com.atmob.voiceai.BuildConfig;
import com.atmob.voiceai.sdk.firebase.FirebaseHelper;
import com.kochava.tracker.Tracker;
import com.kochava.tracker.attribution.InstallAttributionApi;
import com.kochava.tracker.engagement.Engagement;
import com.kochava.tracker.log.LogLevel;

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

public class KochavaHelper {

    private static final String APP_GUID = BuildConfig.KOCHAVA;
    private static final List<AttributionResultCallback> callbacks = new ArrayList<>(5);
    private static final Handler handler = new Handler(Looper.getMainLooper());
    private static Boolean attributed;

    public static void init(Application application, FirebaseHelper firebaseHelper) {
        if (BuildConfig.DEBUG) {
            Tracker.getInstance().setLogLevel(LogLevel.TRACE);
        } else {
            Tracker.getInstance().setLogLevel(LogLevel.INFO);
        }
        Tracker.getInstance().startWithAppGuid(application, APP_GUID);

        firebaseHelper.getMessagingToken(token -> Engagement.getInstance().registerPushToken(token));
        firebaseHelper.addOnMessagingTokenChangeListener(token -> Engagement.getInstance().registerPushToken(token));

        // This callback handler will fire on every launch. Optionally check the isRetrieved if you wish to only parse these results once.
        InstallAttributionApi currentInstallAttribution = Tracker.getInstance().getInstallAttribution();
        if (!currentInstallAttribution.isRetrieved()) {
            Tracker.getInstance().retrieveInstallAttribution(KochavaHelper::onAttributionResult);
        } else {
            onAttributionResult(currentInstallAttribution);
        }
    }

    private static void onAttributionResult(InstallAttributionApi installAttributionApi) {
        synchronized (KochavaHelper.class) {
            attributed = installAttributionApi.isAttributed();
            Iterator<AttributionResultCallback> iterator = callbacks.iterator();
            while (iterator.hasNext()) {
                AttributionResultCallback callback = iterator.next();
                if (callback == null) {
                    iterator.remove();
                    continue;
                }
                handler.post(() -> callback.onResult(attributed));
                iterator.remove();
            }
        }
    }

    public static void registerAttributionResultCallback(AttributionResultCallback callback) {
        if (!ProcessUtil.isMainProcess(ContextUtil.getContext())) {
            return;
        }
        if (callback == null) {
            return;
        }
        if (attributed != null) {
            callback.onResult(attributed);
            return;
        }
        synchronized (KochavaHelper.class) {
            if (attributed != null) {
                callback.onResult(attributed);
                return;
            }
            callbacks.add(callback);
        }
    }

    public static void unregisterAttributionResultCallback(AttributionResultCallback callback) {
        if (!ProcessUtil.isMainProcess(ContextUtil.getContext())) {
            return;
        }
        if (callback == null) {
            return;
        }
        synchronized (KochavaHelper.class) {
            callbacks.remove(callback);
        }
    }

    public static boolean isAttributed() {
        return attributed != null && attributed;
    }

    @FunctionalInterface
    public interface AttributionResultCallback {
        void onResult(boolean attributed);
    }
}