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

import android.app.Activity;
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.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.lc.myapplication.security.mysoundstwo.R;
import com.lc.myapplication.security.mysoundstwo.activity.PreviewActivity;
import com.lc.myapplication.security.mysoundstwo.databinding.WinRecycleBinding;
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 WinAdapter extends RecyclerView.Adapter<WinAdapter.WinVH> {
    private Context mycon;
    private String[] number = new String[]{"4","5","6","7","8","9","10"};
    private List<ScaryPrankLikeEntity> dataList;
    private ScaryPrankLikeEntity list;
    private boolean islike;
    private int c = 0;
    public WinAdapter(Activity activity, List<ScaryPrankLikeEntity> data){
        mycon = activity;
        dataList = data;
    }

    @NonNull
    @Override
    public WinVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        WinRecycleBinding winRecycleBinding = WinRecycleBinding.inflate(LayoutInflater.from(mycon),parent,false);
        return new WinVH(winRecycleBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull WinVH holder, int position) {
        String numtext = number[position];
        ScaryPrankLikeEntity data = dataList.get(position);
        holder.winBinding.winReNum.setText(numtext);
        String putbg = String.valueOf(R.color.sounds_poupur_bg);
        holder.winBinding.winReNum.setTypeface(Typeface.createFromAsset(mycon.getAssets(),"myfront1.ttf"));
        holder.winBinding.winReName.setText(data.getName());
        holder.winBinding.winReName.setTypeface(Typeface.createFromAsset(mycon.getAssets(),"myfront.ttf"));
        Glide.with(mycon)
                .load(data.getImage())
                .transform(new RoundedCorners(90))
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                        //Log.e(MBrManager.TAG, "onLoadFailed-----------------categorylist.getCategoryUrl()=---"+categorylist.getCategoryUrl()+"---------e="+e.getMessage());
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(holder.winBinding.winReImage);
        islike = data.getSoundslike();
        if (islike){
            holder.winBinding.winReLike.setBackgroundResource(R.drawable.win_like);
        }else {
            holder.winBinding.winReLike.setBackgroundResource(R.drawable.win_unlike);
        }
        holder.winBinding.winReLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (islike){
                    holder.winBinding.winReLike.setBackgroundResource(R.drawable.win_unlike);
                    data.setSoundslike(false);
                    islike = false;
                }else {
                    islike = true;
                    holder.winBinding.winReLike.setBackgroundResource(R.drawable.win_like);
                    data.setSoundslike(true);
                }
                MyTool.RunIO(new Runnable() {
                    @Override
                    public void run() {
                        ScaryPrankLikeDatabase.getScaryPrankLikeDatabase().getScaryPrankLikeDao().UpdateLikeSounds(data);
                    }
                });
            }
        });
        holder.winBinding.winRecycleLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mycon, PreviewActivity.class);
                intent.putExtra(MyValue.DATA_NAME,data.getName());
                intent.putExtra(MyValue.DATA_BG,putbg);
                mycon.startActivity(intent);
            }
        });
    }

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

    public class WinVH extends RecyclerView.ViewHolder {
        private WinRecycleBinding winBinding;
        public WinVH(@NonNull WinRecycleBinding itemView) {
            super(itemView.getRoot());
            winBinding = itemView;
        }
    }
}
