// Path: firestore/app/src/main/java/com/google/firebase/example/fireeats/java/model/Rating.java
// public class Rating {
// 
//     private String userId;
//     private String userName;
//     private double rating;
//     private String text;
//     private @ServerTimestamp Date timestamp;
// 
//     public Rating() {}
// 
//     public Rating(FirebaseUser user, double rating, String text) {
//         this.userId = user.getUid();
//         this.userName = user.getDisplayName();
//         if (TextUtils.isEmpty(this.userName)) {
//             this.userName = user.getEmail();
//         }
// 
//         this.rating = rating;
//         this.text = text;
//     }
// 
//     public String getUserId() {
//         return userId;
//     }
// 
//     public void setUserId(String userId) {
//         this.userId = userId;
//     }
// 
//     public String getUserName() {
//         return userName;
//     }
// 
//     public void setUserName(String userName) {
//         this.userName = userName;
//     }
// 
//     public double getRating() {
//         return rating;
//     }
// 
//     public void setRating(double rating) {
//         this.rating = rating;
//     }
// 
//     public String getText() {
//         return text;
//     }
// 
//     public void setText(String text) {
//         this.text = text;
//     }
// 
//     public Date getTimestamp() {
//         return timestamp;
//     }
// 
//     public void setTimestamp(Date timestamp) {
//         this.timestamp = timestamp;
//     }
// }
// 
// Path: firestore/app/src/main/java/com/google/firebase/example/fireeats/java/model/Restaurant.java
// @IgnoreExtraProperties
// public class Restaurant {
// 
//     public static final String FIELD_CITY = "city";
//     public static final String FIELD_CATEGORY = "category";
//     public static final String FIELD_PRICE = "price";
//     public static final String FIELD_POPULARITY = "numRatings";
//     public static final String FIELD_AVG_RATING = "avgRating";
// 
//     private String name;
//     private String city;
//     private String category;
//     private String photo;
//     private int price;
//     private int numRatings;
//     private double avgRating;
// 
//     public Restaurant() {}
// 
//     public Restaurant(String name, String city, String category, String photo,
//                       int price, int numRatings, double avgRating) {
//         this.name = name;
//         this.city = city;
//         this.category = category;
//         this.photo = photo;
//         this.price = price;
//         this.numRatings = numRatings;
//         this.avgRating = avgRating;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public String getCity() {
//         return city;
//     }
// 
//     public void setCity(String city) {
//         this.city = city;
//     }
// 
//     public String getCategory() {
//         return category;
//     }
// 
//     public void setCategory(String category) {
//         this.category = category;
//     }
// 
//     public String getPhoto() {
//         return photo;
//     }
// 
//     public void setPhoto(String photo) {
//         this.photo = photo;
//     }
// 
//     public int getPrice() {
//         return price;
//     }
// 
//     public void setPrice(int price) {
//         this.price = price;
//     }
// 
//     public int getNumRatings() {
//         return numRatings;
//     }
// 
//     public void setNumRatings(int numRatings) {
//         this.numRatings = numRatings;
//     }
// 
//     public double getAvgRating() {
//         return avgRating;
//     }
// 
//     public void setAvgRating(double avgRating) {
//         this.avgRating = avgRating;
//     }
// }


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.view.inputmethod.InputMethodManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.bumptech.glide.Glide;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.snackbar.Snackbar;
import com.google.firebase.example.fireeats.R;
import com.google.firebase.example.fireeats.databinding.FragmentRestaurantDetailBinding;
import com.google.firebase.example.fireeats.java.adapter.RatingAdapter;
import com.google.firebase.example.fireeats.java.model.Rating;
import com.google.firebase.example.fireeats.java.model.Restaurant;
import com.google.firebase.example.fireeats.java.util.RestaurantUtil;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.ListenerRegistration;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.Transaction;
    @Override
    public void onStart() {
        super.onStart();

        mRatingAdapter.startListening();
        mRestaurantRegistration = mRestaurantRef.addSnapshotListener(this);
    }

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

        mRatingAdapter.stopListening();

        if (mRestaurantRegistration != null) {
            mRestaurantRegistration.remove();
            mRestaurantRegistration = null;
        }
    }

    /**
     * Listener for the Restaurant document ({@link #mRestaurantRef}).
     */
    @Override
    public void onEvent(DocumentSnapshot snapshot, FirebaseFirestoreException e) {
        if (e != null) {
            Log.w(TAG, "restaurant:onEvent", e);
            return;
        }

// Path: firestore/app/src/main/java/com/google/firebase/example/fireeats/java/model/Rating.java
// public class Rating {
// 
//     private String userId;
//     private String userName;
//     private double rating;
//     private String text;
//     private @ServerTimestamp Date timestamp;
// 
//     public Rating() {}
// 
//     public Rating(FirebaseUser user, double rating, String text) {
//         this.userId = user.getUid();
//         this.userName = user.getDisplayName();
//         if (TextUtils.isEmpty(this.userName)) {
//             this.userName = user.getEmail();
//         }
// 
//         this.rating = rating;
//         this.text = text;
//     }
// 
//     public String getUserId() {
//         return userId;
//     }
// 
//     public void setUserId(String userId) {
//         this.userId = userId;
//     }
// 
//     public String getUserName() {
//         return userName;
//     }
// 
//     public void setUserName(String userName) {
//         this.userName = userName;
//     }
// 
//     public double getRating() {
//         return rating;
//     }
// 
//     public void setRating(double rating) {
//         this.rating = rating;
//     }
// 
//     public String getText() {
//         return text;
//     }
// 
//     public void setText(String text) {
//         this.text = text;
//     }
// 
//     public Date getTimestamp() {
//         return timestamp;
//     }
// 
//     public void setTimestamp(Date timestamp) {
//         this.timestamp = timestamp;
//     }
// }
// 
// Path: firestore/app/src/main/java/com/google/firebase/example/fireeats/java/model/Restaurant.java
// @IgnoreExtraProperties
// public class Restaurant {
// 
//     public static final String FIELD_CITY = "city";
//     public static final String FIELD_CATEGORY = "category";
//     public static final String FIELD_PRICE = "price";
//     public static final String FIELD_POPULARITY = "numRatings";
//     public static final String FIELD_AVG_RATING = "avgRating";
// 
//     private String name;
//     private String city;
//     private String category;
//     private String photo;
//     private int price;
//     private int numRatings;
//     private double avgRating;
// 
//     public Restaurant() {}
// 
//     public Restaurant(String name, String city, String category, String photo,
//                       int price, int numRatings, double avgRating) {
//         this.name = name;
//         this.city = city;
//         this.category = category;
//         this.photo = photo;
//         this.price = price;
//         this.numRatings = numRatings;
//         this.avgRating = avgRating;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public String getCity() {
//         return city;
//     }
// 
//     public void setCity(String city) {
//         this.city = city;
//     }
// 
//     public String getCategory() {
//         return category;
//     }
// 
//     public void setCategory(String category) {
//         this.category = category;
//     }
// 
//     public String getPhoto() {
//         return photo;
//     }
// 
//     public void setPhoto(String photo) {
//         this.photo = photo;
//     }
// 
//     public int getPrice() {
//         return price;
//     }
// 
//     public void setPrice(int price) {
//         this.price = price;
//     }
// 
//     public int getNumRatings() {
//         return numRatings;
//     }
// 
//     public void setNumRatings(int numRatings) {
//         this.numRatings = numRatings;
//     }
// 
//     public double getAvgRating() {
//         return avgRating;
//     }
// 
//     public void setAvgRating(double avgRating) {
//         this.avgRating = avgRating;
//     }
// }


// Path: firestore/app/src/main/java/com/google/firebase/example/fireeats/java/RestaurantDetailFragment.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.view.inputmethod.InputMethodManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.bumptech.glide.Glide;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.snackbar.Snackbar;
import com.google.firebase.example.fireeats.R;
import com.google.firebase.example.fireeats.databinding.FragmentRestaurantDetailBinding;
import com.google.firebase.example.fireeats.java.adapter.RatingAdapter;
import com.google.firebase.example.fireeats.java.model.Rating;
import com.google.firebase.example.fireeats.java.model.Restaurant;
import com.google.firebase.example.fireeats.java.util.RestaurantUtil;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.ListenerRegistration;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.Transaction;

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

        mRatingAdapter.startListening();
        mRestaurantRegistration = mRestaurantRef.addSnapshotListener(this);
    }

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

        mRatingAdapter.stopListening();

        if (mRestaurantRegistration != null) {
            mRestaurantRegistration.remove();
            mRestaurantRegistration = null;
        }
    }

    /**
     * Listener for the Restaurant document ({@link #mRestaurantRef}).
     */
    @Override
    public void onEvent(DocumentSnapshot snapshot, FirebaseFirestoreException e) {
        if (e != null) {
            Log.w(TAG, "restaurant:onEvent", e);
            return;
        }

        onRestaurantLoaded(snapshot.toObject(Restaurant.class));