package com.ttiglobal.inttilectgpchat.blog.Helper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.github.siyamed.shapeimageview.CircularImageView;
import com.ttiglobal.inttilectgpchat.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class CommentRecyclerAdapter extends RecyclerView.Adapter<CommentRecyclerAdapter.ViewHolder> {

    private ArrayList<HashMap<String, String>> items;
    private Context context;

    public CommentRecyclerAdapter(Context context, ArrayList<HashMap<String, String>> items) {
        this.items = items;
        this.context = context;
    }

    @NonNull
    @Override
    public CommentRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_view_comments, parent, false);
        return new CommentRecyclerAdapter.ViewHolder(v);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull CommentRecyclerAdapter.ViewHolder holder, int position) {

        if (items.get(position).get("message_text") != null)
            holder.txtCommentComment.setText(items.get(position).get("message_text"));
        if (items.get(position).get("file_url") != null) {
            Glide.with(context)
                    .applyDefaultRequestOptions(new RequestOptions()
                            .placeholder(R.drawable.user1)
                            .error(R.drawable.user1)
                            .diskCacheStrategy(DiskCacheStrategy.ALL))
                    .load(items.get(position).get("file_url"))
                    .into(holder.imgCommentUser);
        }

        if (items.get(position).get("sender_name") != null)

            holder.txtCommentName.setText(items.get(position).get("sender_name"));


        ArrayList list;
        if (items.get(position).get("reply_messages") != null)
            list = new ArrayList();
        list = new ArrayList(Collections.singleton(items.get(position).get("reply_messages")));
        if (list.size() > 0)
            for (int i = 0; i < list.size(); i++) {
                ArrayList<HashMap<String, String>> reply_messages = (ArrayList<HashMap<String, String>>) list.get(i);
                if (reply_messages != null && reply_messages.size() > 0)
                    if (reply_messages.get(i).get("replier_message") != null) {
                        holder.txtMoreComment.setText(reply_messages.get(i).get("replier_message"));
                        if (list.size() < 1) {
                            holder.llout_reply.setVisibility(View.VISIBLE);
                            holder.txtViewMore.setVisibility(View.VISIBLE);
                            holder.txtViewMore.setText("View more " + list.size() + " reply");
                        } else {
                            list.size();
                            holder.llout_reply.setVisibility(View.VISIBLE);
                            holder.txtViewMore.setVisibility(View.VISIBLE);
                            holder.txtViewMore.setText("View more " + list.size() + " replies");
                        }
                    }
            }

        holder.view.setTag(items);
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {

        private View view;
        private CircularImageView imgCommentUser, imgCommentReply;
        private ImageView imgLike;
        private TextView txtCommentName, txtCommentTime, txtCommentComment, txtCommentLike, txtCommentReply, txtCommentLikes, txtViewMore, txtMoreName, txtMoreComment;
        private LinearLayout llout_reply;

        private ViewHolder(View itemView) {
            super(itemView);
            view = itemView;

            imgCommentUser = view.findViewById(R.id.img_view_comments);
            txtCommentName = view.findViewById(R.id.txt_comment_name);
//            txtCommentTime = view.findViewById(R.id.txt_comment_time);
            txtCommentComment = view.findViewById(R.id.txt_comment_comments);
           /* txtCommentLike = view.findViewById(R.id.txt_like);
            txtCommentReply = view.findViewById(R.id.txt_reply);
            txtCommentLikes = view.findViewById(R.id.txt_total_likes);*/
            txtMoreComment = view.findViewById(R.id.txt_more_comment);
            txtViewMore = view.findViewById(R.id.txt_view_more);
            txtMoreName = view.findViewById(R.id.txt_more_name);
//            imgLike = view.findViewById(R.id.img_like);
            llout_reply = view.findViewById(R.id.llout_reply);
        }
    }
}