package com.prankster.fx.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.prankster.fx.databinding.ItemHomeBinding;
import com.prankster.fx.db.Category;
import com.prankster.fx.topon.AdManager;
import com.prankster.fx.topon.onActionListener;
import com.prankster.fx.ui.activity.FXAllActivity;
import com.prankster.fx.utils.ConstValues;

import java.lang.ref.WeakReference;
import java.util.List;


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

    private final List<Category> mValues;


    private WeakReference<Activity> mCon;

    public HomeAdapter(Activity context, List<Category> items) {
        mValues = items;
        this.mCon = new WeakReference<>(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new ViewHolder(ItemHomeBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Category category = mValues.get(position);
        String covert = category.getCovert();
        Glide.with(mCon.get()).load(covert).into(holder.vb.image);
        holder.vb.name.setText(category.getName());
        holder.vb.relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity activity = mCon.get();
                if(activity!= null){
                    AdManager.showTopOn(activity, new onActionListener() {
                        @Override
                        public void onAction() {
                            Intent intent = new Intent(activity, FXAllActivity.class);
                            intent.putExtra(ConstValues.KEY_LIST_DATA, category);
                            activity.startActivity(intent);
                        }
                    });

                }

            }
        });

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemHomeBinding vb;

        public ViewHolder(ItemHomeBinding binding) {
            super(binding.getRoot());
            vb = binding;

        }

    }
}