package org.mercury_im.messenger.data.repository.dao;

import org.mercury_im.messenger.data.model.DirectChatModel;
import org.mercury_im.messenger.data.model.DirectMessagesRelation;
import org.mercury_im.messenger.data.model.GroupChatModel;
import org.mercury_im.messenger.data.model.GroupMessagesRelation;
import org.mercury_im.messenger.data.model.MessageModel;

import java.util.UUID;

import io.reactivex.Single;
import io.requery.Persistable;
import io.requery.reactivex.ReactiveEntityStore;
import io.requery.reactivex.ReactiveResult;

public class MessageDao extends RequeryDao {

    private final DirectChatDao directChatDao;
    private final GroupChatDao groupChatDao;

    public MessageDao(ReactiveEntityStore<Persistable> data) {
        super(data);
        this.directChatDao = new DirectChatDao(data);
        this.groupChatDao = new GroupChatDao(data);
    }

    public Single<MessageModel> insert(MessageModel message) {
        return data().insert(message);
    }

    public ReactiveResult<MessageModel> get(UUID messageId) {
        return data().select(MessageModel.class)
                .where(MessageModel.ID.eq(messageId))
                .get();
    }



    private DirectMessagesRelation toRelation(DirectChatModel chat, MessageModel message) {
        DirectMessagesRelation relation = new DirectMessagesRelation();
        relation.setChat(chat);
        relation.setMessage(message);
        return relation;
    }

    private GroupMessagesRelation toRelation(GroupChatModel chat, MessageModel message) {
        GroupMessagesRelation relation = new GroupMessagesRelation();
        relation.setChat(chat);
        relation.setMessage(message);
        return relation;
    }
}
