using AnyThinkAds.Api;
using AnyThinkAds.Common;


/// <summary>
/// @foldcc
/// 用于绑定旧版本的listener监听事件
/// </summary>
public class ADListenerAggregator
{
    private ATRewardedVideoListener _awardVideoListener;
    private ATInterstitialAdListener _interstitialListener;

    public void BindAwardVideoListener(IATRewardedVideoAdClient client, ATRewardedVideoListener listener)
    {
        ToponUnityThread.Initialize();

        if (this._awardVideoListener == null)
        {
            client.onRewardEvent += this.onReward;
            client.onAdClickEvent += this.onRewardedVideoAdPlayClicked;
            client.onAdLoadEvent += this.onRewardedVideoAdLoaded;
            client.onAdLoadFailureEvent += this.onRewardedVideoAdLoadFail;
            client.onAdVideoStartEvent += this.onRewardedVideoAdPlayStart;
            client.onAdVideoEndEvent += this.onRewardedVideoAdPlayEnd;
            client.onAdVideoCloseEvent += this.onRewardedVideoAdPlayClosed;
            client.onAdVideoFailureEvent += this.onRewardedVideoAdPlayFail;
            client.onAdSourceAttemptEvent += this.startLoadingRewardedAdSource;
            client.onAdSourceFilledEvent += this.finishLoadingRewardedAdSource;
            client.onAdSourceLoadFailureEvent += this.failToLoadRewardedAdSource;
            client.onAdSourceBiddingAttemptEvent += this.startBiddingRewardedAdSource;
            client.onAdSourceBiddingFilledEvent += this.finishBiddingRewardedAdSource;
            client.onAdSourceBiddingFailureEvent += this.failBiddingRewardedAdSource;
        }

        this._awardVideoListener = listener;
    }

    public void BindInterstitialAdListener(IATInterstitialAdClient client, ATInterstitialAdListener listener)
    {
        ToponUnityThread.Initialize();

        if (this._interstitialListener == null)
        {
            client.onAdLoadEvent += this.onInterstitialAdLoad;
            client.onAdLoadFailureEvent += this.onInterstitialAdLoadFail;
            client.onAdClickEvent += this.onInterstitialAdClick;
            client.onAdCloseEvent += this.onInterstitialAdClose;
            client.onAdShowEvent += this.onInterstitialAdShow;
            client.onAdShowFailureEvent += this.onInterstitialAdFailedToShow;
            client.onAdVideoStartEvent += this.onInterstitialAdStartPlayingVideo;
            client.onAdVideoEndEvent += this.onInterstitialAdEndPlayingVideo;
            client.onAdVideoFailureEvent += this.onInterstitialAdFailedToPlayVideo;
            client.onAdSourceAttemptEvent += this.startLoadingInterstitialAdSource;
            client.onAdSourceFilledEvent += this.finishLoadingInterstitialAdSource;
            client.onAdSourceLoadFailureEvent += this.failToLoadInterstitialAdSource;
            client.onAdSourceBiddingAttemptEvent += this.startBiddingInterstitialAdSource;
            client.onAdSourceBiddingFilledEvent += this.finishBiddingInterstitialAdSource;
            client.onAdSourceBiddingFailureEvent += this.failBiddingInterstitialAdSource;
        }

        this._interstitialListener = listener;
    }


    void onRewardedVideoAdLoaded(object sender, ATAdEventArgs callbackInfo)
    {
        ToponUnityThread.Post(() => this._awardVideoListener?.onRewardedVideoAdLoaded(callbackInfo.placementId));
    }

    /***
     * The Ad load fail (note: for Android, all callback methods are not in the main thread of Unity)
     */
    void onRewardedVideoAdLoadFail(object sender, ATAdErrorEventArgs callbackInfo)
    {
        ToponUnityThread.Post(() => this._awardVideoListener?.onRewardedVideoAdLoadFail(callbackInfo.placementId,
            callbackInfo.errorCode, callbackInfo.errorMessage));
    }


    /***
     * The Ad play start (note: for Android, all callback methods are not in the main thread of Unity)
     */
    void onRewardedVideoAdPlayStart(object sender, ATAdEventArgs callbackInfo)
    {
        ToponUnityThread.Post(() => this._awardVideoListener?.onRewardedVideoAdPlayStart(callbackInfo.placementId,
            callbackInfo.callbackInfo));
    }

    /***
     * The Ad play end (note: for Android, all callback methods are not in the main thread of Unity)
     */
    void onRewardedVideoAdPlayEnd(object sender, ATAdEventArgs callbackInfo)
    {
        ToponUnityThread.Post(() => this._awardVideoListener?.onRewardedVideoAdPlayEnd(callbackInfo.placementId,
            callbackInfo.callbackInfo));
    }

    /***
     * The Ad play fail（note: for Android, all callback methods are not in the main thread of Unity）
     * @param code error code
     * @param message error message
     */
    void onRewardedVideoAdPlayFail(object sender, ATAdErrorEventArgs callbackInfo)
    {
        ToponUnityThread.Post(() => this._awardVideoListener?.onRewardedVideoAdPlayFail(callbackInfo.placementId,
            callbackInfo.errorCode, callbackInfo.errorMessage));
    }

    /**
		 * The Ad close（note: for Android, all callback methods are not in the main thread of Unity）
		 * @param isReward 
		 */
    void onRewardedVideoAdPlayClosed(object sender, ATAdRewardEventArgs callbackInfo)
    {
        ToponUnityThread.Post(() => this._awardVideoListener?.onRewardedVideoAdPlayClosed(callbackInfo.placementId,
            callbackInfo.isRewarded, callbackInfo.callbackInfo));
    }

    /***
     * The Ad click（note: for Android, all callback methods are not in the main thread of Unity）
     */
    void onRewardedVideoAdPlayClicked(object sender, ATAdEventArgs callbackInfo)
    {
        ToponUnityThread.Post(() => this._awardVideoListener?.onRewardedVideoAdPlayClicked(callbackInfo.placementId,
            callbackInfo.callbackInfo));
    }

    /**
         * The Ad reward（note: for Android, all callback methods are not in the main thread of Unity）
         */
    private void onReward(object sender, ATAdEventArgs callbackInfo)
    {
        ToponUnityThread.Post(() => this._awardVideoListener?.onReward(callbackInfo.placementId,
            callbackInfo.callbackInfo));
    }

    void startLoadingRewardedAdSource(object sender, ATAdEventArgs callbackInfo)
    {
        ToponUnityThread.Post(() => this._awardVideoListener?.startLoadingADSource(callbackInfo.placementId,
            callbackInfo.callbackInfo));
    }

    void finishLoadingRewardedAdSource(object sender, ATAdEventArgs callbackInfo)
    {
        ToponUnityThread.Post(() => this._awardVideoListener?.finishLoadingADSource(callbackInfo.placementId,
            callbackInfo.callbackInfo));
    }

    void failToLoadRewardedAdSource(object sender, ATAdErrorEventArgs callbackInfo)
    {
        ToponUnityThread.Post(() => this._awardVideoListener?.failToLoadADSource(callbackInfo.placementId,
            callbackInfo.callbackInfo, callbackInfo.errorCode, callbackInfo.errorMessage));
    }

    void startBiddingRewardedAdSource(object sender, ATAdEventArgs callbackInfo)
    {
        ToponUnityThread.Post(() => this._awardVideoListener?.startBiddingADSource(callbackInfo.placementId,
            callbackInfo.callbackInfo));
    }

    void finishBiddingRewardedAdSource(object sender, ATAdEventArgs callbackInfo)
    {
        ToponUnityThread.Post(() => this._awardVideoListener?.finishBiddingADSource(callbackInfo.placementId,
            callbackInfo.callbackInfo));
    }

    void failBiddingRewardedAdSource(object sender, ATAdErrorEventArgs callbackInfo)
    {
        ToponUnityThread.Post(() => this._awardVideoListener?.failBiddingADSource(callbackInfo.placementId,
            callbackInfo.callbackInfo, callbackInfo.errorCode, callbackInfo.errorMessage));
    }


    /***
* 加载广告成功（注意：对于Android来说，所有回调方法均不在Unity的主线程）
* @param placementId 广告位id
*/
    void onInterstitialAdLoad(object sender, ATAdEventArgs atAdEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.onInterstitialAdLoad(atAdEventArgs.placementId));
    }

    /***
     * 加载广告失败（注意：对于Android来说，所有回调方法均不在Unity的主线程）
     * @param placementId 广告位id
     * @param code 错误码
     * @param message 错误信息
     */
    void onInterstitialAdLoadFail(object sender, ATAdErrorEventArgs atAdErrorEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.onInterstitialAdLoadFail(atAdErrorEventArgs.placementId,
            atAdErrorEventArgs.errorCode, atAdErrorEventArgs.errorMessage));
    }

    /***
     * 广告展示（注意：对于Android来说，所有回调方法均不在Unity的主线程）
     * @param placementId 广告位id
     */
    void onInterstitialAdShow(object sender, ATAdEventArgs atAdEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.onInterstitialAdShow(atAdEventArgs.placementId,
            atAdEventArgs.callbackInfo));
    }

    /***
     * 广告展示失败（注意：对于Android来说，所有回调方法均不在Unity的主线程）
     * @param placementId 广告位id
     */
    void onInterstitialAdFailedToShow(object sender, ATAdErrorEventArgs atAdErrorEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.onInterstitialAdFailedToShow(atAdErrorEventArgs.placementId));
    }

    /***
     * 广告关闭（注意：对于Android来说，所有回调方法均不在Unity的主线程）
     * @param placementId 广告位id
     */
    void onInterstitialAdClose(object sender, ATAdEventArgs atAdEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.onInterstitialAdClose(atAdEventArgs.placementId,
            atAdEventArgs.callbackInfo));
    }

    /***
     * 广告点击（注意：对于Android来说，所有回调方法均不在Unity的主线程）
     * @param placementId 广告位id
     */
    void onInterstitialAdClick(object sender, ATAdEventArgs atAdEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.onInterstitialAdClick(atAdEventArgs.placementId,
            atAdEventArgs.callbackInfo));
    }

    void onInterstitialAdStartPlayingVideo(object sender, ATAdEventArgs atAdEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.onInterstitialAdStartPlayingVideo(
            atAdEventArgs.placementId, atAdEventArgs.callbackInfo));
    }

    void onInterstitialAdEndPlayingVideo(object sender, ATAdEventArgs atAdEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.onInterstitialAdEndPlayingVideo(
            atAdEventArgs.placementId, atAdEventArgs.callbackInfo));
    }

    void onInterstitialAdFailedToPlayVideo(object sender, ATAdErrorEventArgs atAdErrorEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.onInterstitialAdFailedToPlayVideo(
            atAdErrorEventArgs.placementId, atAdErrorEventArgs.errorCode, atAdErrorEventArgs.errorMessage));
    }

    void startLoadingInterstitialAdSource(object sender, ATAdEventArgs atAdEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.startLoadingADSource(
            atAdEventArgs.placementId, atAdEventArgs.callbackInfo));
    }

    void finishLoadingInterstitialAdSource(object sender, ATAdEventArgs atAdEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.finishLoadingADSource(
            atAdEventArgs.placementId, atAdEventArgs.callbackInfo));
    }

    void failToLoadInterstitialAdSource(object sender, ATAdErrorEventArgs atAdErrorEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.failToLoadADSource(
            atAdErrorEventArgs.placementId, atAdErrorEventArgs.callbackInfo, atAdErrorEventArgs.errorCode,
            atAdErrorEventArgs.errorMessage));
    }

    void startBiddingInterstitialAdSource(object sender, ATAdEventArgs atAdEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.startBiddingADSource(
            atAdEventArgs.placementId, atAdEventArgs.callbackInfo));
    }

    void finishBiddingInterstitialAdSource(object sender, ATAdEventArgs atAdEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.finishBiddingADSource(
            atAdEventArgs.placementId, atAdEventArgs.callbackInfo));
    }

    void failBiddingInterstitialAdSource(object sender, ATAdErrorEventArgs atAdErrorEventArgs)
    {
        ToponUnityThread.Post(() => this._interstitialListener?.failBiddingADSource(
            atAdErrorEventArgs.placementId, atAdErrorEventArgs.callbackInfo, atAdErrorEventArgs.errorCode,
            atAdErrorEventArgs.errorMessage));
    }
}
