package org.mercury_im.messenger.data.repository;

import org.jivesoftware.smackx.ikey.record.IkeyRecord;
import org.jivesoftware.smackx.ox.store.definition.OpenPgpTrustStore;
import org.jxmpp.jid.EntityBareJid;
import org.mercury_im.messenger.core.data.repository.IkeyRecordRepository;
import org.mercury_im.messenger.core.data.repository.OpenPgpTrustRepository;
import org.pgpainless.key.OpenPgpV4Fingerprint;

import java.util.UUID;
import java.util.logging.Logger;

import javax.inject.Inject;

import io.reactivex.Completable;
import io.reactivex.Single;

public class RxIkeyTrustRepository implements OpenPgpTrustRepository {

    private static final Logger LOGGER = Logger.getLogger(RxIkeyTrustRepository.class.getName());

    private final IkeyRecordRepository recordRepository;

    @Inject
    public RxIkeyTrustRepository(IkeyRecordRepository ikeyRecordRepository) {
        this.recordRepository = ikeyRecordRepository;
    }

    @Override
    public Single<OpenPgpTrustStore.Trust> loadTrust(UUID accountId, EntityBareJid owner, OpenPgpV4Fingerprint fingerprint) {
        return recordRepository.loadRecord(accountId, owner)
                .filter(r -> r.hasSubordinate(fingerprint))
                .map(IkeyRecord::getTrust)
                .first(OpenPgpTrustStore.Trust.undecided);
    }

    @Override
    public Completable storeTrust(UUID accountId, EntityBareJid owner, OpenPgpV4Fingerprint fingerprint, OpenPgpTrustStore.Trust trust) {
        return Completable.complete();
    }

}
