package com.team.app.activities;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.RatingBar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import com.team.app.Constant;
import com.team.app.R;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.AdapterStatus;
import com.google.android.gms.ads.nativead.NativeAd;
import com.google.android.gms.ads.nativead.NativeAdOptions;
import com.google.android.gms.ads.nativead.NativeAdView;

import java.util.Map;
import java.util.Objects;

public class Exit_Activity extends AppCompatActivity {

    private NativeAdView nativeAdView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exit);
        MobileAds.initialize(this, initializationStatus -> {
            Map<String, AdapterStatus> statusMap = initializationStatus.getAdapterStatusMap();
            for (String adapterClass : statusMap.keySet()) {
                AdapterStatus status = statusMap.get(adapterClass);
            }
            if(Constant.adShown && !Constant.NATVE_NORMAL_ADS_ID.equals("")) {
                nativeAdView = findViewById(R.id.native_ad_view);
                AdLoader adLoader = new AdLoader.Builder(this,Constant.NATVE_NORMAL_ADS_ID)
                        .forNativeAd(nativeAd -> populateNativeAdView(nativeAd,nativeAdView))
                        .withNativeAdOptions(new NativeAdOptions.Builder().build()).build();
                adLoader.loadAd(new AdRequest.Builder().build());
            }
        });
        findViewById(R.id.iv_Heart).startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.pulse));
        findViewById(R.id.txt_exit).setOnClickListener(view -> Exit_Activity.this.finishAffinity());
    }

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

    private void populateNativeAdView(NativeAd nativeAd, NativeAdView adView) {
        adView.setMediaView(adView.findViewById(R.id.media_view));
        adView.setCallToActionView(adView.findViewById(R.id.cta));
        adView.setHeadlineView(adView.findViewById(R.id.primary));
        adView.setAdvertiserView(adView.findViewById(R.id.secondary));
        adView.setStarRatingView(adView.findViewById(R.id.rating_bar));
        adView.setBodyView(adView.findViewById(R.id.body));
        adView.setIconView(adView.findViewById(R.id.icon));
        ((AppCompatTextView) Objects.requireNonNull(adView.getHeadlineView())).setText(nativeAd.getHeadline());
        ((AppCompatTextView) Objects.requireNonNull(adView.getBodyView())).setText(nativeAd.getBody());
        ((AppCompatButton) Objects.requireNonNull(adView.getCallToActionView())).setText(nativeAd.getCallToAction());
        Objects.requireNonNull(adView.getMediaView()).setMediaContent(Objects.requireNonNull(nativeAd.getMediaContent()));
        if (nativeAd.getIcon() == null) {
            Objects.requireNonNull(adView.getIconView()).setVisibility(View.INVISIBLE);
        } else {
            ((AppCompatImageView) Objects.requireNonNull(adView.getIconView())).setImageDrawable(nativeAd.getIcon().getDrawable());
            adView.getIconView().setVisibility(View.VISIBLE);
        }
        if (nativeAd.getStarRating() == null) {
            Objects.requireNonNull(adView.getStarRatingView()).setVisibility(View.INVISIBLE);
        } else {
            ((RatingBar) Objects.requireNonNull(adView.getStarRatingView())).setRating(nativeAd.getStarRating().floatValue());
            adView.getStarRatingView().setVisibility(View.VISIBLE);
        }
        if (nativeAd.getAdvertiser() == null) {
            Objects.requireNonNull(adView.getAdvertiserView()).setVisibility(View.INVISIBLE);
        } else {
            ((AppCompatTextView) Objects.requireNonNull(adView.getAdvertiserView())).setText(nativeAd.getAdvertiser());
            adView.getAdvertiserView().setVisibility(View.VISIBLE);
        }
        adView.setNativeAd(nativeAd);
    }

}
