package com.ctpodp;

import static java.util.Map.entry;

import android.view.Choreographer;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

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.ReactPropGroup;

import java.util.HashMap;
import java.util.Map;

public class MapViewManager extends ViewGroupManager<RelativeLayout> {
    public static final String REACT_CLASS = "MapViewManager";

    public final int COMMAND_CREATE = 1;
    public final int COMMAND_CENTER = 2;
    public final int COMMAND_SET_MAP_VENUE = 3;
    public final int COMMAND_CENTER_MAP_ON_POINT = 4;
    public final int COMMAND_SET_FOLLOW_BLUE_DOT = 5;
    public final int COMMAND_SET_USER_POSITION = 6;
    public final int COMMAND_SET_USER_POSITION_XY = 7;
    public final int COMMAND_CLEAR_WAYFINDING_PATH = 8;
    public final int COMMAND_WAYFIND_TO_WAYPOINT = 9;
    public final int COMMAND_WAYFIND_BETWEEN_WAYPOINTS = 10;
    public final int COMMAND_DRAW_NAVIGATION_PATH = 11;
    public final int COMMAND_STYLE_UNITS_FOR_DESTINATION = 12;
    public final int COMMAND_STYLE_SHAPE = 13;

    private int propWidth;
    private int propHeight;

    MapFragment mapFragment;

  ThemedReactContext reactContext;

    public MapViewManager(ReactApplicationContext reactContext) {
//        this.reactContext = reactContext;
    }

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

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

    /**
     * Map the "create" command to an integer
     */
    @Nullable
    @Override
    public Map<String, Integer> getCommandsMap() {
        return new HashMap<String, Integer>() {
            {
                put("create", COMMAND_CREATE);
                put("center", COMMAND_CENTER);
                put("setMapVenue", COMMAND_SET_MAP_VENUE);
                put("centerMapOnPoint", COMMAND_CENTER_MAP_ON_POINT);
                put("setFollowBlueDot", COMMAND_SET_FOLLOW_BLUE_DOT);
                put("setUserPosition", COMMAND_SET_USER_POSITION);
                put("setUserPositionXY", COMMAND_SET_USER_POSITION_XY);
                put("clearWayfindingPath", COMMAND_CLEAR_WAYFINDING_PATH);
                put("wayfindToWaypoint", COMMAND_WAYFIND_TO_WAYPOINT);
                put("wayfindBetweenWaypoints", COMMAND_WAYFIND_BETWEEN_WAYPOINTS);
                put("drawNavigationPath", COMMAND_DRAW_NAVIGATION_PATH);
                put("styleUnitsForDestination", COMMAND_STYLE_UNITS_FOR_DESTINATION);
                put("styleShape", COMMAND_STYLE_SHAPE);
            }
        };
    }

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

        switch (commandIdInt) {
            case COMMAND_CREATE:
                createFragment(root, args.getInt(0));
                break;
            case COMMAND_CENTER:
                mapFragment.centerMapOnUser(args.getDouble(0), args.getInt(1));
                break;
            case COMMAND_SET_MAP_VENUE:
                mapFragment.initializeMap(args);
                break;
//          case COMMAND_CENTER_MAP_ON_POINT:
//            mapFragment.initializeMap(args.getMap(0));
//            break;
//          case COMMAND_SET_FOLLOW_BLUE_DOT:
//            mapFragment.initializeMap(args.getMap(0));
//            break;
          case COMMAND_SET_USER_POSITION:
            mapFragment.setUserPosition(args.getDouble(0), args.getDouble(1));
            break;
//          case COMMAND_SET_USER_POSITION_XY:
//            mapFragment.initializeMap(args.getMap(0));
//            break;
//          case COMMAND_CLEAR_WAYFINDING_PATH:
//            mapFragment.initializeMap(args.getMap(0));
//            break;
//          case COMMAND_WAYFIND_TO_WAYPOINT:
//            mapFragment.initializeMap(args.getMap(0));
//            break;
//          case COMMAND_WAYFIND_BETWEEN_WAYPOINTS:
//            mapFragment.initializeMap(args.getMap(0));
//            break;
//          case COMMAND_DRAW_NAVIGATION_PATH:
//            mapFragment.initializeMap(args.getMap(0));
//            break;
//          case COMMAND_STYLE_UNITS_FOR_DESTINATION:
//            mapFragment.initializeMap(args.getMap(0));
//            break;
//          case COMMAND_STYLE_SHAPE:
//            mapFragment.initializeMap(args.getMap(0));
//            break;
            default: {
            }
        }
    }

    @ReactPropGroup(names = { "width", "height" }, customType = "Style")
    public void setStyle(RelativeLayout view, int index, Integer value) {
        if (index == 0) {
            propWidth = value;
        }

        if (index == 1) {
            propHeight = value;
        }
    }


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

        mapFragment = new MapFragment(reactContext);
        FragmentActivity activity = (FragmentActivity) reactContext.getCurrentActivity();
        activity.getSupportFragmentManager()
                .beginTransaction()
                .replace(reactNativeViewId, mapFragment, String.valueOf(reactNativeViewId))
                .commit();
    }

    public void setupLayout(View 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(View view) {
        // propWidth and propHeight coming from react-native props
        int width = propWidth;
        int height = propHeight;

        view.measure(
                View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));

        view.layout(0, 0, width, height);
    }
}
