package com.app.sounds.prank.party.list;

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.app.sounds.prank.party.R;
import com.app.sounds.prank.party.act.PlayActivity;
import com.app.sounds.prank.party.bean.Info;
import com.app.sounds.prank.party.common.Utils;
import com.app.sounds.prank.party.common.Db;
import com.app.sounds.prank.party.databinding.LikeItemBinding;
import com.bumptech.glide.Glide;

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


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

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



    public AdaLike(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(LikeItemBinding.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, Utils.childBgs[colorIndex]));
        if (info.isCustomization()) {
            Glide.with(mCOn).load(R.drawable.default_sounds).into(holder.vb.image);
        } else {
            Glide.with(mCOn).load(info.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(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, PlayActivity.class);
                intent.putExtra(Utils.KEY_PLAY_DATA, info);
                intent.putExtra(Utils.KEY_LIST_COLOR_POS, colorIndex);
                mCOn.startActivity(intent);
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        public LikeItemBinding vb;

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

    }


}