package com.fun.originalityprank.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
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.PrewViewActivity;
import com.fun.originalityprank.data.AllData;
import com.fun.originalityprank.databinding.LikeListBinding;
import com.fun.originalityprank.tool.StaticValue;


import java.util.List;

public class LikeAdapter extends RecyclerView.Adapter<LikeAdapter.LikeVH> {
    private final LayoutInflater minflater;
    private List<AllData> mlist;
    private int currentIndex = 0;
    private int listIndex = 0;

    public LikeAdapter(Activity activity, List<AllData> list) {
        minflater = LayoutInflater.from(activity);
        mlist = list;
    }

    @NonNull
    @Override
    public LikeVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LikeListBinding binding = LikeListBinding.inflate(minflater, parent, false);
        return new LikeVH(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull LikeVH holder, int position) {
        AllData likeData = mlist.get(position);
        Glide.with(minflater.getContext())
                .load(likeData.getPreUrl())
                .placeholder(R.mipmap.logo)
                .into(holder.getImage());
        holder.getText().setText(likeData.getTitle());

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

        Drawable[] likelistcons = {
                ContextCompat.getDrawable(minflater.getContext(), R.mipmap.like_list_1),
                ContextCompat.getDrawable(minflater.getContext(), R.mipmap.like_list_2),
                ContextCompat.getDrawable(minflater.getContext(), R.mipmap.like_list_3),
        };
        Drawable likelist = listbg(likelistcons);

        holder.getLikelistcons().setBackground(likelist);

        holder.getLikecons().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(minflater.getContext(), PrewViewActivity.class);
                intent.putExtra(StaticValue.KEY_TITLE, likeData.getTitle());
                minflater.getContext().startActivity(intent);
            }
        });
    }

    private Drawable listbg(Drawable[] colors) {

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

    }

    private Drawable Randomcolor(Drawable[] colors) {

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

    }

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

    public class LikeVH extends RecyclerView.ViewHolder {
        private ConstraintLayout likecons;
        private TextView text;
        private ImageView image;
        private ImageView likelistcons;
        private View view;

        public LikeVH(@NonNull LikeListBinding binding) {
            super(binding.getRoot());
            likelistcons = binding.likeListImageBg;
            text = binding.likeText;
            likecons = binding.cons;
            image = binding.likeImage;
            view = binding.likeView;
        }

        public TextView getText() {
            return text;
        }

        public View getView() {
            return view;
        }

        public ImageView getImage() {
            return image;
        }

        public ConstraintLayout getLikecons() {
            return likecons;
        }

        public ImageView getLikelistcons() {
            return likelistcons;
        }

    }
}
