package org.mercury_im.messenger.data.repository;

import org.mercury_im.messenger.data.mapping.PeerMapping;
import org.mercury_im.messenger.data.model.PeerModel;
import org.mercury_im.messenger.entity.Account;
import org.mercury_im.messenger.entity.contact.Peer;
import org.mercury_im.messenger.entity.contact.SubscriptionDirection;
import org.mercury_im.messenger.util.Optional;
import org.mercury_im.messenger.util.ThreadUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import javax.inject.Inject;
import javax.inject.Named;

import io.reactivex.Completable;
import io.reactivex.Maybe;
import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.Single;
import io.requery.Persistable;
import io.requery.query.Expression;
import io.requery.query.LogicalCondition;
import io.requery.query.ResultDelegate;
import io.requery.reactivex.ReactiveEntityStore;

public class XmppPeerRepository
        extends RequeryRepository
        implements PeerRepository {

    private final AccountRepository accountRepository;

    private final PeerMapping peerMapping;

    @Inject
    public XmppPeerRepository(ReactiveEntityStore<Persistable> data,
                              @Named(value = ThreadUtils.SCHEDULER_IO) Scheduler subscriberScheduler,
                              @Named(value = ThreadUtils.SCHEDULER_UI) Scheduler observerScheduler,
                              PeerMapping peerMapping, AccountRepository accountRepository) {
        super(data, subscriberScheduler, observerScheduler);
        this.peerMapping = peerMapping;
        this.accountRepository = accountRepository;
    }

    @Override
    public Single<Peer> insertPeer(Peer peer) {
        return data().insert(peerMapping.toModel(peer, new PeerModel()))
                .map(model -> peerMapping.toEntity(model, peer))
                .subscribeOn(subscriberScheduler())
                .observeOn(observerScheduler());
    }

    @Override
    public Observable<Optional<Peer>> observePeer(UUID peerId) {
        return data().select(PeerModel.class)
                .where(PeerModel.ID.eq(peerId))
                .get().observableResult()
                .map(result -> new Optional<>(peerMapping.toEntity(result.firstOrNull(), new Peer())))
                .subscribeOn(subscriberScheduler())
                .observeOn(observerScheduler());
    }

    @Override
    public Maybe<Peer> getPeer(UUID peerId) {
        return data().select(PeerModel.class)
                .where(PeerModel.ID.eq(peerId))
                .get().maybe()
                .map(model -> peerMapping.toEntity(model, new Peer()))
                .subscribeOn(subscriberScheduler())
                .observeOn(observerScheduler());
    }

    @Override
    public Observable<Optional<Peer>> observePeerByAddress(UUID accountId, String address) {
        return data().select(PeerModel.class)
                .where(PeerModel.ACCOUNT_ID.eq(accountId))
                .and(PeerModel.ADDRESS.eq(address))
                .get().observableResult()
                .map(result -> new Optional<>(result.firstOrNull()))
                .map(peerMapping::toEntity)
                .subscribeOn(subscriberScheduler())
                .observeOn(observerScheduler());
    }

    @Override
    public Single<Peer> getOrCreatePeer(UUID accountId, String address) {
        return accountRepository.getAccount(accountId).toSingle()
                .flatMap(account -> getOrCreatePeer(account, address));
    }

    @Override
    public Maybe<Peer> getPeerByAddress(UUID accountId, String address) {
        return data().select(PeerModel.class)
                .where(PeerModel.ACCOUNT_ID.eq(accountId))
                .and(PeerModel.ADDRESS.eq(address))
                .get().maybe()
                .map(peerMapping::toEntity)
                .subscribeOn(subscriberScheduler())
                .observeOn(observerScheduler());
    }

    @Override
    public Single<Peer> getOrCreatePeer(Account account, String address) {
        return getPeerByAddress(account, address)
                .switchIfEmpty(Single
                        .just(new Peer())
                        .map(peer -> {
                            peer.setAccount(account);
                            peer.setAddress(address);
                            return peer;
                        })
                        .flatMap(this::insertPeer))
                .subscribeOn(subscriberScheduler())
                .observeOn(observerScheduler());
    }

    @Override
    public Observable<List<Peer>> observeAllPeers() {
        return data().select(PeerModel.class)
                .orderBy(PeerModel.ADDRESS)
                .get().observableResult()
                .map(ResultDelegate::toList)
                .map(this::peerModelsToEntities)
                .subscribeOn(subscriberScheduler())
                .observeOn(observerScheduler());
    }

    private List<Peer> peerModelsToEntities(List<PeerModel> peerModels) {
        List<Peer> peerEntities = new ArrayList<>(peerModels.size());
        for (PeerModel model : peerModels) {
            peerEntities.add(peerMapping.toEntity(model));
        }
        return peerEntities;
    }

    @Override
    public Observable<List<Peer>> observeAllContactsOfAccount(UUID accountId) {
        return data().select(PeerModel.class)
                .where(PeerModel.ACCOUNT_ID.eq(accountId))
                .and(isContact())
                .get().observableResult()
                .map(ResultDelegate::toList)
                .map(this::peerModelsToEntities)
                .subscribeOn(subscriberScheduler())
                .observeOn(observerScheduler());
    }

    private LogicalCondition<? extends Expression<SubscriptionDirection>, ?> isContact() {
        return PeerModel.SUBSCRIPTION_DIRECTION.in(Arrays.asList(
                SubscriptionDirection.both,
                SubscriptionDirection.to));
    }

    @Override
    public Single<Peer> updatePeer(Peer peer) {
        // In order to update, we fetch the model, update it and write it back.
        return data().select(PeerModel.class)
                .where(PeerModel.ID.eq(peer.getId()))
                .get().maybe().toSingle()
                // write changes into model
                .map(model -> peerMapping.toModel(peer, model))
                .flatMap(data()::update)
                .map(model -> peerMapping.toEntity(model, peer))
                .subscribeOn(subscriberScheduler())
                .observeOn(observerScheduler());
    }

    @Override
    public Single<Peer> upsertPeer(Peer peer) {
        return data().select(PeerModel.class)
                .where(PeerModel.ID.eq(peer.getId()))
                .get().maybe()
                // if not exists, create
                .switchIfEmpty(Single.just(peer)
                        .map(peerMapping::toModel)
                        .flatMap(data()::insert))
                // write changes into fetched model
                .map(model -> peerMapping.toModel(peer, model))
                // write changed model back to db
                .flatMap(data()::update)
                .map(model -> peerMapping.toEntity(model, peer))
                .subscribeOn(subscriberScheduler())
                .observeOn(observerScheduler());
    }

    @Override
    public Completable deletePeer(Peer peer) {
        return data().delete(PeerModel.class)
                .where(PeerModel.ID.eq(peer.getId()))
                .get().single().ignoreElement()
                .subscribeOn(subscriberScheduler())
                .observeOn(observerScheduler());
    }

    @Override
    public Completable deletePeer(UUID accountId, String address) {
        return data().delete(PeerModel.class)
                .where(PeerModel.ACCOUNT_ID.eq(accountId)
                        .and(PeerModel.ADDRESS.eq(address)))
                .get().single().ignoreElement()
                .subscribeOn(subscriberScheduler())
                .observeOn(observerScheduler());
    }
}
