package com.anythink.reactnative.nativead;


import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.annotations.ReactProp;

import java.util.Map;

/**
* Native ViewManager (old architecture shell): @ReactProp ×6 + getCommandsMap + handwritten receiveCommand (String/int double load)
* Distribute updateAssetView / renderNativeAd, all forward shared Impl. Equivalent item-by-item to the newarch shell.
 */
@ReactModule(name = ATNativeAdViewManager.NAME)
public class ATNativeAdViewManager extends ViewGroupManager<ATNativeContainer> {

    public static final String NAME = "ATNativeAdView";

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    protected ATNativeContainer createViewInstance(ThemedReactContext ctx) {
        return ATNativeAdViewManagerImpl.createView(ctx);
    }

    @Override
    protected void onAfterUpdateTransaction(ATNativeContainer view) {
        super.onAfterUpdateTransaction(view);
        ATNativeAdViewManagerImpl.onAfterUpdateTransaction(view);
    }

    @Override
    public void onDropViewInstance(ATNativeContainer view) {
        ATNativeAdViewManagerImpl.onDrop(view);
        super.onDropViewInstance(view);
    }

    @ReactProp(name = "placementID")
    public void setPlacementID(ATNativeContainer view, String value) {
        ATNativeAdViewManagerImpl.stateOf(view).placementId = value;
        ATNativeAdViewManagerImpl.maybeAttach(view);
    }

    @ReactProp(name = "adId")
    public void setAdId(ATNativeContainer view, String value) {
        ATNativeAdViewManagerImpl.stateOf(view).adId = value;
        view.adId = value;
        ATNativeAdViewManagerImpl.maybeAttach(view);
    }

    @ReactProp(name = "isAdaptiveHeight")
    public void setIsAdaptiveHeight(ATNativeContainer view, boolean value) {
        ATNativeAdViewManagerImpl.stateOf(view).adaptive = value;
    }

    @ReactProp(name = "adWidth")
    public void setAdWidth(ATNativeContainer view, int value) {
        ATNativeAdViewManagerImpl.stateOf(view).adWidth = value;
    }

    @ReactProp(name = "adHeight")
    public void setAdHeight(ATNativeContainer view, int value) {
        ATNativeAdViewManagerImpl.stateOf(view).adHeight = value;
    }

//Old architecture command name→id registration (JS codegenNativeCommands fallback takes UIManager.dispatchViewManagerCommand)
    @Override
    public Map<String, Integer> getCommandsMap() {
        return MapBuilder.of("updateAssetView", 0, "renderNativeAd", 1);
    }

// Different RN versions of the old architecture may issue String command names or int ids. Both overloads are implemented and distributed uniformly.
    @Override
    public void receiveCommand(ATNativeContainer root, String commandId, ReadableArray args) {
        dispatch(root, commandId, args);
    }

    @Override
    public void receiveCommand(ATNativeContainer root, int commandId, ReadableArray args) {
        dispatch(root, String.valueOf(commandId), args);
    }

    private void dispatch(ATNativeContainer root, String cmd, ReadableArray args) {
        switch (cmd) {
            case "updateAssetView":
            case "0":
                if (args != null) {
                    ATNativeAdViewManagerImpl.updateAssetView(root, args.getInt(0), args.getString(1));
                }
                break;
            case "renderNativeAd":
            case "1":
                ATNativeAdViewManagerImpl.renderNativeAd(root);
                break;
            default:
                break;
        }
    }
}
