// Import platform-specific interstitial SDKs
import ATiOSInterstitialJS from "./iOS/ATiOSInterstitialJS";
import ATAndroidInterstitialJS from "./Android/ATAndroidInterstitialJS";

// Define platform bridge initialization
const initPlatformBridge = (): any => {  
    if (cc.sys.os === cc.sys.OS_IOS) {           
        return ATiOSInterstitialJS;
    } else if (cc.sys.os === cc.sys.OS_ANDROID) {
        return ATAndroidInterstitialJS;
    }
};

// Initialize platform bridge
const platformBridge = initPlatformBridge();

// Define listener interface for ad callbacks
interface IAdListener {
    onInterstitialAdLoaded?: (placementId: string) => void;
    onInterstitialAdLoadFail?: (placementId: string, errorInfo: any) => void;
    onInterstitialAdShow?: (placementId: string, callbackInfo: any) => void;
    onInterstitialAdStartPlayingVideo?: (placementId: string, callbackInfo: any) => void;
    onInterstitialAdEndPlayingVideo?: (placementId: string, callbackInfo: any) => void;
    onInterstitialAdFailedToPlayVideo?: (placementId: string, errorInfo: any) => void;
    onInterstitialAdFailedToShow?: (placementId: string) => void;
    onInterstitialAdClose?: (placementId: string, callbackInfo: any) => void;
    onInterstitialAdClick?: (placementId: string, callbackInfo: any) => void;
}

// Define interstitial SDK class
const ATInterstitialSDK = {
    UseRewardedVideoAsInterstitial: "UseRewardedVideoAsInterstitial",

    ATInterstitialListener: {
        developerCallback: null as IAdListener | null,

        onInterstitialAdLoaded(placementId: string) {
            this.developerCallback?.onInterstitialAdLoaded?.(placementId);
        },

        onInterstitialAdLoadFail(placementId: string, errorInfo: any) {
            this.developerCallback?.onInterstitialAdLoadFail?.(placementId, errorInfo);
        },

        onInterstitialAdShow(placementId: string, callbackInfo: any) {
            this.developerCallback?.onInterstitialAdShow?.(placementId, callbackInfo);
        },

        onInterstitialAdStartPlayingVideo(placementId: string, callbackInfo: any) {
            this.developerCallback?.onInterstitialAdStartPlayingVideo?.(placementId, callbackInfo);
        },

        onInterstitialAdEndPlayingVideo(placementId: string, callbackInfo: any) {
            this.developerCallback?.onInterstitialAdEndPlayingVideo?.(placementId, callbackInfo);
        },

        onInterstitialAdFailedToPlayVideo(placementId: string, errorInfo: any) {
            this.developerCallback?.onInterstitialAdFailedToPlayVideo?.(placementId, errorInfo);
        },

        onInterstitialAdFailedToShow(placementId: string) {
            this.developerCallback?.onInterstitialAdFailedToShow?.(placementId);
        },

        onInterstitialAdClose(placementId: string, callbackInfo: any) {
            this.developerCallback?.onInterstitialAdClose?.(placementId, callbackInfo);
        },

        onInterstitialAdClick(placementId: string, callbackInfo: any) {
            this.developerCallback?.onInterstitialAdClick?.(placementId, callbackInfo);
        }
    },
    
    loadInterstitial(placementId: string, settings: Record<string, any> = {}) {
        if (platformBridge) {
            platformBridge.loadInterstitial(placementId, JSON.stringify(settings));
        } else {
            cc.log("You must run on Android or iOS.");
        }
    },

    setAdListener(listener: IAdListener) {
        const eventJSON: Record<string, string> = {};
        eventJSON[LoadedCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdLoaded";
        eventJSON[LoadFailCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdLoadFail";
        eventJSON[PlayStartCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdStartPlayingVideo";
        eventJSON[PlayEndCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdEndPlayingVideo";
        eventJSON[PlayFailCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdFailedToPlayVideo";
        eventJSON[CloseCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdClose";
        eventJSON[ClickCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdClick";
        eventJSON[ShowCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdShow";
        eventJSON[ShowFailCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdFailedToShow";

        if (platformBridge) {
            platformBridge.setAdListener(JSON.stringify(eventJSON));
        } else {
            cc.log("You must run on Android or iOS.");
        }

        this.ATInterstitialListener.developerCallback = listener;
    },

    hasAdReady(placementId: string): boolean {
        if (platformBridge) {
            return platformBridge.hasAdReady(placementId);
        } else {
            cc.log("You must run on Android or iOS.");
        }
        return false;
    },

    checkAdStatus(placementId: string): string {
        if (platformBridge) {
            return platformBridge.checkAdStatus(placementId);
        } else {
            cc.log("You must run on Android or iOS.");
        }
        return "";
    },

    showAd(placementId: string) {
        if (platformBridge) {
            platformBridge.showAd(placementId);
        } else {
            cc.log("You must run on Android or iOS.");
        }
    },

    showAdInScenario(placementId: string, scenario: string = "") {
        if (platformBridge) {
            platformBridge.showAdInScenario(placementId, scenario);
        } else {
            cc.log("You must run on Android or iOS.");
        }
    } 
};

// Define callback keys
const LoadedCallbackKey = "InterstitialLoaded";
const LoadFailCallbackKey = "InterstitialLoadFail";
const PlayStartCallbackKey = "InterstitialPlayStart";
const PlayEndCallbackKey = "InterstitialPlayEnd";
const PlayFailCallbackKey = "InterstitialPlayFail";
const CloseCallbackKey = "InterstitialClose";
const ClickCallbackKey = "InterstitialClick";
const ShowCallbackKey = "InterstitialAdShow";
const ShowFailCallbackKey = "InterstitialAdShowFail";

// Export the SDK to the global window object
(window as any).ATInterstitialJSSDK = ATInterstitialSDK;
