package com.anythink.reactnative.nativead;

import android.content.Context;
import android.graphics.Canvas;
import android.view.View;

import com.thinkup.nativead.api.TUNativeAdView;

import java.util.LinkedHashMap;
import java.util.Map;

/**
* Native container (shared, common to old and new architectures) = SDK embedded container TUNativeAdView subclass.
* RN asset sub-node (Text/Image) is naturally rendered within it; holds asset tag↔type registry + associated Helper + bound adId.
* Field public: for cross-class access between shared Impl (com.anythink.reactnative.nativead.ATNativeAdViewManagerImpl) and Helper.
 */
public class ATNativeContainer extends TUNativeAdView {
    /** Asset registry: react tag → type name ("title"/"appIcon"/"mediaView"...). LinkedHashMap preserves order for diagnostics. */
    public final Map<Integer, String> assetTags = new LinkedHashMap<>();
    /** Text asset JS style.color: react tag → color int. Used directly in self-render rebuild. */
    public final Map<Integer, Integer> assetColors = new LinkedHashMap<>();
    public ATNativeHelper helper;
    /** Ad instance reference bound to this view (from JS ad.adId). */
    public String adId;

    public ATNativeContainer(Context context) {
        super(context);
    }

    /** Set true after self-render rebuild: drawChild skips RN subtree, draws only native children (GDT container/rebuild layer). */
    public boolean selfRenderActive = false;

    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
// Under Self-render: The nesting level of RN AssetView is variable (grandchildren/great-grandchildren) and cannot be skipped by tag; the reconstruction layer is also renderAdContainer
// Remount into the GDT container (no longer a direct child). Therefore, distinguish by class: skip the direct child of RN rendering (com.facebook.react.*,
// That is, the AssetView tree root), keep the native child (GDT NativeAdContainer / reconstruction layer) and continue to draw → RN is invisible and the reconstruction layer is visible.
        if (selfRenderActive && child.getClass().getName().startsWith("com.facebook.react")) {
            return false;
        }
        return super.drawChild(canvas, child, drawingTime);
    }
}
