package de.beyondsoft.ownchat.data.repository.specifications;

import de.beyondsoft.ownchat.data.repository.models.MessageRealm;
import io.realm.Case;
import io.realm.Realm;
import io.realm.RealmQuery;

public class MessagesByGroupAndSearchTextSpecification implements RealmSpecification {
    private long mGroupId;
    private String mSearchText;
    private final long mUserId;

    public MessagesByGroupAndSearchTextSpecification(long userId) {
        mUserId = userId;
    }

    public void setGroupId(long groupId) {
        mGroupId = groupId;
    }

    public void setSearchText(String searchText) { mSearchText = searchText; }

    @Override
    public RealmQuery<MessageRealm> toRealmQuery(Realm realm) {
        return realm.where(MessageRealm.class)
                .equalTo(MessageRealm.GROUP_ID, mGroupId)
                .equalTo(MessageRealm.USER_ID, mUserId)
                .beginGroup()
                .contains(MessageRealm.MESSAGE, mSearchText, Case.INSENSITIVE)
                .or()
                .contains(MessageRealm.FILENAME, mSearchText, Case.INSENSITIVE)
                .endGroup();
    }
}
