package com.noibu.mobile.reactnative;

import android.graphics.drawable.Drawable;
import android.view.View;

import com.facebook.react.uimanager.PixelUtil;
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.ComputedBorderRadius;
import com.facebook.react.uimanager.style.LogicalEdge;

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

/**
 * Typed access to RN's background/border drawables. They are Kotlin-`internal` (public in
 * bytecode, so reachable from Java but not Kotlin) — isolated here so
 * {@link ReactDrawableStyleResolver} can guard class loading on hosts running RN versions that
 * predate these classes (< 0.76).
 */
final class ReactDrawableStyles {
    private ReactDrawableStyles() {}

    /** All style facts in one unwrap, or null when the drawable isn't RN's. */
    static DrawableStyle resolve(View view, Drawable drawable) {
        BackgroundDrawable bg = null;
        BorderDrawable border = null;
        if (drawable instanceof CompositeBackgroundDrawable) {
            CompositeBackgroundDrawable composite = (CompositeBackgroundDrawable) drawable;
            bg = composite.getBackground();
            border = composite.getBorder();
        } else if (drawable instanceof BackgroundDrawable) {
            bg = (BackgroundDrawable) drawable;
        }
        if (bg == null && border == null) return null;

        Integer backgroundColor = bg != null ? bg.getBackgroundColor() : null;
        Float radiusPx = bg != null ? cornerRadiusPx(view, bg) : null;

        Integer borderColor = null;
        Float borderWidthPx = null;
        if (border != null && border.getBorderWidth() != null) {
            // Spacing stores px: BackgroundStyleApplicator.setBorderWidth dpToPx()es at prop-set.
            float widthPx = border.getBorderWidth().get(Spacing.ALL);
            if (Float.isNaN(widthPx) || widthPx <= 0f) {
                widthPx = border.getBorderWidth().get(Spacing.LEFT); // per-edge: approximate as uniform
            }
            if (!Float.isNaN(widthPx) && widthPx > 0f) {
                borderWidthPx = widthPx;
                borderColor = border.getBorderColor(LogicalEdge.ALL);
            }
        }
        return new DrawableStyle(backgroundColor, radiusPx, borderColor, borderWidthPx);
    }

    private static Float cornerRadiusPx(View view, BackgroundDrawable bg) {
        BorderRadiusStyle radius = bg.getBorderRadius();
        if (radius == null || !radius.hasRoundedBorders()) return 0f;
        // Same resolution BackgroundDrawable.updatePath performs: dp-space dims in, dp radii out.
        ComputedBorderRadius computed = radius.resolve(
            view.getLayoutDirection(),
            view.getContext(),
            PixelUtil.toDIPFromPixel(view.getWidth()),
            PixelUtil.toDIPFromPixel(view.getHeight()));
        return PixelUtil.toPixelFromDIP(computed.getTopLeft().getHorizontal());
    }
}
