package __PACKAGE_NAME__;

import android.view.Choreographer;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.annotations.ReactProp;

import java.util.Map;

public class PoiMapViewManager extends ViewGroupManager<FrameLayout> {

    public static final String REACT_CLASS = "PoiMapViewManager";
    public final int COMMAND_CREATE = 1;
    
    private String applicationId;
    private String applicationSecret;
    private String uniqueId;
    private String language = "en";
    private String showOnMapStoreId;
    private String getRouteStoreId;

    ReactApplicationContext reactContext;

    public PoiMapViewManager(ReactApplicationContext reactContext) {
        this.reactContext = reactContext;
    }

    @Override
    public String getName() {
        return REACT_CLASS;
    }

    /**
     * Return a FrameLayout which will later hold the Fragment
     */
    @Override
    public FrameLayout createViewInstance(ThemedReactContext reactContext) {
        return new FrameLayout(reactContext);
    }

    /**
     * Map the "create" command to an integer
     */
    @Nullable
    @Override
    public Map<String, Integer> getCommandsMap() {
        return MapBuilder.of("create", COMMAND_CREATE);
    }

    /**
     * Handle "create" command (called from JS) and call createFragment method
     */
    @Override
    public void receiveCommand(
            @NonNull FrameLayout root,
            String commandId,
            @Nullable ReadableArray args
    ) {
        super.receiveCommand(root, commandId, args);
        int reactNativeViewId = args.getInt(0);
        int commandIdInt = Integer.parseInt(commandId);

        switch (commandIdInt) {
            case COMMAND_CREATE:
                createFragment(root, reactNativeViewId);
                break;
            default: {
            }
        }
    }

    @ReactProp(name = "applicationId")
    public void setApplicationId(FrameLayout view, String value) {
        applicationId = value;
    }

    @ReactProp(name = "applicationSecret")
    public void setApplicationSecret(FrameLayout view, String value) {
        applicationSecret = value;
    }

    @ReactProp(name = "uniqueId")
    public void setUniqueId(FrameLayout view, String value) {
        uniqueId = value;
    }

    @ReactProp(name = "language")
    public void setLanguage(FrameLayout view, String value) {
        language = value;
    }

    @ReactProp(name = "showOnMap")
    public void setShowPointOnMap(FrameLayout view, String value) {
        showOnMapStoreId = value;
    }

    @ReactProp(name = "getRouteTo")
    public void setGetRouteTo(FrameLayout view, String value) {
        getRouteStoreId = value;
    }

    /**
     * Replace your React Native view with a custom fragment
     */
    public void createFragment(FrameLayout root, int reactNativeViewId) {
        ViewGroup parentView = (ViewGroup) root.findViewById(reactNativeViewId);
        setupLayout(parentView);

        final PoiMapFragment poiMapFragment = PoiMapFragment.newInstance(
            applicationId, 
            applicationSecret, 
            uniqueId, 
            language, 
            showOnMapStoreId, 
            getRouteStoreId
        );
        
        FragmentActivity activity = (FragmentActivity) reactContext.getCurrentActivity();
        if (activity != null && !activity.isFinishing()) {
            activity.getSupportFragmentManager()
                    .beginTransaction()
                    .replace(reactNativeViewId, poiMapFragment, String.valueOf(reactNativeViewId))
                    .commit();
        }
    }

    public void setupLayout(ViewGroup view) {
        Choreographer.getInstance().postFrameCallback(new Choreographer.FrameCallback() {
            @Override
            public void doFrame(long frameTimeNanos) {
                manuallyLayoutChildren(view);
                view.getViewTreeObserver().dispatchOnGlobalLayout();
                Choreographer.getInstance().postFrameCallback(this);
            }
        });
    }

    /**
     * Layout all children properly
     */
    public void manuallyLayoutChildren(ViewGroup view) {
        for (int i = 0; i < view.getChildCount(); i++) {
            View child = view.getChildAt(i);
            child.measure(View.MeasureSpec.makeMeasureSpec(view.getMeasuredWidth(), View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(view.getMeasuredHeight(), View.MeasureSpec.EXACTLY));
            child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
        }
    }
}