package com.ttiglobal.inttilectgpchat.blog.Helper;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

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.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.ttiglobal.inttilectgpchat.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;

import de.hdodenhof.circleimageview.CircleImageView;

public class HoViewHolder extends RecyclerView.ViewHolder {

    View view;
    FirebaseAuth mAuth;
    String user_id;
    ArrayList<HashMap<String, String>> childHashMap = new ArrayList<>();
    private LinearLayout postapproval;
    TextView txt_post_approval;
    String refid;


    public HoViewHolder(View itemView) {
        super(itemView);
        view = itemView;

        mAuth = FirebaseAuth.getInstance();
        user_id = Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid();

        txt_post_approval=view.findViewById(R.id.txt_post_approval);
        postapproval = view.findViewById(R.id.postapproval);
    }

    public void setImage(String url, Context ctx) {
        final ImageView post_image = view.findViewById(R.id.post_image);
        Glide.with(ctx)
                .applyDefaultRequestOptions(new RequestOptions()
                        .placeholder(R.drawable.image_not_available)
                        .error(R.drawable.image_not_available)
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .load(url)
                .into(post_image);
    }

    public void setUser(final String user_name, final String file_url, Context applicationContext) {
        final TextView username = view.findViewById(R.id.user_name);
        final CircleImageView userimage = view.findViewById(R.id.profile_image);

        username.setText(user_name);
        Glide.with(applicationContext)
                .applyDefaultRequestOptions(new RequestOptions()
                        .placeholder(R.drawable.user1)
                        .error(R.drawable.user1)
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .load(file_url)
                .into(userimage);
    }

    public void setTitle(String title) {
        TextView head = view.findViewById(R.id.post_title);
        head.setText(title);
    }

    public void setDesc(String desc) {
        TextView post_desc = view.findViewById(R.id.post_desc);
        post_desc.setText(desc);
    }

    public void setApprove(final String is_ho_approved, String ref_id) {

        final String refid = ref_id;
        final String sucess = "Approved";
        final String hoapprove = is_ho_approved;
        final String failed = "Rejected";
//        txt_post_approval.setText(failed);
        if(is_ho_approved == "1"){

            txt_post_approval.setText(sucess);

        }
        else if (is_ho_approved == "0"){

            txt_post_approval.setText(failed);

        }



        postapproval.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                DocumentReference hoapprove = FirebaseFirestore.getInstance().collection("forum_datas").document(refid);
                hoapprove.update("is_ho_approved","1" ,"is_ho_rejected","0");
                txt_post_approval.setText(sucess);





            }
        });
    }


//    public void showPostDetails(final String sender_name, final String refId, final String imgurl, final String user, final String filetype, String time, final String title, final String details, final int size, final Activity activity) {
//
//        final TextView post_title = view.findViewById(R.id.post_title);
//        TextView post_desc = view.findViewById(R.id.post_desc);
//
//        view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (filetype != null && filetype.equalsIgnoreCase("1")) {
//                    final Intent blogIntent = new Intent(activity, BlogDetails.class);
//                    blogIntent.putExtra("file_url", imgurl);
//                    blogIntent.putExtra("file_type", filetype);
//                    blogIntent.putExtra("user_id", user);
//                    blogIntent.putExtra("blog_title", title);
//                    blogIntent.putExtra("blog_desc", details);
//                    blogIntent.putExtra("ref_id", refId);
//                    blogIntent.putExtra("sender_name",sender_name);
//                    activity.startActivity(blogIntent);
//                } else {
//                    final Intent videoIntent = new Intent(activity, VideoView.class);
//                    videoIntent.putExtra("filepath", imgurl);
//                    activity.startActivity(videoIntent);
//
//                }
//            }
//        });
//
//        post_desc.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                post_title.performClick();
//            }
//        });
//    }
}