package com.foxx.androidcast.commercial;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;

import com.foxx.androidcast.R;

/**
 * In-app ads placeholder. No AdMob SDK wired yet — when {@link CommercialFeatures#isAdsEnabled}
 * is true, logs and reserves UI hooks for a future banner/interstitial integration.
 */
public final class InAppAdCoordinator {
    private static final String TAG = "InAppAdCoordinator";
    private static volatile InAppAdCoordinator instance;

    private final Context appContext;
    private boolean initialized;

    public static InAppAdCoordinator getInstance(Context context) {
        if (instance == null) {
            synchronized (InAppAdCoordinator.class) {
                if (instance == null) {
                    instance = new InAppAdCoordinator(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    private InAppAdCoordinator(Context appContext) {
        this.appContext = appContext;
    }

    public void ensureInitialized() {
        if (!CommercialFeatures.isAdsEnabled(appContext)) {
            return;
        }
        if (initialized) {
            return;
        }
        initialized = true;
        Log.i(TAG, "Ads enabled (developer) — integrate AdMob App ID in manifest when ready.");
    }

    public void shutdown() {
        initialized = false;
    }

    /** Attach a placeholder banner container (no network ads until SDK + unit IDs are added). */
    public void attachBannerPlaceholder(Activity activity, ViewGroup container) {
        if (!CommercialFeatures.isAdsEnabled(appContext) || container == null) {
            if (container != null) {
                container.setVisibility(ViewGroup.GONE);
            }
            return;
        }
        ensureInitialized();
        container.setVisibility(ViewGroup.VISIBLE);
        if (container.getChildCount() == 0) {
            android.widget.TextView hint = new android.widget.TextView(activity);
            hint.setText(R.string.commercial_ads_placeholder);
            hint.setPadding(24, 16, 24, 16);
            container.addView(hint);
        }
    }
}
