package com.fun.funnyprank.adapter;

import android.app.Activity;
import android.content.Context;
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.funnyprank.R;
import com.fun.funnyprank.activity.PrewActivity;
import com.fun.funnyprank.data.LikeData;
import com.fun.funnyprank.data.StaticValue;
import com.fun.funnyprank.databinding.PreRvItemBinding;


import java.util.List;

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

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

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

    @Override
    public void onBindViewHolder(@NonNull ListAdapter.PreVH holder, int position) {
        LikeData otherdata = otherdataList.get(position);

        if (otherdata.getPreUrl().equals("custom_url")) {
            holder.getImageViewThumb().setImageResource(R.mipmap.save_image);
        } else {
            Glide.with(mInflater.getContext())
                    .load(otherdata.getPreUrl())
                    .placeholder(R.mipmap.icon_logo)
                    .into(holder.getImageViewThumb());
        }

        holder.getTextView().setText(otherdata.getTitle());
        Drawable backgroundDrawable = ContextCompat.getDrawable(mInflater.getContext(), R.drawable.mainlist_bg);
        if (backgroundDrawable != null) {
            backgroundDrawable.setColorFilter(mcolor, PorterDuff.Mode.SRC_ATOP);
            holder.getListcons().setBackground(backgroundDrawable);
        }
        holder.getConstraintLayout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mInflater.getContext(), PrewActivity.class);
                intent.putExtra(StaticValue.KEY_TITLE, otherdata.getTitle());
                mInflater.getContext().startActivity(intent);
            }
        });
    }

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

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

        public TextView getTextView() {
            return textView;
        }

        public ConstraintLayout getListcons() {
            return listcons;
        }

        public PreVH(@NonNull PreRvItemBinding binding) {
            super(binding.getRoot());
            imageViewThumb = binding.listItemImage;
            constraintLayout = binding.listCons;
            textView = binding.listItemText;
            listcons = binding.listItemCons;

        }

        public ConstraintLayout getConstraintLayout() {
            return constraintLayout;
        }

        public ImageView getImageViewThumb() {
            return imageViewThumb;
        }

    }
}
