package com.prank.app.color.list;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.prank.app.color.activity.ActivityList;
import com.prank.app.color.databinding.ItemHomeBinding;
import com.prank.app.color.dbdata.MyData;
import com.prank.app.color.utils.Common;

import java.util.List;


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

    private final List<MyData> mValues;

    private Context mCon;


    public AdapterHome(Context context, List<MyData> items) {
        mValues = items;
        this.mCon = 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) {
        int absoluteAdapterPosition = holder.getAbsoluteAdapterPosition();
        int i = absoluteAdapterPosition % 5;
        holder.vb.relative.setBackground(ContextCompat.getDrawable(mCon, Common.bgs[i]));
        MyData myData = mValues.get(absoluteAdapterPosition);
        String covert = myData.getCovert();
        Glide.with(mCon).load(covert).into(holder.vb.image);
        holder.vb.name.setText(myData.getName());
        holder.vb.relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mCon, ActivityList.class);
                intent.putExtra(Common.KEY_LIST_DATA, myData);
                intent.putExtra(Common.KEY_LIST_COLOR_POS, i);
                mCon.startActivity(intent);
            }
        });
        if(absoluteAdapterPosition == mValues.size()-1||absoluteAdapterPosition == mValues.size()-2){
            holder.vb.view.setVisibility(View.VISIBLE);
            Log.d("TAG","------1");
        }else {
            holder.vb.view.setVisibility(View.GONE);
            Log.d("TAG","------2");
        }

    }

    @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;

        }
    }
}