/*
 *    Copyright 2018-2019 Adview.org, 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.Gravity;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import com.adview.sdk.AdSize;
import com.adview.sdk.AdviewMobile;
import com.adview.sdk.api.exceptions.AdException;
import com.adview.sdk.api.rendering.BannerView;
import com.adview.sdk.api.rendering.listeners.BannerViewListener;
import com.adview.mobile.javademo.activities.BaseAdActivity;

public class InAppDisplayBanner320x50Activity extends BaseAdActivity {

    private static final String CONFIG_ID = "POSID0wbfijwzm430"; //offical

    private static final int WIDTH = 320;
    private static final int HEIGHT = 50;

    private BannerView adView;
    private String configId = CONFIG_ID;

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

        createAd();
    }

    private void createAd() {
        adView = new BannerView(this, configId, new AdSize(WIDTH, HEIGHT));

        float density = getResources().getDisplayMetrics().density;
        int widthPx = (int) (WIDTH * density);
        int heightPx = (int) (HEIGHT * density);

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(widthPx, heightPx);
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;

        getAdWrapperView().addView(adView, layoutParams);

        adView.setBannerListener(new BannerViewListener() {
            @Override
            public void onAdFailed(BannerView bannerView, AdException exception) {
                Log.e("InAppDisplayBanner", "Ad failed: " + (exception != null ? exception.getMessage() : "unknown error"));
            }

            @Override
            public void onAdLoaded(BannerView bannerView) {}

            @Override
            public void onAdClicked(BannerView bannerView) {}

            @Override
            public void onAdDisplayed(BannerView bannerView) {}

            @Override
            public void onAdClosed(BannerView bannerView) {
                Log.e("InAppDisplayBanner", "Ad closed");
            }
        });

        adView.setAutoRefreshDelay(35);
        adView.loadAd();
    }

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