package com.noibu.mobile.reactnative;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import android.app.Application;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;

import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.LengthPercentage;
import com.facebook.react.uimanager.LengthPercentageType;
import com.facebook.react.uimanager.Spacing;
import com.facebook.react.uimanager.drawable.BackgroundDrawable;
import com.facebook.react.uimanager.drawable.BorderDrawable;
import com.facebook.react.uimanager.drawable.CompositeBackgroundDrawable;
import com.facebook.react.uimanager.style.BorderRadiusStyle;
import com.facebook.react.uimanager.style.LogicalEdge;

import com.noibu.mobile.sessionreplay.monitors.DrawableStyle;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import java.util.Collections;

/** Against the REAL RN drawables (Kotlin-internal — hence this test, like the helper, is Java). */
@RunWith(RobolectricTestRunner.class)
@Config(sdk = 34)
public class ReactDrawableStylesTest {

    private Application app;
    private View host;

    @Before
    public void setUp() {
        app = RuntimeEnvironment.getApplication();
        DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(app);
        host = new View(app);
        host.layout(0, 0, 200, 100);
    }

    @Test
    public void reads_background_color_and_uniform_radius() {
        BorderRadiusStyle radius = new BorderRadiusStyle(Collections.emptyList());
        radius.setUniform(new LengthPercentage(16f, LengthPercentageType.POINT));
        BackgroundDrawable bg = new BackgroundDrawable(app, radius, null);
        bg.setBackgroundColor(Color.WHITE);
        bg.setBounds(0, 0, 200, 100);

        DrawableStyle style = ReactDrawableStyles.resolve(host, bg);
        assertEquals(Integer.valueOf(Color.WHITE), style.getBackgroundColor());
        // robolectric density = 1: dp == px
        assertEquals(16f, style.getCornerRadiusPx(), 0.5f);
    }

    @Test
    public void reads_border_from_composite_drawable() {
        BackgroundDrawable bg = new BackgroundDrawable(app, null, null);
        bg.setBackgroundColor(Color.WHITE);
        Spacing width = new Spacing();
        width.set(Spacing.ALL, 1f); // px (prop setter converts dp->px before storing)
        BorderDrawable border = new BorderDrawable(app, width, null, null, null);
        border.setBorderColor(LogicalEdge.ALL, Color.RED);
        CompositeBackgroundDrawable composite = new CompositeBackgroundDrawable(
            app, null, Collections.emptyList(), bg, null, border, null,
            Collections.emptyList(), null, null, null);

        DrawableStyle style = ReactDrawableStyles.resolve(host, composite);
        assertEquals(Integer.valueOf(Color.WHITE), style.getBackgroundColor());
        assertEquals(Integer.valueOf(Color.RED), style.getBorderColor());
        assertEquals(1f, style.getBorderWidthPx(), 0.5f);
    }

    @Test
    public void non_react_drawables_are_not_claimed() {
        assertNull(ReactDrawableStyles.resolve(host, new ColorDrawable(Color.RED)));
    }
}
