package com.anythink.reactnative.nativead;

import android.view.View;
import android.view.ViewGroup;

import com.thinkup.nativead.api.TUNativePrepareExInfo;
import com.thinkup.nativead.api.TUNativePrepareInfo;
import com.anythink.reactnative.utils.Const;
import com.anythink.reactnative.utils.MsgTools;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* Self-rendering binding: JS asset {react tag → type} registered by command,
* native `container.findViewById(tag)` (view.id==react tag under Fabric) gets the RN rendered view,
* According to type setXxxView + include all clickViewList → SDK will perform click/exposure accordingly (advertising source registerViewForInteraction).
 *
* Note: RN view is laid out by RN itself. It only registers references and does not re-parent, so there is no need for relayout.
 */
public final class NativePrepareBinder {

    private NativePrepareBinder() {
    }

    /**
* @param container TUNativeAdView container (asset child node is inside it, obtained by findViewById)
* @param assetTags react tag → type name (Const.Native.* value: title/desc/cta/appIcon/mainImage/mediaView...)
* @param mediaView SDK's getAdMediaView() result (if it has been added to mediaView by addView, add clickViewList); can be empty
     */
    public static TUNativePrepareInfo build(ViewGroup container, Map<Integer, String> assetTags, View mediaView) {
        TUNativePrepareInfo info = new TUNativePrepareExInfo();
        List<View> clickViews = new ArrayList<>();
        if (container == null || assetTags == null) {
            return info;
        }

        for (Map.Entry<Integer, String> e : assetTags.entrySet()) {
            int tag = e.getKey() != null ? e.getKey() : 0;
            String type = e.getValue();
            if (tag <= 0 || type == null) {
                continue;
            }
            View v = container.findViewById(tag);
            MsgTools.printMsg("bind " + type + " tag=" + tag + " -> "
                    + (v != null ? v.getClass().getSimpleName() : "null"));
            if (v == null) {
                continue;
            }
            bindByType(info, type, v);
// The close button only goes to setCloseView and does not enter clickViewList
            if (!Const.Native.dislike.equals(type)) {
                clickViews.add(v);
            }
        }

// SDK mediaView (video/large picture surface) is also a clickable area
        if (mediaView != null) {
            clickViews.add(mediaView);
        }
// Add the container itself (the whole piece can be clicked, and the bottom exposure area is covered)
        clickViews.add(container);

        info.setClickViewList(clickViews);
        MsgTools.printMsg("clickViewList size=" + clickViews.size());
        return info;
    }

    private static void bindByType(TUNativePrepareInfo info, String type, View v) {
        if (Const.Native.title.equals(type)) {
            info.setTitleView(v);
        } else if (Const.Native.desc.equals(type)) {
            info.setDescView(v);
        } else if (Const.Native.cta.equals(type)) {
            info.setCtaView(v);
        } else if (Const.Native.icon.equals(type)) {
            info.setIconView(v);
        } else if (Const.Native.mainImage.equals(type)) {
            info.setMainImageView(v);
        } else if (Const.Native.adLogo.equals(type)) {
            info.setAdFromView(v);
        } else if (Const.Native.dislike.equals(type)) {
            info.setCloseView(v);
        }
// mediaView type is not setXxxView (SDK view provided by getAdMediaView and addView), only clickView
    }
}
