/*
 *    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.applovin;

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

import androidx.annotation.Nullable;

import com.adview.sdk.api.mediation.MediationRewardedVideoAdUnit;
import com.adview.mobile.javademo.activities.BaseAdActivity;
import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.MaxReward;
import com.applovin.mediation.MaxRewardedAdListener;
import com.applovin.mediation.adapters.adview.utils.MaxMediationRewardedUtils;
import com.applovin.mediation.ads.MaxRewardedAd;

public class AppLovinMaxVideoRewardedActivity extends BaseAdActivity {

    private static final String TAG = "AdExample";
    public static final String AD_UNIT_ID = "3131555b97f6aeb2"; //max unit
    public static final String CONFIG_ID = "POSIDwog9g88wqy1g"; //for video

    private MaxRewardedAd maxRewardedAd;
    private MediationRewardedVideoAdUnit adUnit;

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

    private void createAd() {
        binding.btnLoadAd.setVisibility(View.VISIBLE);

        maxRewardedAd = MaxRewardedAd.getInstance(AD_UNIT_ID, this);
        maxRewardedAd.setListener(new MaxRewardedAdListener() {
            @Override
            public void onAdLoaded(MaxAd ad) {
                Log.d(TAG, "=== RewardedAd Ad was loaded: " + ad.getRevenue() + " ===== ");
                binding.btnShowAd.setVisibility(View.VISIBLE);
                binding.btnShowAd.setOnClickListener(v -> {
                    maxRewardedAd.showAd();
                    binding.btnShowAd.setVisibility(View.GONE);
                });
            }

            @Override
            public void onAdDisplayed(MaxAd ad) {
                Log.d(TAG, "=== RewardedAd Ad was displayed.==");
            }

            @Override
            public void onAdHidden(MaxAd ad) {
                Log.d(TAG, "=== RewardedAd Ad was hidden.==");
                binding.btnLoadAd.setEnabled(true);
            }

            @Override
            public void onAdClicked(MaxAd ad) {
                Log.d(TAG, "=== RewardedAd Ad was clicked.==");
            }

            @Override
            public void onAdLoadFailed(String adUnitId, MaxError error) {
                Log.e(TAG, "=== Reward failed: " + error.getMessage() + " ===");
                binding.btnLoadAd.setEnabled(true);
            }

            @Override
            public void onAdDisplayFailed(MaxAd ad, MaxError error) {
                Log.e(TAG, "=== Reward display failed: " + error.getMessage() + " ===");
                binding.btnLoadAd.setEnabled(true);
            }

            @Override
            public void onUserRewarded(MaxAd ad, MaxReward reward) {
                Log.d(TAG, "=== User earned reward: " + reward.getLabel() + "  " + reward.getAmount() + " ===");
            }
        });

        MaxMediationRewardedUtils mediationUtils = new MaxMediationRewardedUtils(maxRewardedAd);
        adUnit = new MediationRewardedVideoAdUnit(
                this,
                CONFIG_ID,
                mediationUtils
        );

        binding.btnLoadAd.setOnClickListener(v -> {
            binding.btnLoadAd.setEnabled(false);
            binding.btnShowAd.setVisibility(View.GONE);
            adUnit.fetchDemand(resultCode -> {
                maxRewardedAd.loadAd();
            });
        });
    }

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