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.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.R;
import com.lc.myapplication.security.mysoundstwo.activity.PreviewActivity;
import com.lc.myapplication.security.mysoundstwo.databinding.FavRecycleBinding;
import com.lc.myapplication.security.mysoundstwo.databinding.RadioRecycleBinding;
import com.lc.myapplication.security.mysoundstwo.fragment.FavoriteFragment;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeEntity;
import com.lc.myapplication.security.mysoundstwo.value.MyValue;

import java.util.List;

public class FavouriteAdapter extends RecyclerView.Adapter<FavouriteAdapter.FavVh>{
    private List<ScaryPrankLikeEntity> scaryPrankLikeEntity;
    private int c = 1;
    private int[] bgswitch;
    private int i=0;
    int bg;
    private Activity myac;
    public FavouriteAdapter(Activity activity, List<ScaryPrankLikeEntity> entities){
        myac = activity;
        scaryPrankLikeEntity = entities;
    }
    @NonNull
    @Override
    public FavVh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        FavRecycleBinding favRecycleBinding = FavRecycleBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false);
        return new FavVh(favRecycleBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull FavVh holder, int position) {
        ScaryPrankLikeEntity entity = scaryPrankLikeEntity.get(position);
        holder.favRecycleBinding.favName.setText(entity.getName());
        holder.favRecycleBinding.favName.setTypeface(Typeface.createFromAsset(myac.getAssets(),"myfront.ttf"));
        if (entity.getResource() == c){
            holder.favRecycleBinding.favImage.setBackgroundResource(R.mipmap.creat_image);
            String image = String.valueOf(R.mipmap.creat_image);
            entity.setImage(image);
        }else {
           Glide.with(myac)
                    .load(entity.getImage())
                    .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.favRecycleBinding.favImage);
        }
        int swbg = gotoswitchbg(gotocolor());
        String putbg = String.valueOf(swbg);
        holder.favRecycleBinding.favImageBg.setBackgroundResource(swbg);
        holder.favRecycleBinding.favLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(myac, PreviewActivity.class);
                intent.putExtra(MyValue.DATA_NAME,entity.getName());
                intent.putExtra(MyValue.DATA_BG,putbg);
                myac.startActivity(intent);
            }
        });
    }
    private int gotoswitchbg(int[] gotoswitch) {
        if(i>=gotoswitch.length){
            i = 0;
        }
        bg = gotoswitch[i];
        i++;
        return bg;
    }
    private int[] gotocolor() {
        bgswitch = new int[6];
        bgswitch[0] = R.color.sounds_yellow_bg;
        bgswitch[1] = R.color.sounds_poupur_bg;
        bgswitch[2] = R.color.sounds_green_bg;
        bgswitch[3] = R.color.sounds_organge_bg;
        bgswitch[4] = R.color.sounds_pink_bg;
        bgswitch[5] = R.color.sounds_blue_bg;
        return bgswitch;
    }

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

    public class FavVh extends RecyclerView.ViewHolder {
        private FavRecycleBinding favRecycleBinding;
        public FavVh(@NonNull FavRecycleBinding fb) {
            super(fb.getRoot());
            favRecycleBinding = fb;
        }
    }
}
