package com.fun.originalityprank.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
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.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.fun.originalityprank.R;
import com.fun.originalityprank.activity.PrewViewActivity;
import com.fun.originalityprank.data.AllData;
import com.fun.originalityprank.databinding.ListRvItemBinding;
import com.fun.originalityprank.tool.StaticValue;

import java.util.List;

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.PreVH> {
    private List<AllData> otherdataList;
    private final LayoutInflater mInflater;
    private int mcolor;

    public ListAdapter(Activity activity, List<AllData> list,int color) {
        mInflater = LayoutInflater.from(activity);
        otherdataList = list;
        mcolor = color;
    }

    @NonNull
    @Override
    public PreVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ListRvItemBinding binding = ListRvItemBinding.inflate(mInflater, parent, false);
        return new PreVH(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull PreVH holder, int position) {
        AllData otherdata = otherdataList.get(position);
        Glide.with(mInflater.getContext())
                .load(otherdata.getPreUrl())
                .placeholder(R.mipmap.logo)
                .into(holder.getImageViewThumb());
        holder.getTextView().setText(otherdata.getTitle());
        Drawable backgroundDrawable = ContextCompat.getDrawable(mInflater.getContext(), R.drawable.listactivty_list_bg);
        if (backgroundDrawable != null) {
            backgroundDrawable.setColorFilter(mcolor, PorterDuff.Mode.SRC_ATOP);
            holder.getConstraintLayout().setBackground(backgroundDrawable);
        }
        holder.getConstraintLayout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mInflater.getContext(), PrewViewActivity.class);
                intent.putExtra(StaticValue.KEY_TITLE, otherdata.getTitle());
                intent.putExtra(StaticValue.KEY_CategoryName,otherdata.getCategoryname());
                mInflater.getContext().startActivity(intent);
            }
        });
    }

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

    public class PreVH extends RecyclerView.ViewHolder {
        private ImageView imageViewThumb;
        private ConstraintLayout constraintLayout;
        private TextView textView;

        public TextView getTextView() {
            return textView;
        }



        public PreVH(@NonNull ListRvItemBinding binding) {
            super(binding.getRoot());
            imageViewThumb = binding.listItemImage;
            constraintLayout = binding.listactivtyCons;
            textView = binding.listItemText;


        }

        public ConstraintLayout getConstraintLayout() {
            return constraintLayout;
        }

        public ImageView getImageViewThumb() {
            return imageViewThumb;
        }

    }
}
