// Importing platform-specific SDKs
import ATAndroidRewardedVideoJS from './Android/ATAndroidRewardedVideoJS';
import ATiOSRewardedVideoJS from './iOS/ATiOSRewardedVideoJS';
import ATJSSDK from './ATJSSDK';
// Platform bridge initialization
const initPlatformBridge = () => {
    if (cc.sys.os === cc.sys.OS_IOS) {
        return ATiOSRewardedVideoJS;
    } else if (cc.sys.os === cc.sys.OS_ANDROID) {
        return ATAndroidRewardedVideoJS;
    }
};

const platformBridge = initPlatformBridge();

// Interface for developer callback methods
interface DeveloperCallback {
    onRewardedVideoAdLoaded?: (placementId: string) => void;
    onRewardedVideoAdFailed?: (placementId: string, errorInfo: string) => void;
    onRewardedVideoAdPlayStart?: (placementId: string, callbackInfo: any) => void;
    onRewardedVideoAdPlayEnd?: (placementId: string, callbackInfo: any) => void;
    onRewardedVideoAdPlayFailed?: (placementId: string, errorInfo: string, callbackInfo: any) => void;
    onRewardedVideoAdClosed?: (placementId: string, callbackInfo: any) => void;
    onRewardedVideoAdPlayClicked?: (placementId: string, callbackInfo: any) => void;
    onReward?: (placementId: string, callbackInfo: any) => void;
}

// Interface for the SDK
interface ATRewardedVideoSDK {
    userIdKey: string;
    userDataKey: string;
    ATRewardedVideoListener: {
        developerCallback: DeveloperCallback | null;
        onRewardedVideoAdLoaded: (placementId: string) => void;
        onRewardedVideoAdFailed: (placementId: string, errorInfo: string) => void;
        onRewardedVideoAdPlayStart: (placementId: string, callbackInfo: any) => void;
        onRewardedVideoAdPlayEnd: (placementId: string, callbackInfo: any) => void;
        onRewardedVideoAdPlayFailed: (placementId: string, errorInfo: string, callbackInfo: any) => void;
        onRewardedVideoAdClosed: (placementId: string, callbackInfo: any) => void;
        onRewardedVideoAdPlayClicked: (placementId: string, callbackInfo: any) => void;
        onReward: (placementId: string, callbackInfo: any) => void;
    };
    loadRewardedVideo(placementId: string, settings?: object): void;
    setAdListener(listener: DeveloperCallback): void;
    hasAdReady(placementId: string): boolean;
    checkAdStatus(placementId: string): string | boolean;
    showAd(placementId: string): void;
    showAdInScenario(placementId: string, scenario?: string): void;
}

// The actual SDK implementation
const ATRewardedVideoSDK: ATRewardedVideoSDK = {
    userIdKey: "userID",
    userDataKey: "media_ext",

    ATRewardedVideoListener: {
        developerCallback: null,

        onRewardedVideoAdLoaded(placementId: string) {
            ATJSSDK.printLog(`ATRewardedVideoJSSDK.ATRewardedVideoListener.onRewardedVideoAdLoaded(${placementId})`);
            this.developerCallback?.onRewardedVideoAdLoaded?.(placementId);
        },

        onRewardedVideoAdFailed(placementId: string, errorInfo: string) {
            ATJSSDK.printLog(`ATRewardedVideoJSSDK.ATRewardedVideoListener.onRewardedVideoAdFailed(${placementId}, ${errorInfo})`);
            this.developerCallback?.onRewardedVideoAdFailed?.(placementId, errorInfo);
        },

        onRewardedVideoAdPlayStart(placementId: string, callbackInfo: any) {
            ATJSSDK.printLog(`ATRewardedVideoJSSDK.ATRewardedVideoListener.onRewardedVideoAdPlayStart(${placementId}, ${callbackInfo})`);
            this.developerCallback?.onRewardedVideoAdPlayStart?.(placementId, callbackInfo);
        },

        onRewardedVideoAdPlayEnd(placementId: string, callbackInfo: any) {
            ATJSSDK.printLog(`ATRewardedVideoJSSDK.ATRewardedVideoListener.onRewardedVideoAdPlayEnd(${placementId}, ${callbackInfo})`);
            this.developerCallback?.onRewardedVideoAdPlayEnd?.(placementId, callbackInfo);
        },

        onRewardedVideoAdPlayFailed(placementId: string, errorInfo: string, callbackInfo: any) {
            ATJSSDK.printLog(`ATRewardedVideoJSSDK.ATRewardedVideoListener.onRewardedVideoAdPlayFailed(${placementId}, ${callbackInfo})`);
            this.developerCallback?.onRewardedVideoAdPlayFailed?.(placementId, errorInfo, callbackInfo);
        },

        onRewardedVideoAdClosed(placementId: string, callbackInfo: any) {
            ATJSSDK.printLog(`ATRewardedVideoJSSDK.ATRewardedVideoListener.onRewardedVideoAdClosed(${placementId}, ${callbackInfo})`);
            this.developerCallback?.onRewardedVideoAdClosed?.(placementId, callbackInfo);
        },

        onRewardedVideoAdPlayClicked(placementId: string, callbackInfo: any) {
            ATJSSDK.printLog(`ATRewardedVideoJSSDK.ATRewardedVideoListener.onRewardedVideoAdPlayClicked(${placementId}, ${callbackInfo})`);
            this.developerCallback?.onRewardedVideoAdPlayClicked?.(placementId, callbackInfo);
        },

        onReward(placementId: string, callbackInfo: any) {
            ATJSSDK.printLog(`ATRewardedVideoJSSDK.ATRewardedVideoListener.onReward(${placementId}, ${callbackInfo})`);
            this.developerCallback?.onReward?.(placementId, callbackInfo);
        }
    },

    loadRewardedVideo(placementId: string, settings: object = {}) {
        if (platformBridge) {
            platformBridge.loadRewardedVideo(placementId, JSON.stringify(settings));
        } else {
            cc.log("You must run on Android or iOS.");
        }
    },

    setAdListener(listener: DeveloperCallback) {
        const eventJSON: { [key: string]: string } = {};
        eventJSON[LoadedCallbackKey] = "ATRewardedVideoJSSDK.ATRewardedVideoListener.onRewardedVideoAdLoaded";
        eventJSON[LoadFailCallbackKey] = "ATRewardedVideoJSSDK.ATRewardedVideoListener.onRewardedVideoAdFailed";
        eventJSON[PlayStartCallbackKey] = "ATRewardedVideoJSSDK.ATRewardedVideoListener.onRewardedVideoAdPlayStart";
        eventJSON[PlayEndCallbackKey] = "ATRewardedVideoJSSDK.ATRewardedVideoListener.onRewardedVideoAdPlayEnd";
        eventJSON[PlayFailCallbackKey] = "ATRewardedVideoJSSDK.ATRewardedVideoListener.onRewardedVideoAdPlayFailed";
        eventJSON[CloseCallbackKey] = "ATRewardedVideoJSSDK.ATRewardedVideoListener.onRewardedVideoAdClosed";
        eventJSON[ClickCallbackKey] = "ATRewardedVideoJSSDK.ATRewardedVideoListener.onRewardedVideoAdPlayClicked";
        eventJSON[RewardCallbackKey] = "ATRewardedVideoJSSDK.ATRewardedVideoListener.onReward";

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

        this.ATRewardedVideoListener.developerCallback = listener;
    },

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

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

    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.");
        }
    }
};

// Constant keys for callbacks
const LoadedCallbackKey = "RewardedVideoLoaded";
const LoadFailCallbackKey = "RewardedVideoLoadFail";
const PlayStartCallbackKey = "RewardedVideoPlayStart";
const PlayEndCallbackKey = "RewardedVideoPlayEnd";
const PlayFailCallbackKey = "RewardedVideoPlayFail";
const CloseCallbackKey = "RewardedVideoClose";
const ClickCallbackKey = "RewardedVideoClick";
const RewardCallbackKey = "RewardedVideoReward";

// Exposing the SDK globally
(window as any).ATRewardedVideoJSSDK = ATRewardedVideoSDK;
export default ATRewardedVideoSDK;
