#!/usr/bin/env python3
"""thinkup brand: replace native SDK imports and AT*→TU* types in the Android bridge directory only (B8).

Does not modify:
  - com.anythink.reactnative package names and bridge class names
  - Const.java event strings
"""
from __future__ import annotations

import re
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
JAVA_ROOT = ROOT / "android" / "src" / "main" / "java" / "com" / "anythink" / "reactnative"

SKIP_FILES = {"Const.java"}

# Bridge class placeholders to avoid ATNative→TUNative wrongly matching ATNativeHelper etc.
BRIDGE_PLACEHOLDERS = [
    "ATReactNativeBridgeModule",
    "ATReactNativeEventEmitter",
    "ATReactNativePackage",
    "ATBannerViewManagerImpl",
    "ATNativeAdViewManagerImpl",
    "ATAutoLoadInterstitialHelper",
    "ATAutoLoadRewardVideoHelper",
    "ATBannerViewManager",
    "ATNativeAdViewManager",
    "ATInterstitialManager",
    "ATRewardVideoManager",
    "ATSplashManager",
    "ATBannerManager",
    "ATNativeManager",
    "ATBannerContainer",
    "ATNativeContainer",
    "ATBannerHelper",
    "ATNativeHelper",
    "ATInterstitialHelper",
    "ATRewardVideoHelper",
    "ATSplashHelper",
    "ATInitManager",
    "NativeSelfRenderRebuilder",
    "NativePrepareBinder",
    "AdSourcePrivacyPolicyStore",
    "AdListenerFactory",
    "BridgeJsonMapUtil",
    "MsgTools",
    "RNMapUtil",
    "Utils",
]

SDK_PKG_MAP = {
    "com.anythink.banner": "com.thinkup.banner",
    "com.anythink.core": "com.thinkup.core",
    "com.anythink.rewardvideo": "com.thinkup.rewardvideo",
    "com.anythink.splashad": "com.thinkup.splashad",
    "com.anythink.nativead": "com.thinkup.nativead",
    "com.anythink.interstitial": "com.thinkup.interstitial",
    "com.anythink.debug": "com.thinkup.debug",
}

BRIDGE_PKG_TOKEN = "__ANYTHINK_REACTNATIVE_PKG__"

# Longer names first
SDK_TYPE_MAP = [
    ("ATBannerExListener", "TUBannerExListener"),
    ("ATBannerView", "TUBannerView"),
    ("ATAdSourceStatusListener", "TUAdSourceStatusListener"),
    ("ATAdMultipleLoadedListener", "TUAdMultipleLoadedListener"),
    ("ATAdRevenueListener", "TUAdRevenueListener"),
    ("ATNetworkConfirmInfo", "TUNetworkConfirmInfo"),
    ("ATRewardVideoAutoEventListener", "TURewardVideoAutoEventListener"),
    ("ATRewardVideoAutoLoadListener", "TURewardVideoAutoLoadListener"),
    ("ATRewardVideoAutoAd", "TURewardVideoAutoAd"),
    ("ATRewardVideoExListener", "TURewardVideoExListener"),
    ("ATRewardVideoAd", "TURewardVideoAd"),
    ("ATInterstitialAutoEventListener", "TUInterstitialAutoEventListener"),
    ("ATInterstitialAutoLoadListener", "TUInterstitialAutoLoadListener"),
    ("ATInterstitialAutoAd", "TUInterstitialAutoAd"),
    ("ATInterstitialExListener", "TUInterstitialExListener"),
    ("ATSplashAdExtraInfo", "TUSplashAdExtraInfo"),
    ("ATSplashExListener", "TUSplashExListener"),
    ("ATSplashAd", "TUSplashAd"),
    ("ATNativePrepareExInfo", "TUNativePrepareExInfo"),
    ("ATNativePrepareInfo", "TUNativePrepareInfo"),
    ("ATNativeNetworkListener", "TUNativeNetworkListener"),
    ("ATNativeEventExListener", "TUNativeEventExListener"),
    ("ATNativeDislikeListener", "TUNativeDislikeListener"),
    ("ATNativeImageView", "TUNativeImageView"),
    ("ATNativeMaterial", "TUNativeMaterial"),
    ("ATNativeAdView", "TUNativeAdView"),
    ("ATRequestingInfo", "TURequestingInfo"),
    ("ATEventInterface", "TUEventInterface"),
    ("ATAdStatusInfo", "TUAdStatusInfo"),
    ("ATShowConfig", "TUShowConfig"),
    ("ATAdRequest", "TUAdRequest"),
    ("ATInterstitial", "TUInterstitial"),
    ("ATAdInfo", "TUAdInfo"),
    ("ATNative", "TUNative"),
]

METHOD_MAP = [
    ("getATTopAdInfo", "getTUTopAdInfo"),
]


def transform(content: str) -> str:
    placeholders: dict[str, str] = {}
    for i, name in enumerate(BRIDGE_PLACEHOLDERS):
        token = f"__BRIDGE_{i}__"
        placeholders[token] = name
        content = content.replace(name, token)

    content = content.replace("com.anythink.reactnative", BRIDGE_PKG_TOKEN)

    for old_pkg, new_pkg in SDK_PKG_MAP.items():
        content = content.replace(old_pkg, new_pkg)

    for old, new in SDK_TYPE_MAP:
        content = re.sub(rf"\b{re.escape(old)}\b", new, content)

    for old, new in METHOD_MAP:
        content = content.replace(old, new)

    content = content.replace(BRIDGE_PKG_TOKEN, "com.anythink.reactnative")

    for token, name in placeholders.items():
        content = content.replace(token, name)

    return content


def java_files() -> list[Path]:
    roots = [
        JAVA_ROOT,
        ROOT / "android" / "src" / "newarch" / "java" / "com" / "anythink" / "reactnative",
        ROOT / "android" / "src" / "oldarch" / "java" / "com" / "anythink" / "reactnative",
    ]
    files: list[Path] = []
    for root in roots:
        if root.exists():
            files.extend(sorted(root.rglob("*.java")))
    return files


def main() -> None:
    changed = 0
    for path in java_files():
        if path.name in SKIP_FILES:
            continue
        original = path.read_text(encoding="utf-8")
        updated = transform(original)
        if updated != original:
            path.write_text(updated, encoding="utf-8")
            print(f"updated: {path.relative_to(ROOT)}")
            changed += 1
    print(f"done, {changed} file(s) updated")


if __name__ == "__main__":
    main()
