package io.hansel.react;

import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import java.util.HashMap;
import io.hansel.core.logger.HSLLogger;
import io.hansel.core.logger.LogGroup;
import io.hansel.hanselsdk.Hansel;



public class HanselRn {

    private static ReactApplicationContext reactAppContext;

    public HanselRn(ReactApplicationContext reactContext) {
        reactAppContext = reactContext;
    }

    public static void getInteractionMaps(Callback callback) {
        HashMap<String, String> journeysMap = Hansel.getInteractionMaps();
        if (callback != null) {
            callback.invoke(HSLReactConversionUtil.toWritableMap(journeysMap));
        }
    }

    public static boolean onBackButtonPressed() {
        return Hansel.onBackButtonPressed();
    }

    public static void onSetScreen(String screenName) {
        Hansel.onSetScreen(screenName);
    }

    public static void onUnsetScreen() {
        Hansel.onUnsetScreen();
    }

    public static void showToast(String text, boolean longDuration) {
        Hansel.showToast(text, longDuration);
    }

    public static void setTypeface(String fontFileName) {
        Typeface customTypeface = Typeface.createFromAsset(reactAppContext.getAssets(), "fonts/" + fontFileName);
        Hansel.setTypeface(customTypeface);
    }

    public static void setAppFont(String fontName) {
        Hansel.setAppFont(fontName);
    }
    
    public static void setNativeID() {}

    public static void setHanselIgnoreViewTag(@NonNull final View view, @NonNull final int parentsLayerCount, final int childLayerIndex) {
     view.post(new Runnable() {
         @Override
         public void run() {
             try {
                 View ignoreView = null;
                 ViewGroup parentView = (ViewGroup) view;
                 for (int i = 0; i <= parentsLayerCount; i++) {
                     if (parentView == null) {
                         break;
                     }
                     if (parentsLayerCount == i) {
                         ignoreView = parentView.getChildAt(childLayerIndex);
                     } else {
                         parentView = (ViewGroup) parentView.getChildAt(0);
                     }
                 }
                 if (ignoreView != null) {
                    
                     ignoreView.setTag(R.id.hansel_ignore_view, true);
                 }
             } catch (Throwable throwable) {
                 HSLLogger.printStackTrace(throwable, "Error setting Ignore View", LogGroup.PT);
             }
         }
     });

    }

    public static void setDynamicHanselIndex(@NonNull final View view, @NonNull final String hanselIndex, final int extraLayers) {
        view.post(new Runnable() {
            @Override
            public void run() {
                try {
                    View dynamicView = view;
                    for (int i = 0; i < extraLayers; i++) {
                        View parent = (View) dynamicView.getParent();
                        if (parent != null) {
                            dynamicView = parent;
                        } else {
                            dynamicView = null;
                            break;
                        }
                    }
                    if (dynamicView != null) {
                        Hansel.setCustomHanselIndex(dynamicView, hanselIndex);
                    }
                } catch (Throwable throwable) {
                    HSLLogger.printStackTrace(throwable, "Error setting Dynamic index", LogGroup.PT);
                }
            }
        });
    }



}
