package com.mintegral.mediation.rtb;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.ads.mediation.mintegral.MintegralConstants;
import com.google.ads.mediation.mintegral.MintegralMediationAdapter;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationRewardedAd;
import com.google.android.gms.ads.mediation.MediationRewardedAdCallback;
import com.google.android.gms.ads.mediation.MediationRewardedAdConfiguration;
import com.google.android.gms.ads.rewarded.RewardItem;
import com.mbridge.msdk.MBridgeConstans;
import com.mbridge.msdk.out.MBBidRewardVideoHandler;
import com.mbridge.msdk.out.MBridgeIds;
import com.mbridge.msdk.out.RewardInfo;
import com.mbridge.msdk.out.RewardVideoListener;
import com.mintegral.mediation.MintegralUtils;

public class MintegralRtbRewardedAd implements MediationRewardedAd, RewardVideoListener {

  private static final String TAG = MintegralMediationAdapter.class.getSimpleName();
  /**
   * Data used to render an RTB interstitial ad.
   */
  private final MediationRewardedAdConfiguration adConfiguration;

  /**
   * Callback object to notify the Google Mobile Ads SDK if ad rendering succeeded or failed.
   */
  private final MediationAdLoadCallback<MediationRewardedAd, MediationRewardedAdCallback>
          callback;

  private MBBidRewardVideoHandler mbBidRewardVideoHandler;
  private MediationRewardedAdCallback rewardedAdCallback;

  public MintegralRtbRewardedAd(MediationRewardedAdConfiguration adConfiguration,
                                MediationAdLoadCallback<MediationRewardedAd, MediationRewardedAdCallback> callback) {
    this.adConfiguration = adConfiguration;
    this.callback = callback;
  }

  public void loadAd() {
    String unitId = adConfiguration.getServerParameters().getString(MintegralConstants.AD_UNIT_ID);
    String placementId = adConfiguration.getServerParameters().getString(MintegralConstants.PLACEMENT_ID);
    mbBidRewardVideoHandler = new MBBidRewardVideoHandler(adConfiguration.getContext(), placementId, unitId);
    mbBidRewardVideoHandler.setRewardVideoListener(this);
    String token = adConfiguration.getBidResponse();
    if (TextUtils.isEmpty(token)) {
      AdError error = MintegralConstants.createAdapterError(MintegralConstants.ERROR_INVALID_BID_RESPONSE, "Failed to load rewarded ad from MIntegral. Missing or invalid bid response.");
      callback.onFailure(error);
      return;
    }
    mbBidRewardVideoHandler.loadFromBid(token);
  }

  @Override
  public void showAd(@NonNull Context context) {
    boolean muted = MintegralUtils.shouldMuteAudio(adConfiguration.getMediationExtras());
    mbBidRewardVideoHandler.playVideoMute(muted ? MBridgeConstans.REWARD_VIDEO_PLAY_MUTE : MBridgeConstans.REWARD_VIDEO_PLAY_NOT_MUTE);
    mbBidRewardVideoHandler.showFromBid();
  }

  @Override
  public void onVideoLoadSuccess(MBridgeIds mBridgeIds) {
    rewardedAdCallback = callback.onSuccess(this);
  }

  @Override
  public void onLoadSuccess(MBridgeIds mBridgeIds) {
    //No-op, AdMob has no corresponding method
  }

  @Override
  public void onVideoLoadFail(MBridgeIds mBridgeIds, String s) {
    AdError error = MintegralConstants.createSdkError(MintegralConstants.ERROR_SDK_INTER_ERROR, s);
    Log.w(TAG, error.toString());
    callback.onFailure(error);
  }

  @Override
  public void onAdShow(MBridgeIds mBridgeIds) {
    if (rewardedAdCallback != null) {
      rewardedAdCallback.onAdOpened();
      rewardedAdCallback.reportAdImpression();
    }
  }

  @Override
  public void onAdClose(MBridgeIds mBridgeIds, RewardInfo rewardInfo) {
    if (rewardedAdCallback != null) {
      rewardedAdCallback.onAdClosed();
    }
    if (rewardInfo == null || !rewardInfo.isCompleteView()) {
      return;
    }
    RewardItem rewardItem = new RewardItem() {
      @NonNull
      @Override
      public String getType() {
        return rewardInfo.getRewardName();
      }

      @Override
      public int getAmount() {
        int amount = 0;
        try {
          amount = Integer.getInteger(rewardInfo.getRewardAmount());
        } catch (Exception e) {
          e.printStackTrace();
        }
        return amount;
      }
    };
    if (rewardedAdCallback != null) {
      rewardedAdCallback.onUserEarnedReward(rewardItem);
    }


  }

  @Override
  public void onShowFail(MBridgeIds mBridgeIds, String s) {
    if (rewardedAdCallback != null) {
      AdError error = MintegralConstants.createAdapterError(MintegralConstants.ERROR_SDK_INTER_ERROR, s);
      Log.w(TAG, error.toString());
      rewardedAdCallback.onAdFailedToShow(error);
    }
  }

  @Override
  public void onVideoAdClicked(MBridgeIds mBridgeIds) {
    if (rewardedAdCallback != null) {
      rewardedAdCallback.reportAdClicked();
    }
  }

  @Override
  public void onVideoComplete(MBridgeIds mBridgeIds) {
    //No-op, AdMob has no corresponding method
  }

  @Override
  public void onEndcardShow(MBridgeIds mBridgeIds) {
    //No-op, AdMob has no corresponding method
  }
}

@@ -223,77 +223,8 @@
 ts;%0A
-import com.google.ads.mediation.mintegral.MintegralMediationAdapter;%0A
 impo
@@ -865,21 +865,109 @@
 integral
-Utils
+ExtrasBuilder;%0Aimport static com.google.ads.mediation.mintegral.MintegralMediationAdapter.TAG
 ;%0A%0Apubli
@@ -1056,326 +1056,75 @@
  %7B%0A%0A
-  private static final String TAG = MintegralMediationAdapter.class.getSimpleName();%0A  /**%0A   * Data used to render an RTB interstitial ad.%0A   */%0A  private final MediationRewardedAdConfiguration adConfiguration;%0A%0A  /**%0A   * Callback object to notify the Google Mobile Ads SDK if ad rendering succeeded or failed.%0A   */
+%0A  private final MediationRewardedAdConfiguration adConfiguration;%0A
 %0A  p
@@ -1664,17 +1664,19 @@
  String 
-u
+adU
 nitId = 
@@ -1963,17 +1963,19 @@
 mentId, 
-u
+adU
 nitId);%0A
@@ -2499,39 +2499,8 @@
 d = 
-MintegralUtils.shouldMuteAudio(
 adCo
@@ -2531,16 +2531,61 @@
 Extras()
+.getBoolean(MintegralExtrasBuilder.MUTE_AUDIO
 );%0A    m
@@ -3070,33 +3070,44 @@
 idgeIds, String 
-s
+errorMessage
 ) %7B%0A    AdError 
@@ -3152,51 +3152,20 @@
 ror(
-MintegralConstants.ERROR_SDK_INTER_ERROR, s
+errorMessage
 );%0A 
@@ -4279,17 +4279,28 @@
  String 
-s
+errorMessage
 ) %7B%0A    
@@ -4435,17 +4435,28 @@
 _ERROR, 
-s
+errorMessage
 );%0A     

modify reward