/*
 *    Copyright 2018-2019 adview.com, Inc.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.adview.mobile.javademo.activities.ads.admob;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.adview.sdk.admob.AdMobMediationRewardedUtils;
import com.adview.sdk.admob.AdviewRewardedAdapter;
import com.adview.sdk.api.mediation.MediationRewardedVideoAdUnit;
import com.adview.mobile.javademo.activities.BaseAdActivity;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;

public class AdMobVideoRewardedActivity extends BaseAdActivity {

    private static final String AD_UNIT_ID = "ca-app-pub-7283949697136772/2035607522";
    private static final String CONFIG_ID = "POSIDwog9g88wqy1g"; //for video

    private RewardedAd rewardedAd;
    private MediationRewardedVideoAdUnit adUnit;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        createAd();
    }

    private void createAd() {
        Bundle extras = new Bundle();
        AdRequest request = new AdRequest.Builder()
                .addNetworkExtrasBundle(AdviewRewardedAdapter.class, extras)
                .build();
        AdMobMediationRewardedUtils mediationUtils = new AdMobMediationRewardedUtils(extras);
        adUnit = new MediationRewardedVideoAdUnit(
                this,
                CONFIG_ID,
                mediationUtils
        );

        adUnit.fetchDemand(resultCode -> {
            Log.d("Adview", "Fetch demand result: " + resultCode);

            RewardedAd.load(this, AD_UNIT_ID, request, new RewardedAdLoadCallback() {
                @Override
                public void onAdLoaded(@NonNull RewardedAd ad) {
                    Log.d("AdMobRewarded", "=== RewardedAd Ad was loaded.===");
                    rewardedAd = ad;
                    rewardedAd.setFullScreenContentCallback(createFullScreenContentCallback());
                    rewardedAd.setOnPaidEventListener(adValue -> Log.d("AdMobRewarded", "[admob-app] Paid event: " + adValue));
                    rewardedAd.show(AdMobVideoRewardedActivity.this, rewardItem -> Log.d("AdExample", "=== User earned reward: " + rewardItem.getAmount() + " " + rewardItem.getType() + " ==="));
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError adError) {
                    Log.e("AdMobRewarded", " === Reward failed: " + adError.getMessage() + " ===");
                    rewardedAd = null;
                }
            });
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adUnit != null) {
            adUnit.destroy();
        }
    }

    protected FullScreenContentCallback createFullScreenContentCallback() {
        return new FullScreenContentCallback() {
            @Override
            public void onAdClicked() {
                Log.d("AdMobRewarded", "==== onAdClicked ===");
            }

            @Override
            public void onAdImpression() {
                Log.d("AdMobRewarded", "==== onAdImpression ===");
            }

            @Override
            public void onAdShowedFullScreenContent() {
                Log.d("AdMobRewarded", "==== onAdShowedFullScreenContent ===");
            }

            @Override
            public void onAdDismissedFullScreenContent() {
                Log.d("AdMobRewarded", "==== onAdDismissedFullScreenContent ===");
            }

            @Override
            public void onAdFailedToShowFullScreenContent(@NonNull AdError p0) {
                Log.d("AdMobRewarded", "==== onAdFailedToShowFullScreenContent ===");
            }
        };
    }
}
