package com.team.app.adapter;

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

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

import com.team.app.R;
import com.team.app.activities.GameDetailActivity;
import com.team.app.models.Cricket;
import com.github.ybq.android.spinkit.SpinKitView;
import com.squareup.picasso.Picasso;

import java.util.List;

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

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

    public Context context;
    private final List<Object> list;
    private final SpinKitView progress;
    private final OnTopicClickListener onTopicClickListener;
    public interface OnTopicClickListener {
        void onTopicClick();
    }

    public CricketAdapter(Context context2, SpinKitView spinKitView, List<Object> list2,OnTopicClickListener onTopicClickListener) {
        this.context = context2;
        this.progress = spinKitView;
        this.list = list2;
        this.onTopicClickListener = onTopicClickListener;
    }

    @NonNull
    @Override
    public CricketViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new CricketViewHolder(LayoutInflater.from(this.context).inflate(R.layout.match,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final Cricket cricket = (Cricket) this.list.get(position);
        CricketViewHolder cricketViewHolder = (CricketViewHolder) holder;
        String imgurl = APP_URL_MAIN + "images/";
        cricketViewHolder.team1.setText(cricket.getMatch_teamA());
        cricketViewHolder.team2.setText(cricket.getMatch_teamB());
        Picasso.get().load(imgurl+cricket.getMatch_image_1()).placeholder(R.drawable.logo).fit().into(cricketViewHolder.image1);
        Picasso.get().load(imgurl+cricket.getMatch_image_2()).placeholder(R.drawable.logo).fit().into(cricketViewHolder.image2);
        cricketViewHolder.txtSeries.setText(cricket.getMatch_name());
        cricketViewHolder.txtDate.setText(cricket.getMatch_date());
        cricketViewHolder.txtPlace.setText(cricket.getMatch_place());
        cricketViewHolder.txtStatus.setText(cricket.getMatch_status());
        if(cricket.getMatch_detail().equals("")) {
            cricketViewHolder.pre.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.not,0);
        } else {
            cricketViewHolder.pre.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.yes,0);
        }
        if(cricket.getMatch_image().equals("")) {
            cricketViewHolder.ptm.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.not,0);
        } else {
            cricketViewHolder.ptm.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.yes,0);
        }
        if(cricket.getMatch_image_p().equals("")) {
            cricketViewHolder.ftm.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.not,0);
        } else {
            cricketViewHolder.ftm.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.yes,0);
        }
        cricketViewHolder.itemView.setOnClickListener(view -> {
            GameDetailActivity.cricket = cricket;
            if(onTopicClickListener != null) {
                onTopicClickListener.onTopicClick();
            }
        });
        this.progress.setVisibility(View.GONE);
    }

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

    private static class CricketViewHolder extends RecyclerView.ViewHolder {

        private final ImageView image1;
        private final ImageView image2;
        private final TextView team1;
        private final TextView team2;
        private final TextView txtDate;
        private final TextView txtPlace;
        private final TextView txtSeries;
        private final TextView txtStatus;
        private final TextView pre;
        private final TextView ptm;
        private final TextView ftm;

        CricketViewHolder(View view) {
            super(view);
            this.team1 = view.findViewById(R.id.txtTeam1);
            this.team2 = view.findViewById(R.id.txtTeam2);
            this.txtDate = view.findViewById(R.id.txtDate);
            this.txtPlace = view.findViewById(R.id.txtPlace);
            this.txtStatus = view.findViewById(R.id.txtStatus);
            this.txtSeries = view.findViewById(R.id.txtSeries);
            this.image1 = view.findViewById(R.id.imgTeam1);
            this.image2 = view.findViewById(R.id.imgTeam2);
            this.pre = view.findViewById(R.id.pre);
            this.ptm = view.findViewById(R.id.ptm);
            this.ftm = view.findViewById(R.id.ftm);
        }
    }

}
