package com.awesomeproject3;


import android.content.Intent;
import android.util.Log;
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.annotations.ReactProp;
import com.facebook.react.uimanager.annotations.ReactPropGroup;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.ThemedReactContext;

import java.util.Map;

public class MyViewManager extends ViewGroupManager<FrameLayout> {

    public static final String REACT_CLASS = "MyViewManager";
    public final int COMMAND_CREATE = 1;
    public final int SET_MARKER = 2;
    private int propWidth = 1500;
    private int propHeight = 1500;

    ReactApplicationContext reactContext;
    MyFragment myFragment;
    FragmentActivity activity;

    public MyViewManager(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:
               // if (myFragment == null){

                    if (myFragment != null){
                        Constant.isResume = true;
                    }else {
                        Constant.isResume = false;
                    }
                    createFragment(root, reactNativeViewId);
//                }else{
//                    // resumeFragment(root, reactNativeViewId);
//                    Log.d("CHECKMYFRAGMENT" , "ELSE");
//                    myFragment.onResume();
//
//                }
                break;

            case SET_MARKER:
                setMarker();
                break;
            default: {}
        }
    }

//    @ReactPropGroup(names = {"CustomStyle"}, defaultFloat = 0f)
//    public void setDimensions(int width, int height) {
//        //if (dimensions.size() >= 2) {
//            propWidth = width;
//            propHeight = height;
//            Log.d("TADABICHUWIDTH" , "HAHAH");
//         //   view.setDimensions(width, height);
//      //  }
//    }


    @ReactProp(name = "customWidth", defaultFloat = 0f)
    public void setWidth(FrameLayout view, float width) {
            propWidth = (int) width;
    }

    @ReactProp(name = "customHeight", defaultFloat = 0f)
    public void setHeight(FrameLayout view, float height) {
        propHeight = (int) height;
    }

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

        myFragment = new MyFragment();
       // final GoogleFragment myFragment = new GoogleFragment();
        FragmentActivity activity = (FragmentActivity) reactContext.getCurrentActivity();
        activity.getSupportFragmentManager()
                .beginTransaction()
                .replace(reactNativeViewId, myFragment, String.valueOf(reactNativeViewId))
                .commit();

    }

    public void resumeFragment(FrameLayout root, int reactNativeViewId){

       // ViewGroup parentView = root;
       setupLayout(root);

//        activity = (FragmentActivity) reactContext.getCurrentActivity();
//        activity.getSupportFragmentManager()
//                .beginTransaction()
//                .replace(reactNativeViewId, myFragment, 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);
    }



    public void setMarker() {
        myFragment.zoomCurrentLoc();
    }
}
