package com.firebase.ui.firestore.paging;

import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.Query;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;

/**
 * Key for Firestore pagination. Holds the DocumentSnapshot(s) that bound the page.
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class PageKey {

    private final DocumentSnapshot mStartAfter;
    private final DocumentSnapshot mEndBefore;

    public PageKey(@Nullable DocumentSnapshot startAfter, @Nullable DocumentSnapshot endBefore) {
        mStartAfter = startAfter;
        mEndBefore = endBefore;
    }

    @NonNull
    public Query getPageQuery(@NonNull Query baseQuery, int size) {
        Query pageQuery = baseQuery;

        if (mStartAfter != null) {
            pageQuery = pageQuery.startAfter(mStartAfter);
        }

        if (mEndBefore != null) {
            pageQuery = pageQuery.endBefore(mEndBefore);
        } else {
            pageQuery = pageQuery.limit(size);
        }

        return pageQuery;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PageKey key = (PageKey) o;
        if (mStartAfter == null && key.mStartAfter == null)
            return true;
        if (mEndBefore == null && key.mEndBefore == null)
            return true;
        return mStartAfter.getId() == key.mStartAfter.getId() &&
                mEndBefore.getId() == key.mEndBefore.getId();
    }

    @Override
    @NonNull
    public String toString() {
        String startAfter = mStartAfter == null ? null : mStartAfter.getId();
        String endBefore = mEndBefore == null ? null : mEndBefore.getId();
        return "PageKey{" +
                "StartAfter=" + startAfter +
                ", EndBefore=" + endBefore +
                '}';
    }
}

@@ -1339,33 +1339,35 @@
 artAfter == null
-)
+ &&
 %0A            ret
@@ -1367,33 +1367,12 @@
     
-return true;%0A        if (
+    
 mEnd
@@ -1468,28 +1468,32 @@
 fter.getId()
- == 
+.equals(
 key.mStartAf
@@ -1503,16 +1503,17 @@
 .getId()
+)
  &&%0A    
@@ -1542,20 +1542,24 @@
 .getId()
- == 
+.equals(
 key.mEnd
@@ -1568,24 +1568,25 @@
 fore.getId()
+)
 ;%0A    %7D%0A%0A   

fix page keys comparison