package com.fun.originalityprank.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.fun.originalityprank.R;
import com.fun.originalityprank.activity.ListActivity;
import com.fun.originalityprank.data.AllData;
import com.fun.originalityprank.databinding.MainRvItemBinding;
import com.fun.originalityprank.tool.StaticValue;

import java.util.List;

public class ThumAdapter extends RecyclerView.Adapter<ThumAdapter.ThumVH> {
    private List<AllData> myDataList;

    private final LayoutInflater mInflater;

    private int currentIndex = 0;
    private int currentgradient = 0;

    public ThumAdapter(Activity activity, List<AllData> list) {
        myDataList = list;
        mInflater = LayoutInflater.from(activity);
    }

    @NonNull
    @Override
    public ThumVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MainRvItemBinding binding = MainRvItemBinding.inflate(mInflater, parent, false);
        return new ThumVH(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ThumVH holder, int position) {
        AllData myData = myDataList.get(position);

        if (position == getItemCount() - 1) {
            holder.getView().setVisibility(View.VISIBLE);
            Log.e("zzj", "String.valueOf(position)11111111111111");
        } else if (position == getItemCount()-2){
            holder.getView().setVisibility(View.VISIBLE);
            Log.e("zzj", "String.valueOf(position)222222222222222");
        }else {
            holder.getView().setVisibility(View.GONE);
        }


        Glide.with(mInflater.getContext())
                .load(myData.getCategoryUrl())
                .placeholder(R.mipmap.logo)
                .into(holder.getImageViewThumb());
        holder.getTextView().setText(myData.getCategoryname());

        int[] randomcolor = {
                ContextCompat.getColor(mInflater.getContext(), R.color.color_1),
                ContextCompat.getColor(mInflater.getContext(), R.color.color_2),
                ContextCompat.getColor(mInflater.getContext(), R.color.color_3),
                ContextCompat.getColor(mInflater.getContext(), R.color.color_4),
                ContextCompat.getColor(mInflater.getContext(), R.color.color_5),
                ContextCompat.getColor(mInflater.getContext(), R.color.color_6),
                ContextCompat.getColor(mInflater.getContext(), R.color.color_7),
                ContextCompat.getColor(mInflater.getContext(), R.color.color_8),
                ContextCompat.getColor(mInflater.getContext(), R.color.color_9),
        };
        int[] rmcolor = {
                ContextCompat.getColor(mInflater.getContext(), R.color.gradient_1),
                ContextCompat.getColor(mInflater.getContext(), R.color.gradient_2),
                ContextCompat.getColor(mInflater.getContext(), R.color.gradient_3),
                ContextCompat.getColor(mInflater.getContext(), R.color.gradient_4),
                ContextCompat.getColor(mInflater.getContext(), R.color.gradient_5),
                ContextCompat.getColor(mInflater.getContext(), R.color.gradient_6),
                ContextCompat.getColor(mInflater.getContext(), R.color.gradient_7),
                ContextCompat.getColor(mInflater.getContext(), R.color.gradient_8),
                ContextCompat.getColor(mInflater.getContext(), R.color.gradient_9),
        };
        int gradientcolor = Randomgradientcolor(rmcolor);
        int randomColor = Randomcolor(randomcolor);
        Drawable backgroundDrawable1 = ContextCompat.getDrawable(mInflater.getContext(), R.drawable.mainlist_background);
        if (backgroundDrawable1 != null) {
            backgroundDrawable1.setColorFilter(randomColor, PorterDuff.Mode.SRC_ATOP);
            holder.getMainlistcons().setBackground(backgroundDrawable1);
        }
        Drawable backgroundDrawable = ContextCompat.getDrawable(mInflater.getContext(), R.drawable.mainlist_image_bg);
        if (backgroundDrawable != null) {
            backgroundDrawable.setColorFilter(gradientcolor, PorterDuff.Mode.SRC_ATOP);
            holder.getMainlistimage().setBackground(backgroundDrawable);
        }
        holder.getConstraintLayout().setOnClickListener(v -> {
            Intent intent = new Intent(mInflater.getContext(), ListActivity.class);
            intent.putExtra(StaticValue.KEY_CategoryName, myData.getCategoryname());
            intent.putExtra(StaticValue.KEY_Color, randomColor);
            mInflater.getContext().startActivity(intent);
        });
    }

    private int Randomgradientcolor(int[] colors) {

        if (currentgradient >= colors.length) {
            currentgradient = 0;
        }
        int nextColor = colors[currentgradient];
        currentgradient++;
        return nextColor;

    }

    private int Randomcolor(int[] colors) {

        if (currentIndex >= colors.length) {
            currentIndex = 0;
        }
        int nextColor = colors[currentIndex];
        currentIndex++;
        return nextColor;

    }

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

    public class ThumVH extends RecyclerView.ViewHolder {

        private ImageView imageViewThumb;
        private ConstraintLayout constraintLayout, mainlistcons, mainlistimage;

        public ConstraintLayout getMainlistcons() {
            return mainlistcons;
        }

        private TextView textView;
        private View view;
        public ConstraintLayout getMainlistimage() {
            return mainlistimage;
        }

        public ThumVH(@NonNull MainRvItemBinding binding) {
            super(binding.getRoot());
            imageViewThumb = binding.mainRvImage;
            constraintLayout = binding.mainCons;
            mainlistcons = binding.mainListCons;
            textView = binding.mainRvText;
            view = binding.view;
            mainlistimage = binding.mainListImageBg;
        }
        public View getView() {
            return view;
        }
        public ConstraintLayout getConstraintLayout() {
            return constraintLayout;
        }

        public ImageView getImageViewThumb() {
            return imageViewThumb;
        }


        public TextView getTextView() {
            return textView;
        }
    }
}
