package com.haircut.pranks.app.recyadapter;

import android.content.Context;
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.haircut.pranks.app.R;
import com.haircut.pranks.app.data.ChildData;
import com.haircut.pranks.app.databinding.ItemCollectionBinding;
import com.haircut.pranks.app.manager.DataManager;
import com.haircut.pranks.app.manager.Tools;
import com.haircut.pranks.app.uiview.ActPlaySounds;

import java.io.File;
import java.util.List;


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

    private List<ChildData> mValues;
    private Context mCOn;
    private boolean showDelete = false;



    public AdapterCollection(Context context, List<ChildData> items, boolean showDelete) {
        mValues = items;
        mCOn = context;
        this.showDelete = showDelete;

    }

    public void updateData(List<ChildData> items) {
        mValues = items;
        notifyDataSetChanged();
    }


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

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



    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        ChildData childData = mValues.get(position);
        holder.vb.ttvName.setText(childData.getName());
        int i = position % 5;
//        holder.vb.frameIm.setBackground(ContextCompat.getDrawable(mCOn, Utils.bgs[i]));
        if (childData.isCustomization()) {
            Glide.with(mCOn).load(R.drawable.im_import_audio).into(holder.vb.image);
        } else {
            Glide.with(mCOn).load(childData.getCovert()).into(holder.vb.image);
        }

        if (showDelete) {
            holder.vb.imDelete.setVisibility(View.VISIBLE);
            holder.vb.imDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DataManager.getBoxBean().remove(childData);
                    int bindingAdapterPosition = holder.getBindingAdapterPosition();
                    String audioUrl = childData.getAudioUrl();
                    int pos = holder.getBindingAdapterPosition();
                    mValues.remove(pos);
                    notifyItemRemoved(pos);
                    File file = new File(audioUrl);
                    if (file.exists()) {
                        file.delete();
                    }

                }
            });
        } else {
            holder.vb.imDelete.setVisibility(View.GONE);
        }


        holder.vb.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mCOn, ActPlaySounds.class);
                intent.putExtra(Tools.KEY_PLAY_DATA, childData);
                mCOn.startActivity(intent);
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        public ItemCollectionBinding vb;

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

        }

    }


}