package com.light.prank.sound.app.allui.like;

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.light.prank.sound.app.allui.AcPlayPranks;
import com.light.prank.sound.app.dataListener.PrankBean;
import com.light.prank.sound.app.dataListener.objectBoxMan;
import com.light.prank.sound.app.utils.MyUtils;
import com.prank.tool.sounds.app.R;
import com.prank.tool.sounds.app.databinding.LikeAdapterBinding;

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


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

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



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

    }

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


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

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



    }

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

        if (showDelete) {
            holder.vb.view.setVisibility(View.GONE);
            if(mValues.size()%2==0){
                if(position == mValues.size()-1||position == mValues.size()-2){
                    holder.vb.view.setVisibility(View.VISIBLE);
                }
            }else {
                if (position == mValues.size() - 1) {
                    holder.vb.view.setVisibility(View.VISIBLE);
                }
            }
            holder.vb.imDelete.setVisibility(View.VISIBLE);
            holder.vb.imDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    objectBoxMan.getBoxBean().remove(PrankBean);
                    String audioUrl = PrankBean.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, AcPlayPranks.class);
                intent.putExtra(MyUtils.KEY_PLAY_DATA, PrankBean);
                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 LikeAdapterBinding vb;

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

    }


}