package com.fun.funnyprank.adapter;

import android.app.Activity;
import android.content.Context;
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.funnyprank.R;
import com.fun.funnyprank.activity.ListActivity;
import com.fun.funnyprank.data.LikeData;
import com.fun.funnyprank.data.Mydata;
import com.fun.funnyprank.data.StaticValue;
import com.fun.funnyprank.databinding.MainRvItemBinding;


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

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

    private final LayoutInflater mInflater;

    private int currentIndex = 0;

    public ThumAdapter(Activity activity, List<LikeData> 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) {
        LikeData myData = myDataList.get(position);

        if (position == getItemCount() - 1) {
            holder.getView().setVisibility(View.VISIBLE);
        } else if (position == getItemCount() - 2) {
            holder.getView().setVisibility(View.VISIBLE);
        } else {
            holder.getView().setVisibility(View.GONE);
        }

        if (myData.getCategoryUrl().equals("custom_url")) {
            holder.getImageViewThumb().setImageResource(R.mipmap.save_image);
        } else {
            Glide.with(mInflater.getContext())
                    .load(myData.getCategoryUrl())
                    .placeholder(R.mipmap.icon_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 randomColor = Randomcolor(randomcolor);
        int i = position % 9;
        Drawable backgroundDrawable1 = ContextCompat.getDrawable(mInflater.getContext(), R.drawable.mainlist_bg);
        if (backgroundDrawable1 != null) {
            backgroundDrawable1.setColorFilter(randomcolor[i], PorterDuff.Mode.SRC_ATOP);
            holder.getMainlistcons().setBackground(backgroundDrawable1);
        }
        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[i]);
            mInflater.getContext().startActivity(intent);
        });
    }

    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;

        public ConstraintLayout getMainlistcons() {
            return mainlistcons;
        }

        private TextView textView;
        private View view;

        public ThumVH(@NonNull MainRvItemBinding binding) {
            super(binding.getRoot());
            imageViewThumb = binding.mainRvImage;
            constraintLayout = binding.cons;
            mainlistcons = binding.mainListCons;
            textView = binding.mainRvText;
            view = binding.view;
        }

        public ConstraintLayout getConstraintLayout() {
            return constraintLayout;
        }

        public ImageView getImageViewThumb() {
            return imageViewThumb;
        }

        public View getView() {
            return view;
        }

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