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

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

import androidx.annotation.Nullable;

import com.adview.sdk.api.exceptions.AdException;
import com.adview.sdk.api.rendering.RewardedAdUnit;
import com.adview.sdk.api.rendering.listeners.RewardedAdUnitListener;
import com.adview.mobile.javademo.activities.BaseAdActivity;
import com.adview.sdk.rendering.interstitial.rewarded.Reward;

public class InAppVideoRewardedActivity extends BaseAdActivity {

    private static final String CONFIG_ID = "POSIDwog9g88wqy1g"; //for video

    private RewardedAdUnit adUnit;

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

        createAd();
    }

    private void createAd() {
        if (adUnit != null) {
            adUnit.destroy();
        }

        adUnit = new RewardedAdUnit(this, CONFIG_ID);
        adUnit.setRewardedAdUnitListener(new RewardedAdUnitListener() {
            @Override
            public void onAdLoaded(RewardedAdUnit rewardedAdUnit) {
                adUnit.show();
            }

            @Override
            public void onAdDisplayed(RewardedAdUnit rewardedAdUnit) {}

            @Override
            public void onAdFailed(RewardedAdUnit rewardedAdUnit, AdException exception) {}

            @Override
            public void onAdClicked(RewardedAdUnit rewardedAdUnit) {}

            @Override
            public void onAdClosed(RewardedAdUnit rewardedAdUnit) {}

            @Override
            public void onUserEarnedReward(RewardedAdUnit rewardedAdUnit, Reward reward) {
                if (reward != null) {
                    String type = reward.getType();
                    int amount = reward.getCount();
                }

                Log.d("InAppVideoRewarded", "User earned reward: " + reward);
            }
        });
        adUnit.loadAd();
    }

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