package com.fun.funnyprank.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.funnyprank.R;
import com.fun.funnyprank.activity.PrewActivity;
import com.fun.funnyprank.data.LikeData;
import com.fun.funnyprank.data.StaticValue;
import com.fun.funnyprank.databinding.LikeListBinding;

import java.util.List;

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

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

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

    @Override
    public void onBindViewHolder(@NonNull LikeAdapter.LikeVH holder, int position) {
        LikeData likeData = mlist.get(position);
        if (likeData.getPreUrl().equals("custom_url")) {
            holder.getImage().setImageResource(R.mipmap.save_image);
        } else {
            Glide.with(minflater.getContext())
                    .load(likeData.getPreUrl())
                    .placeholder(R.mipmap.icon_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[] randomcolor = {
                ContextCompat.getDrawable(minflater.getContext(), R.drawable.like_background_1),
                ContextCompat.getDrawable(minflater.getContext(), R.drawable.like_background_2),
                ContextCompat.getDrawable(minflater.getContext(), R.drawable.like_background_3),
                ContextCompat.getDrawable(minflater.getContext(), R.drawable.like_background_4),
        };
        Drawable[] likelistcons = {
                ContextCompat.getDrawable(minflater.getContext(), R.mipmap.like_pg_1),
                ContextCompat.getDrawable(minflater.getContext(), R.mipmap.like_pg_2),
        };
        Drawable drawable = Randomcolor(randomcolor);
        Drawable likelist = listbg(likelistcons);

        holder.getLikecons().setBackground(drawable);
        holder.getLikelistcons().setBackground(likelist);

        holder.getLikecons().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(minflater.getContext(), PrewActivity.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, likelistcons;
        private TextView text;
        private ImageView image;
        private View view;

        public LikeVH(@NonNull LikeListBinding binding) {
            super(binding.getRoot());
            likecons = binding.likeConsList;
            likelistcons = binding.likeConsListBg;
            text = binding.likeText;
            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 ConstraintLayout getLikelistcons() {
            return likelistcons;
        }

    }
}
