package com.team.app.activities;

import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.Toast;

import androidx.annotation.NonNull;
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.Functions;
import com.team.app.R;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.AdapterStatus;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
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 Start_Activity extends AppCompatActivity {

    private Dialog dialog;
    private AdView adView,adView0;
    private NativeAdView nativeAdView;
    private InterstitialAd mInterstitial;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        MobileAds.initialize(this, initializationStatus -> {
            Map<String, AdapterStatus> statusMap = initializationStatus.getAdapterStatusMap();
            for (String adapterClass : statusMap.keySet()) {
                AdapterStatus status = statusMap.get(adapterClass);
            }
            FrameLayout adContainerView = findViewById(R.id.bannerView);
            FrameLayout adContainerView0 = findViewById(R.id.bannerAdView);
            if(Constant.adShown && !Constant.BANNER_ADS_ID.equals("")) {
                adView = new AdView(this);
                adView0 = new AdView(this);
                adView.setAdUnitId(Constant.BANNER_ADS_ID);
                adView0.setAdUnitId(Constant.BANNER_ADS_ID);
                adContainerView.addView(adView);
                adContainerView0.addView(adView0);
                adView.setAdSize(Functions.getAdSize(this));
                adView0.setAdSize(Functions.getAdSize(this));
                adView.loadAd(new AdRequest.Builder().build());
                adView0.loadAd(new AdRequest.Builder().build());
                adContainerView.setVisibility(View.VISIBLE);
                adContainerView0.setVisibility(View.VISIBLE);
            }
            createDialog();
        });
        if(Constant.adShown && !Constant.INTERTIAL_ADS_ID.equals("")) {
            AdRequest adRequest = new AdRequest.Builder().build();
            InterstitialAd.load(this, Constant.INTERTIAL_ADS_ID, adRequest, new InterstitialAdLoadCallback() {
                @Override
                public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                    super.onAdLoaded(interstitialAd);
                    mInterstitial = interstitialAd;
                    mInterstitial.setFullScreenContentCallback(new FullScreenContentCallback() {
                        @Override
                        public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                            super.onAdFailedToShowFullScreenContent(adError);
                            startActivity(new Intent(Start_Activity.this,
                                    Home_Activity.class));
                        }

                        @Override
                        public void onAdDismissedFullScreenContent() {
                            super.onAdDismissedFullScreenContent();
                            startActivity(new Intent(Start_Activity.this,
                                    Home_Activity.class));
                        }
                    });
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    super.onAdFailedToLoad(loadAdError);
                    mInterstitial = null;
                }
            });
        }
        findViewById(R.id.img_start).setOnClickListener(v -> {
            if(Constant.adShown && !Constant.INTERTIAL_ADS_ID.equals("")) {
                if (mInterstitial != null) {
                    mInterstitial.show(this);
                } else {
                    startActivity(new Intent(Start_Activity.this,
                            Home_Activity.class));
                }
            } else {
                startActivity(new Intent(Start_Activity.this,
                        Home_Activity.class));
            }
        });
        findViewById(R.id.img_share).setOnClickListener(v -> {
            try {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.app_name));
                intent.putExtra(Intent.EXTRA_TEXT, "\nLet me recommend you this application\n\n" + "https://play.google.com/store/apps/details?id=" + getPackageName() + "\n\n");
                startActivity(Intent.createChooser(intent, "choose one"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        findViewById(R.id.img_more).setOnClickListener(v -> {
            try {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constant.DEVELOPER_PAGE+getPackageName())));
            } catch (ActivityNotFoundException unused) {
                Toast.makeText(Start_Activity.this,"unable to find market app",Toast.LENGTH_SHORT).show();
            }
        });
        findViewById(R.id.img_rate).setOnClickListener(v -> {
            try {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName())));
            } catch (ActivityNotFoundException unused) {
                Toast.makeText(Start_Activity.this,"unable to find market app",Toast.LENGTH_SHORT).show();
            }
        });
        LinearLayout bb = findViewById(R.id.bb);
        Animation animation = AnimationUtils.loadAnimation(this,R.anim.bottom);
        bb.setAnimation(animation);
    }

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

    @Override
    public void onBackPressed() {
        dialog.show();
    }

    private void createDialog() {
        dialog = new Dialog(this,R.style.transparent_dialog);
        dialog.setContentView(R.layout.layout_exit_dialog);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        if(Constant.adShown && !Constant.NATVE_NORMAL_ADS_ID.equals("")) {
            nativeAdView = dialog.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());
        }
        dialog.findViewById(R.id.txt_rate).setOnClickListener(view -> {
            startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName())));
            dialog.dismiss();
        });
        dialog.findViewById(R.id.txt_later).setOnClickListener(view -> {
            startActivity(new Intent(Start_Activity.this,
                    Exit_Activity.class));
            dialog.dismiss();
            finish();
        });
    }

    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);
    }

}
