package com.prank.tool.sounds.app.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.sounds.app.R;
import com.prank.tool.sounds.app.activity.ViewPlaySound;
import com.prank.tool.sounds.app.common.MyUtils;
import com.prank.tool.sounds.app.data.Db;
import com.prank.tool.sounds.app.data.detail;
import com.prank.tool.sounds.app.databinding.AdaLikeBinding;

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


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

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



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

    }

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


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

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



    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        detail detail = mValues.get(position);
        holder.vb.ttvName.setText(detail.getName());
        int colorIndex = detail.getColorIndex();
        holder.vb.layout.setBackground(ContextCompat.getDrawable(mCOn, MyUtils.childBgs[colorIndex]));
        if (detail.isCustomization()) {
            Glide.with(mCOn).load(R.drawable.ic_default_sounds).into(holder.vb.image);
        } else {
            Glide.with(mCOn).load(detail.getCovert()).into(holder.vb.image);
        }

        if (showDelete) {
            if(position == mValues.size()-1||position == mValues.size()-2){
                holder.vb.view.setVisibility(View.VISIBLE);
            }else {
                holder.vb.view.setVisibility(View.GONE);
            }
            holder.vb.imDelete.setVisibility(View.VISIBLE);
            holder.vb.imDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Db.getBoxBean().remove(detail);
                    String audioUrl = detail.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, ViewPlaySound.class);
                intent.putExtra(MyUtils.KEY_PLAY_DATA, detail);
                intent.putExtra(MyUtils.KEY_LIST_COLOR_POS, colorIndex);
                mCOn.startActivity(intent);
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        public AdaLikeBinding vb;

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

    }


}