package org.mercury_im.messenger.ui.chat;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import org.jxmpp.jid.EntityBareJid;
import org.mercury_im.messenger.MercuryImApplication;
import org.mercury_im.messenger.data.repository.DirectChatRepository;
import org.mercury_im.messenger.data.repository.MessageRepository;
import org.mercury_im.messenger.data.repository.PeerRepository;
import org.mercury_im.messenger.entity.chat.DirectChat;
import org.mercury_im.messenger.entity.contact.Peer;
import org.mercury_im.messenger.entity.message.Message;

import java.util.List;
import java.util.UUID;

import javax.inject.Inject;

import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class ChatViewModel extends ViewModel {

    private final CompositeDisposable disposable = new CompositeDisposable();

    @Inject
    PeerRepository contactRepository;

    @Inject
    DirectChatRepository chatRepository;

    @Inject
    MessageRepository messageRepository;

    private MutableLiveData<Peer> contact = new MutableLiveData<>();
    private MutableLiveData<List<Message>> messages = new MutableLiveData<>();
    private MutableLiveData<String> contactDisplayName = new MutableLiveData<>();
    private MutableLiveData<DirectChat> chat = new MutableLiveData<>();

    public ChatViewModel() {
        super();
        MercuryImApplication.getApplication().getAppComponent().inject(this);
    }

    public void init(UUID accountId, EntityBareJid jid) {
        disposable.add(contactRepository.getOrCreatePeer(accountId, jid.toString())
                .subscribe((Consumer<Peer>) this::init));
    }

    public void init(Peer peer) {
        disposable.add(chatRepository.getOrCreateChatWithPeer(peer)
                .subscribe((Consumer<DirectChat>) this::init));
    }

    public void init(DirectChat chat) {
        this.chat.setValue(chat);

        // Subscribe peer
        disposable.add(contactRepository.observePeer(chat.getPeer().getId())
                .subscribe(peer -> contactDisplayName.setValue(peer.getItem().getName())));

        // Subscribe messages
        disposable.add(messageRepository.observeMessages(chat)
                .subscribe(ChatViewModel.this.messages::setValue));
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        disposable.clear();
    }

    public LiveData<List<Message>> getMessages() {
        return messages;
    }

    public LiveData<Peer> getContact() {
        return contact;
    }

    public LiveData<String> getContactDisplayName() {
        return contactDisplayName;
    }

    public void queryTextChanged(String query) {
        Observable<List<Message>> observable = query.isEmpty() ?
                messageRepository.observeMessages(chat.getValue()) :
                messageRepository.findMessagesWithBody(chat.getValue(), query);

        disposable.add(observable.subscribe(messages ->
                ChatViewModel.this.messages.setValue(messages)));
    }

    public Completable requestMamMessages() {
        /*
        return Completable.fromAction(() -> {
            ChatModel chatModel = ChatViewModel.this.chat.getValue();
            if (chatModel == null) {
                return;
            }
            connectionCenter.requestMamMessagesFor(chatModel);
        });

         */
        return null;
    }
}
