package com.prank.tool.adapter;

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.prank.tool.R;
import com.prank.tool.act.ActivityPlay;
import com.prank.tool.databinding.ItemLikeBinding;
import com.prank.tool.jbean.Info;
import com.prank.tool.utils.DbManager;
import com.prank.tool.utils.Tools;

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


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

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



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

    }

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


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

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



    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Info info = mValues.get(position);
        holder.vb.ttvName.setText(info.getName());
        int colorIndex = info.getColorIndex();
        holder.vb.layout.setBackground(ContextCompat.getDrawable(mCOn, Tools.childBgs[colorIndex]));
        if (info.isCustomization()) {
            Glide.with(mCOn).load(R.drawable.default_import_audio).into(holder.vb.image);
        } else {
            Glide.with(mCOn).load(info.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) {
                    DbManager.getBoxBean().remove(info);
                    int bindingAdapterPosition = holder.getBindingAdapterPosition();
                    String audioUrl = info.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, ActivityPlay.class);
                intent.putExtra(Tools.KEY_PLAY_DATA, info);
                intent.putExtra(Tools.KEY_LIST_COLOR_POS, colorIndex);
                mCOn.startActivity(intent);
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        public ItemLikeBinding vb;

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

        }

    }


}