package com.light.prank.sound.app.allui.main;

import android.content.Context;
import android.content.Intent;
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.light.prank.sound.app.allui.AcCategoryList;
import com.light.prank.sound.app.dataListener.SoundsWrapper;
import com.light.prank.sound.app.utils.MyUtils;
import com.prank.tool.sounds.app.databinding.MainAdapterBinding;

import java.util.List;


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

    private final List<SoundsWrapper> mValues;

    private Context mCon;


    public AdapterMain(Context context, List<SoundsWrapper> items) {
        mValues = items;
        this.mCon = context;
    }

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

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

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        int absoluteAdapterPosition = holder.getAbsoluteAdapterPosition();
        int i = absoluteAdapterPosition % 6;
        holder.vb.relative.setBackground(ContextCompat.getDrawable(mCon, MyUtils.bgs[i]));
        SoundsWrapper soundsWrapper = mValues.get(absoluteAdapterPosition);
        String covert = soundsWrapper.getCovert();
        Glide.with(mCon).load(covert).into(holder.vb.image);
        holder.vb.name.setText(soundsWrapper.getName());
        holder.vb.relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mCon, AcCategoryList.class);
                intent.putExtra(MyUtils.KEY_LIST_DATA, soundsWrapper);
                intent.putExtra(MyUtils.KEY_LIST_COLOR_POS, i);
                mCon.startActivity(intent);
            }
        });
        if(absoluteAdapterPosition == mValues.size()-1||absoluteAdapterPosition == mValues.size()-2){
            holder.vb.view.setVisibility(View.VISIBLE);
        }else {
            holder.vb.view.setVisibility(View.GONE);
        }

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        private MainAdapterBinding vb;

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

        }
    }
}