package com.prank.sound.funnyapp.myadapter;

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.sound.funnyapp.R;
import com.prank.sound.funnyapp.activity.PlayItemActivity;
import com.prank.sound.funnyapp.bean.MyInfo;
import com.prank.sound.funnyapp.databinding.AdapterFavoriteBinding;
import com.prank.sound.funnyapp.bean.DbManager;
import com.prank.sound.funnyapp.mytool.Common;

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


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

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



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

    }

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


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

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

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


        if (holder.vb.layout.getTag() == null) {
            holder.vb.layout.setTag(true);

            holder.vb.layout.post(() -> {
                int width = holder.vb.layout.getWidth();
                ViewGroup.LayoutParams params = holder.vb.layout.getLayoutParams();
                params.height = width;
                holder.vb.layout.setLayoutParams(params);

            });
        }

        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) {
                    DbManager.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, PlayItemActivity.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 AdapterFavoriteBinding vb;

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

    }


}