package com.terrylinla.rnsketchcanvas;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.uimanager.NativeViewHierarchyManager;
import com.facebook.react.uimanager.UIBlock;
import com.facebook.react.uimanager.UIManagerModule;

public class SketchCanvasModule extends ReactContextBaseJavaModule {
    SketchCanvasModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "SketchCanvasModule";
    }

    @ReactMethod
    public void transferToBase64(final int tag, final String type, final boolean transparent,
                                 final boolean includeImage, final boolean includeText, final boolean cropToImageSize, final Callback callback) {
        try {
            final ReactApplicationContext context = getReactApplicationContext();
            UIManagerModule uiManager = context.getNativeModule(UIManagerModule.class);
            uiManager.addUIBlock(new UIBlock() {
                public void execute(NativeViewHierarchyManager nvhm) {
                    SketchCanvas view = (SketchCanvas) nvhm.resolveView(tag);
                    String base64 = view.getBase64(type, transparent, includeImage, includeText, cropToImageSize);
                    callback.invoke(null, base64);
                }
            });
        } catch (Exception e) {
            callback.invoke(e.getMessage(), null);
        }
    }
    
    @ReactMethod
    public void moveText(final int tag, final int textId, final com.facebook.react.bridge.ReadableMap position) {
        try {
            final ReactApplicationContext context = getReactApplicationContext();
            UIManagerModule uiManager = context.getNativeModule(UIManagerModule.class);
            uiManager.addUIBlock(new UIBlock() {
                public void execute(NativeViewHierarchyManager nvhm) {
                    SketchCanvas view = (SketchCanvas) nvhm.resolveView(tag);
                    
                    // Create a map with the text ID and new position
                    com.facebook.react.bridge.WritableMap textData = com.facebook.react.bridge.Arguments.createMap();
                    textData.putInt("id", textId);
                    textData.putMap("position", position);
                    
                    // Update the text position
                    view.updateText(textData);
                }
            });
        } catch (Exception e) {
            // Handle error
        }
    }
}