package com.makeeasy.dpandstatus.adapter;

import static com.makeeasy.dpandstatus.Constant.BASE_URL;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
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.NativeAdView;
import com.makeeasy.dpandstatus.PhotoActivity;
import com.makeeasy.dpandstatus.R;
import com.makeeasy.dpandstatus.model.Photo;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Objects;

public class PhotoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context ctx;
    private final List<Object> photoList;
    private InterstitialAd mInterstitialAd;
    private static final int MENU_ITEM = 5510;
    private static final int NATIVE_AD = 5511;

    public PhotoAdapter(Context ctx, List<Object> photoList) {
        this.ctx = ctx;
        loadInterstitialAds();
        this.photoList = photoList;
    }

    @Override
    public int getItemViewType(int position) {
        Object item = photoList.get(position);
        if (item instanceof NativeAd) {
            return NATIVE_AD;
        } else {
            return MENU_ITEM;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case NATIVE_AD:
                View nativeView = LayoutInflater.from(ctx).inflate(R.layout.ad_unified,parent,false);
                return new NativeViewHolder(nativeView);
            case MENU_ITEM:
            default:
                View itemView = LayoutInflater.from(ctx).inflate(R.layout.photo, parent, false);
                return new PhotoView(itemView);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case NATIVE_AD:
                NativeAd nativeAd = (NativeAd) photoList.get(position);
                populateNativeAdView(nativeAd, ((NativeViewHolder) holder).getAdView());
                break;
            case MENU_ITEM:
            default:
                PhotoView photoView = (PhotoView) holder;
                Photo photo = (Photo) photoList.get(position);
                Picasso.get().load(BASE_URL + "upload/" + photo.getImage()).fit().into(photoView.imageView);
                photoView.itemView.setOnClickListener(view -> {
                    PhotoActivity.selected_photo = photo;
                    PhotoActivity.photoList = photoList;
                    if (mInterstitialAd != null) {
                        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                            @Override
                            public void onAdDismissedFullScreenContent() {
                                super.onAdDismissedFullScreenContent();
                                loadInterstitialAds();
                                ctx.startActivity(new Intent(ctx, PhotoActivity.class));
                            }
                            @Override
                            public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                                super.onAdFailedToShowFullScreenContent(adError);
                                ctx.startActivity(new Intent(ctx, PhotoActivity.class));
                            }
                        });
                        mInterstitialAd.show((Activity) ctx);
                    } else {
                        ctx.startActivity(new Intent(ctx, PhotoActivity.class));
                    }
                });
        }
    }

    @Override
    public int getItemCount() {
        return photoList.size();
    }

    private static class PhotoView extends RecyclerView.ViewHolder {
        private final ImageView imageView;
        public PhotoView(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.img);
        }
    }

    public void addMoreData(List<Object> photos) {
        int position = photoList.size();
        photoList.addAll(photos);notifyItemInserted(position);
    }

    private void loadInterstitialAds() {
        AdRequest adRequest = new AdRequest.Builder().build();
        InterstitialAd.load(ctx, ctx.getString(R.string.interstitial_id),adRequest,new InterstitialAdLoadCallback() {
            @Override
            public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                super.onAdLoaded(interstitialAd);
                mInterstitialAd = interstitialAd;
            }
            @Override
            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                super.onAdFailedToLoad(loadAdError);
                mInterstitialAd = null;
            }
        });
    }

    private static class NativeViewHolder extends RecyclerView.ViewHolder {
        private final NativeAdView adView;
        public NativeAdView getAdView() {
            return adView;
        }
        public NativeViewHolder(View view) {
            super(view);
            adView = view.findViewById(R.id.native_ad_view);
            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));
        }
    }

    private void populateNativeAdView(NativeAd nativeAd, NativeAdView adView) {
        ((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);
    }
}
