package org.mercury_im.messenger.usecase;

import org.jivesoftware.smack.roster.Roster;
import org.mercury_im.messenger.data.repository.AccountRepository;
import org.mercury_im.messenger.data.repository.PeerRepository;
import org.mercury_im.messenger.entity.Account;
import org.mercury_im.messenger.store.MercuryRosterStore;
import org.mercury_im.messenger.xmpp.MercuryConnection;

import java.util.UUID;

import javax.inject.Inject;

public class RosterStoreBinder {

    private final AccountRepository accountRepository;
    private final PeerRepository peerRepository;

    @Inject
    public RosterStoreBinder(AccountRepository accountRepository, PeerRepository peerRepository) {
        this.accountRepository = accountRepository;
        this.peerRepository = peerRepository;
    }

    public void setRosterStoreOn(MercuryConnection connection) {
        MercuryRosterStore store =
                createRosterStore(connection.getAccount().getId(), accountRepository, peerRepository);
        Roster roster = Roster.getInstanceFor(connection.getConnection());
        roster.setRosterStore(store);
    }

    private MercuryRosterStore createRosterStore(UUID accountId, AccountRepository accountRepository, PeerRepository peerRepository) {
        return new MercuryRosterStore(accountId, peerRepository, accountRepository);
    }
}
