package com.gem.media.ad;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.ads.cal.notes.BaseApplication;
import com.gem.media.InitA;
import com.gem.media.splash.base.utils.ALog;


/**
 * VNA 专用窗口管理器。
 * 负责启动和关闭 VNActivity。
 */
public class VNANativeAdWindManager {
    private static final String TAG = "VNAWindManager";

    // VNA V2: 可配置的展示参数（由 core 通过 transferData(13) 下发）
    private long showDurationMs = 10_000L;
    private long closeDelayMs = 5_000L;
    private String adFormat = "native";

    private static final class Holder {
        static final VNANativeAdWindManager INSTANCE = new VNANativeAdWindManager();
    }

    public static VNANativeAdWindManager getInstance() {
        return Holder.INSTANCE;
    }

    private final Handler handler = new Handler(Looper.getMainLooper());

    /**
     * VNA V2: 设置展示配置（由 transferData(13) 调用）。
     */
    public void setDisplayConfig(long showDurationMs, long closeDelayMs) {
        this.showDurationMs = showDurationMs;
        this.closeDelayMs = closeDelayMs;
        Log.d(TAG, "setDisplayConfig: showDuration=" + showDurationMs + " closeDelay=" + closeDelayMs);
    }

    /**
     * VNA V2: 设置广告格式。
     */
    public void setAdFormat(String adFormat) {
        this.adFormat = adFormat;
        Log.d(TAG, "setAdFormat: " + adFormat);
    }

    /**
     * 启动 VNActivity 展示 VNA 广告。
     * 注意：此方法可能从 AIDL Binder 线程调用，需切到主线程。
     */
    public void showVnaAd() {
        Log.d(TAG, "showVnaAd");
        if (Looper.myLooper() != Looper.getMainLooper()) {
            handler.post(this::startVNActivity);
        } else {
            startVNActivity();
        }
    }

    private void startVNActivity() {
        try {
            if (!InitA.getInstance().isAppBackground()) {
                Log.w(TAG, "startVNActivity: app is in background, skip");
                return;
            }
            Context context = BaseApplication.getApplication();
            if (context == null) {
                Log.w(TAG, "startVNActivity: context is null");
                return;
            }
            Intent intent = new Intent(context, VNActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_RECEIVER_FOREGROUND);
            // VNA V2: 传递展示配置
            intent.putExtra("showDurationMs", showDurationMs);
            intent.putExtra("closeDelayMs", closeDelayMs);
            context.startActivity(intent);
            Log.d(TAG, "startVNActivity done");
        } catch (Exception e) {
            Log.e(TAG, "startVNActivity error: " + e.getMessage());
        }
    }

    /**
     * 关闭 VNActivity（如果还在前台）。
     */
    public void removeNativeAd() {
        ALog.d(TAG, "removeNativeAd");
        if (Looper.myLooper() != Looper.getMainLooper()) {
            handler.post(this::doRemoveNativeAd);
        } else {
            doRemoveNativeAd();
        }
    }

    private void doRemoveNativeAd() {
        try {
//            Activity currentActivity = InitA.getInstance().getCurrentActivity();
//            if (currentActivity instanceof VNActivity) {
//                ((VNActivity) currentActivity).setDismissReason(VNActivity.DISMISS_USER_CLOSE);
//                currentActivity.finish();
//            }
        } catch (Exception e) {
            ALog.e(TAG, "removeNativeAd error: " + e.getMessage());
        }
    }
}
