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 android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
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.CategoryActivity;
import com.lc.myapplication.security.mysoundstwo.list.SoundsList;
import com.lc.myapplication.security.mysoundstwo.value.MyValue;

import java.util.List;
import java.util.Random;

public class SoundsListAdapter extends RecyclerView.Adapter<SoundsListAdapter.SoundsVH> {
    private Context mycontext;
    private List<SoundsList> mylist;
    private int[] bgswitch;
    private int[] bgswitch1;
    private int i = 0;
    int bg;

    public SoundsListAdapter(Context context, List<SoundsList> list) {
        mycontext = context;
        mylist = list;
        bgswitch1 = new int[6];
        bgswitch1[0] = R.mipmap.sounds_yellow_bg;
        bgswitch1[1] = R.mipmap.sounds_popur_bg;
        bgswitch1[2] = R.mipmap.sounds_green_bg;
        bgswitch1[3] = R.mipmap.sounds_red_bg;
        bgswitch1[4] = R.mipmap.sounds_pink_bg;
        bgswitch1[5] = R.mipmap.sounds_littlegreen_bg;
    }

    @NonNull
    @Override
    public SoundsVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mycontext).inflate(R.layout.fra_recycle_sounds, parent, false);
        return new SoundsVH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SoundsVH holder, int position) {
        SoundsList sounds = mylist.get(position);
        holder.getRe_sounds_text().setText(sounds.getCategoryName());
        holder.getRe_sounds_text().setTypeface(Typeface.createFromAsset(mycontext.getAssets(), "myfront1.ttf"));
        Log.d("--", "----" + sounds.getCategoryUrl());
        Glide.with(mycontext)
                .load(sounds.getCategoryUrl())
                .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.getRe_sounds_image());
        int a = position % 6;
        int randombg = gotorandom(gotocolor());
        //Log.d("color", "color" + randombg);
        String putbg = String.valueOf(randombg);
        holder.getRe_sounds_bg().setBackgroundResource(bgswitch1[a]);
        holder.getRe_sounds_layout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mycontext, CategoryActivity.class);
                intent.putExtra(MyValue.CATEGORY, sounds);
                intent.putExtra(MyValue.COLOR, putbg);
                mycontext.startActivity(intent);
            }
        });
    }

    private int gotorandom(int[] gotocolor) {
        int[] array = {0, 1, 2, 3, 4, 5};
        Random rand = new Random(); // 创建 Random 对象
        int randomIndex = rand.nextInt(gotocolor.length); // 生成随机索引
        int randomValue = array[randomIndex]; // 获取随机值
        return gotocolor[randomValue];
    }

    private int gotoswitchbg(int[] gotoswitch) {
        if (i >= gotoswitch.length) {
            i = 0;
        }
        bg = gotoswitch[i % 6];
        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 mylist.size();
    }

    public class SoundsVH extends RecyclerView.ViewHolder {
        private ImageView re_sounds_image;
        private TextView re_sounds_text;
        private ImageView re_sounds_bg;
        private FrameLayout re_sounds_layout;

        public SoundsVH(@NonNull View itemView) {
            super(itemView);
            re_sounds_image = itemView.findViewById(R.id.re_sounds_image);
            re_sounds_text = itemView.findViewById(R.id.re_sounds_text);
            re_sounds_bg = itemView.findViewById(R.id.re_sounds_bg);
            re_sounds_layout = itemView.findViewById(R.id.re_sounds_layout);
        }

        public ImageView getRe_sounds_image() {
            return re_sounds_image;
        }

        public TextView getRe_sounds_text() {
            return re_sounds_text;
        }

        public ImageView getRe_sounds_bg() {
            return re_sounds_bg;
        }

        public FrameLayout getRe_sounds_layout() {
            return re_sounds_layout;
        }
    }
}
