package com.team.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.team.app.Constant;
import com.team.app.R;
import com.team.app.models.Cricket;
import com.github.ybq.android.spinkit.SpinKitView;
import com.google.android.gms.ads.AdRequest;
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.squareup.picasso.Picasso;

import static com.team.app.Constant.APP_URL_MAIN;

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

    private final Context context;
    private InterstitialAd mInterstitialAd;
    private final SpinKitView progress;
    private final Cricket cricket;

    public TeamImageAdapter(Context context, Cricket cricket, SpinKitView spinKitView) {
        this.context = context;
        this.cricket = cricket;
        this.progress = spinKitView;
        if(Constant.adShown && !Constant.INTERTIAL_ADS_ID.equals("")) {
            AdRequest adRequest = new AdRequest.Builder().build();
            InterstitialAd.load(context,Constant.INTERTIAL_ADS_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;
                }
            });
        }
    }

    @NonNull
    @Override
    public TeamImageViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        return new TeamImageViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.team_image_layout, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        TeamImageViewHolder teamImageViewHolder = (TeamImageViewHolder) holder;
        if(position == 0) {
            String imgurl = APP_URL_MAIN + "images/";
            String title = cricket.getMatch_teamA() +" vs "+cricket.getMatch_teamB()+"\nFree Dream Team";
            teamImageViewHolder.textView.setText(title);
            if(!cricket.getMatch_image().equals("")) {
                Picasso.get().load(imgurl + cricket.getMatch_image()).placeholder(R.drawable.logo).fit().into(teamImageViewHolder.imageView);
            }
        } else {
            String imgurl = APP_URL_MAIN + "images/";
            String title = cricket.getMatch_teamA() +" vs "+cricket.getMatch_teamB()+"\nPro Dream Team";
            teamImageViewHolder.textView.setText(title);
            if(!cricket.getMatch_image_p().equals("")) {
                Picasso.get().load(imgurl + cricket.getMatch_image_p()).placeholder(R.drawable.logo).fit().into(teamImageViewHolder.imageView);
            }
            teamImageViewHolder.imageView.setVisibility(View.GONE);
            teamImageViewHolder.iView.setVisibility(View.VISIBLE);
            teamImageViewHolder.itemView.setOnClickListener(v -> {
                if(Constant.adShown && !Constant.INTERTIAL_ADS_ID.equals("")) {
                    if(mInterstitialAd != null) {
                        mInterstitialAd.show((Activity) context);
                    }
                    teamImageViewHolder.iView.setVisibility(View.GONE);
                    teamImageViewHolder.imageView.setVisibility(View.VISIBLE);
                } else {
                    teamImageViewHolder.iView.setVisibility(View.GONE);
                    teamImageViewHolder.imageView.setVisibility(View.VISIBLE);
                }
            });
        }
        this.progress.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        return 2;
    }

    public static class TeamImageViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        ImageView imageView;
        ImageView iView;
        LinearLayout innerLinearLayout;
        LinearLayout linearLayout;
        TextView textView;

        TeamImageViewHolder(View view) {
            super(view);
            this.imageView = view.findViewById(R.id.imageView);
            this.iView = view.findViewById(R.id.iView);
            this.textView = view.findViewById(R.id.textView);
            this.linearLayout = view.findViewById(R.id.linearLayout);
            this.innerLinearLayout = view.findViewById(R.id.innerLinearLayout);
            this.cardView = view.findViewById(R.id.crd);
        }
    }
}
