package com.lc.myapplication.security.mysoundstwo.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.lc.myapplication.security.mysoundstwo.activity.PreviewActivity;
import com.lc.myapplication.security.mysoundstwo.databinding.CategoryRecycleBinding;
import com.lc.myapplication.security.mysoundstwo.list.SoundsData;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeDatabase;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeEntity;
import com.lc.myapplication.security.mysoundstwo.tool.MyTool;
import com.lc.myapplication.security.mysoundstwo.value.MyValue;

import java.util.List;

public class CategoryListAdapter extends RecyclerView.Adapter<CategoryListAdapter.CategoryVH> {
    private Context mycon;
    private List<SoundsData> mylist;
    private int bgcolor;
    private ScaryPrankLikeEntity entity;
    public CategoryListAdapter(Context context,List<SoundsData> list,int bg){
        mycon = context;
        mylist = list;
        bgcolor = bg;
    }
    @NonNull
    @Override
    public CategoryVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        CategoryRecycleBinding categoryRecycleBinding = CategoryRecycleBinding.inflate(LayoutInflater.from(mycon),parent,false);
        return new CategoryVH(categoryRecycleBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull CategoryVH holder, int position) {
        SoundsData datalist = mylist.get(position);
        holder.recycleBinding.categorySoundsText.setText(datalist.getTitle());
        holder.recycleBinding.categorySoundsText.setTypeface(Typeface.createFromAsset(mycon.getAssets(),"myfront1.ttf"));
        Glide.with(mycon)
                .load(datalist.getPreUrl())
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(holder.recycleBinding.categorySoundsImage);
        holder.recycleBinding.categoryBg.setBackgroundResource(bgcolor);
        String putbg = String.valueOf(bgcolor);
        holder.recycleBinding.categoryLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mycon, PreviewActivity.class);
                intent.putExtra(MyValue.DATA_NAME,datalist.getTitle());
                Log.d("dhncatname1","---"+datalist.getTitle());
                intent.putExtra(MyValue.DATA_BG,putbg);
                mycon.startActivity(intent);
            }
        });
    }

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

    public class CategoryVH extends RecyclerView.ViewHolder {
        private CategoryRecycleBinding recycleBinding;
        public CategoryVH(CategoryRecycleBinding categoryRecycleBinding) {
            super(categoryRecycleBinding.getRoot());
            recycleBinding = categoryRecycleBinding;
        }
    }
}
