package com.prank.funny.pranks.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.funny.pranks.R;
import com.prank.funny.pranks.bean.Info;
import com.prank.funny.pranks.databinding.ItemLayoutLikeBinding;
import com.prank.funny.pranks.pager.ActivityPlayPranks;
import com.prank.funny.pranks.tools.Common;
import com.prank.funny.pranks.tools.DataBase;

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


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

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



    public AdLike(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(ItemLayoutLikeBinding.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, Common.childBgs[colorIndex]));
        if (info.isCustomization()) {
            Glide.with(mCOn).load(R.drawable.default_prank).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) {
                    DataBase.getBoxBean().remove(info);
                    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, ActivityPlayPranks.class);
                intent.putExtra(Common.KEY_PLAY_DATA, info);
                intent.putExtra(Common.KEY_LIST_COLOR_POS, colorIndex);
                mCOn.startActivity(intent);
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        public ItemLayoutLikeBinding vb;

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

    }


}