package com.fun.airhornprank.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.Log;
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.airhornprank.R;
import com.fun.airhornprank.activity.PreViewActivity;
import com.fun.airhornprank.data.Mydata;
import com.fun.airhornprank.databinding.ListRvItemBinding;
import com.fun.airhornprank.tool.StaticValue;


import java.util.List;

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

    public ListAdapter(Activity activity, List<Mydata> 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) {
        Mydata 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.logo)
                    .into(holder.getImageViewThumb());
        }
        if (otherdata.getTitle().length() > 6) {
            String name = otherdata.getTitle().substring(0, 6);
            holder.getTextView().setText(name + "...");
        } else {
            holder.getTextView().setText(otherdata.getTitle());
        }
        Drawable backgroundDrawable = ContextCompat.getDrawable(mInflater.getContext(), R.drawable.list_rv_item_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(), PreViewActivity.class);
                intent.putExtra(StaticValue.KEY_TITLE, otherdata.getTitle());
//                intent.putExtra(StaticValue.KEY_Color,mcolor);
//                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.listRvCons;
            textView = binding.listItemText;


        }

        public ConstraintLayout getConstraintLayout() {
            return constraintLayout;
        }

        public ImageView getImageViewThumb() {
            return imageViewThumb;
        }

    }
}
