package com.fun.airhornprank.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.airhornprank.R;
import com.fun.airhornprank.activity.PreViewActivity;
import com.fun.airhornprank.data.Mydata;
import com.fun.airhornprank.databinding.LikeListBinding;
import com.fun.airhornprank.tool.StaticValue;

import java.util.List;

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

    public LikeAdapter(Activity activity, List<Mydata> 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) {
        Mydata 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.logo)
                    .into(holder.getImage());
        }
        holder.getText().setText(likeData.getTitle());

        if (position == getItemCount() - 1) {
            holder.getView().setVisibility(View.VISIBLE);
        } else {
            holder.getView().setVisibility(View.GONE);
        }
        holder.getLikecons().setBackgroundResource(R.drawable.like_item_bg);
        holder.getLikecons().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(minflater.getContext(), PreViewActivity.class);
                intent.putExtra(StaticValue.KEY_TITLE, likeData.getTitle());
                minflater.getContext().startActivity(intent);
            }
        });
    }


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

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

        private View view;

        public LikeVH(@NonNull LikeListBinding binding) {
            super(binding.getRoot());

            text = binding.likeText;
            likecons = binding.likeCons;
            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;
        }

    }
}
