/*
 * Copyright (c) 2015-present, Horcrux.
 * All rights reserved.
 *
 * This source code is licensed under the MIT-style license found in the
 * LICENSE file in the root directory of this source tree.
 */


package com.horcrux.svg;

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.bridge.ReadableMap;
import com.facebook.react.bridge.UiThreadUtil;

import javax.annotation.Nonnull;

class RNSVGRenderableManager extends ReactContextBaseJavaModule {
    RNSVGRenderableManager(ReactApplicationContext reactContext) {
        super(reactContext);
    }

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

    private static void isPointInFill(final int tag, final float[] src, final Callback successCallback, final int attempt) {
        UiThreadUtil.runOnUiThread(
                new Runnable() {
                    @Override
                    public void run() {
                        RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);

                        if (svg == null) {
                            if (attempt < 1) {
                                RenderableViewManager.runWhenViewIsAvailable(tag, new Runnable() {
                                    @Override
                                    public void run() {
                                        RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);
                                        if (svg == null) { // Should never happen
                                            successCallback.invoke(false);
                                            return;
                                        }
                                        isPointInFill(tag, src, successCallback, attempt + 1);
                                    }
                                });
                            } else {
                                successCallback.invoke(false);
                            }
                        } else {
                            float scale = svg.mScale;
                            src[0] *= scale;
                            src[1] *= scale;
                            int i = svg.hitTest(src);
                            successCallback.invoke(i != -1);
                        }
                    }
                }
        );
    }

    @SuppressWarnings("unused")
    @ReactMethod
    public void isPointInFill(int tag, ReadableMap options, Callback successCallback) {
        float x = (float)options.getDouble("x");
        float y = (float)options.getDouble("y");
        float[] src = new float[] { x, y };
        isPointInFill(tag, src, successCallback, 0);
    }
}

@@ -227,16 +227,54 @@
 x.svg;%0A%0A
+import android.graphics.PathMeasure;%0A%0A
 import c
@@ -2902,11 +2902,935 @@
 );%0A    %7D
+%0A%0A    @SuppressWarnings(%22unused%22)%0A    @ReactMethod%0A    public void getTotalLength(int tag, Callback successCallback) %7B%0A        RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);%0A        PathMeasure pm = new PathMeasure(svg.getPath(null, null), false);%0A        successCallback.invoke(pm.getLength());%0A    %7D%0A%0A    @SuppressWarnings(%22unused%22)%0A    @ReactMethod%0A    public void getPointAtLength(int tag, ReadableMap options, Callback successCallback) %7B%0A        float length = (float)options.getDouble(%22length%22);%0A        RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);%0A        PathMeasure pm = new PathMeasure(svg.getPath(null, null), false);%0A        float pathLength = pm.getLength();%0A        float%5B%5D pos = new float%5B2%5D;%0A        float%5B%5D tan = new float%5B2%5D;%0A        pm.getPosTan(Math.max(0, Math.min(length, pathLength)), pos, tan);%0A        successCallback.invoke(pos%5B0%5D, pos%5B1%5D);%0A    %7D
 %0A%7D%0A

implement getTotalLength and getPointAtLength