// Path: database/app/src/main/java/com/google/firebase/quickstart/database/java/models/Comment.java
// @IgnoreExtraProperties
// public class Comment {
// 
//     public String uid;
//     public String author;
//     public String text;
// 
//     public Comment() {
//         // Default constructor required for calls to DataSnapshot.getValue(Comment.class)
//     }
// 
//     public Comment(String uid, String author, String text) {
//         this.uid = uid;
//         this.author = author;
//         this.text = text;
//     }
// 
// }
// 
// Path: database/app/src/main/java/com/google/firebase/quickstart/database/java/models/Post.java
// @IgnoreExtraProperties
// public class Post {
// 
//     public String uid;
//     public String author;
//     public String title;
//     public String body;
//     public int starCount = 0;
//     public Map<String, Boolean> stars = new HashMap<>();
// 
//     public Post() {
//         // Default constructor required for calls to DataSnapshot.getValue(Post.class)
//     }
// 
//     public Post(String uid, String author, String title, String body) {
//         this.uid = uid;
//         this.author = author;
//         this.title = title;
//         this.body = body;
//     }
// 
//     @Exclude
//     public Map<String, Object> toMap() {
//         HashMap<String, Object> result = new HashMap<>();
//         result.put("uid", uid);
//         result.put("author", author);
//         result.put("title", title);
//         result.put("body", body);
//         result.put("starCount", starCount);
//         result.put("stars", stars);
// 
//         return result;
//     }
// 
// }
// 
// Path: database/app/src/main/java/com/google/firebase/quickstart/database/java/models/User.java
// @IgnoreExtraProperties
// public class User {
// 
//     public String username;
//     public String email;
// 
//     public User() {
//         // Default constructor required for calls to DataSnapshot.getValue(User.class)
//     }
// 
//     public User(String username, String email) {
//         this.username = username;
//         this.email = email;
//     }
// 
// }
// 
// Path: database/app/src/main/java/com/google/firebase/quickstart/database/java/viewholder/CommentViewHolder.java
// public class CommentViewHolder extends RecyclerView.ViewHolder {
//     public TextView authorView;
//     public TextView bodyView;
// 
//     public CommentViewHolder(View itemView) {
//         super(itemView);
//         authorView = itemView.findViewById(R.id.commentAuthor);
//         bodyView = itemView.findViewById(R.id.commentBody);
//     }
// }


import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.quickstart.database.R;
import com.google.firebase.quickstart.database.databinding.FragmentPostDetailBinding;
import com.google.firebase.quickstart.database.java.models.Comment;
import com.google.firebase.quickstart.database.java.models.Post;
import com.google.firebase.quickstart.database.java.models.User;
import com.google.firebase.quickstart.database.java.viewholder.CommentViewHolder;
import java.util.ArrayList;
import java.util.List;
        // Get post key from arguments
        mPostKey = requireArguments().getString(EXTRA_POST_KEY);
        if (mPostKey == null) {
            throw new IllegalArgumentException("Must pass EXTRA_POST_KEY");
        }

        // Initialize Database
        mPostReference = FirebaseDatabase.getInstance().getReference()
                .child("posts").child(mPostKey);
        mCommentsReference = FirebaseDatabase.getInstance().getReference()
                .child("post-comments").child(mPostKey);

        binding.buttonPostComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                postComment();
            }
        });
        binding.recyclerPostComments.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    public void onStart() {
        super.onStart();

        // Add value event listener to the post
        ValueEventListener postListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // Get Post object and use the values to update the UI
// Path: database/app/src/main/java/com/google/firebase/quickstart/database/java/models/Comment.java
// @IgnoreExtraProperties
// public class Comment {
// 
//     public String uid;
//     public String author;
//     public String text;
// 
//     public Comment() {
//         // Default constructor required for calls to DataSnapshot.getValue(Comment.class)
//     }
// 
//     public Comment(String uid, String author, String text) {
//         this.uid = uid;
//         this.author = author;
//         this.text = text;
//     }
// 
// }
// 
// Path: database/app/src/main/java/com/google/firebase/quickstart/database/java/models/Post.java
// @IgnoreExtraProperties
// public class Post {
// 
//     public String uid;
//     public String author;
//     public String title;
//     public String body;
//     public int starCount = 0;
//     public Map<String, Boolean> stars = new HashMap<>();
// 
//     public Post() {
//         // Default constructor required for calls to DataSnapshot.getValue(Post.class)
//     }
// 
//     public Post(String uid, String author, String title, String body) {
//         this.uid = uid;
//         this.author = author;
//         this.title = title;
//         this.body = body;
//     }
// 
//     @Exclude
//     public Map<String, Object> toMap() {
//         HashMap<String, Object> result = new HashMap<>();
//         result.put("uid", uid);
//         result.put("author", author);
//         result.put("title", title);
//         result.put("body", body);
//         result.put("starCount", starCount);
//         result.put("stars", stars);
// 
//         return result;
//     }
// 
// }
// 
// Path: database/app/src/main/java/com/google/firebase/quickstart/database/java/models/User.java
// @IgnoreExtraProperties
// public class User {
// 
//     public String username;
//     public String email;
// 
//     public User() {
//         // Default constructor required for calls to DataSnapshot.getValue(User.class)
//     }
// 
//     public User(String username, String email) {
//         this.username = username;
//         this.email = email;
//     }
// 
// }
// 
// Path: database/app/src/main/java/com/google/firebase/quickstart/database/java/viewholder/CommentViewHolder.java
// public class CommentViewHolder extends RecyclerView.ViewHolder {
//     public TextView authorView;
//     public TextView bodyView;
// 
//     public CommentViewHolder(View itemView) {
//         super(itemView);
//         authorView = itemView.findViewById(R.id.commentAuthor);
//         bodyView = itemView.findViewById(R.id.commentBody);
//     }
// }


// Path: database/app/src/main/java/com/google/firebase/quickstart/database/java/PostDetailFragment.java
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.quickstart.database.R;
import com.google.firebase.quickstart.database.databinding.FragmentPostDetailBinding;
import com.google.firebase.quickstart.database.java.models.Comment;
import com.google.firebase.quickstart.database.java.models.Post;
import com.google.firebase.quickstart.database.java.models.User;
import com.google.firebase.quickstart.database.java.viewholder.CommentViewHolder;
import java.util.ArrayList;
import java.util.List;

        // Get post key from arguments
        mPostKey = requireArguments().getString(EXTRA_POST_KEY);
        if (mPostKey == null) {
            throw new IllegalArgumentException("Must pass EXTRA_POST_KEY");
        }

        // Initialize Database
        mPostReference = FirebaseDatabase.getInstance().getReference()
                .child("posts").child(mPostKey);
        mCommentsReference = FirebaseDatabase.getInstance().getReference()
                .child("post-comments").child(mPostKey);

        binding.buttonPostComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                postComment();
            }
        });
        binding.recyclerPostComments.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    public void onStart() {
        super.onStart();

        // Add value event listener to the post
        ValueEventListener postListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // Get Post object and use the values to update the UI
                Post post = dataSnapshot.getValue(Post.class);