package de.beyondsoft.ownchat.data.repository;

import de.beyondsoft.ownchat.data.repository.mapper.Mapper;
import de.beyondsoft.ownchat.data.repository.models.GroupRealm;
import de.beyondsoft.ownchat.data.repository.models.MessageRealm;
import de.beyondsoft.ownchat.data.repository.models.UserRealm;
import de.beyondsoft.ownchat.di.scopes.ApplicationScope;
import de.beyondsoft.ownchat.model.Group;
import de.beyondsoft.ownchat.model.Message;
import de.beyondsoft.ownchat.model.User;

import dagger.Module;
import dagger.Provides;
import io.realm.RealmConfiguration;

/**
 * Created by Rares Teodorescu on 11/01/2017.
 */
@Module
public class RepositoryModule {

    @Provides
    @ApplicationScope
    RealmConfiguration provideRealmConfiguration() {
        return new RealmConfiguration.Builder()
                .deleteRealmIfMigrationNeeded()
                .build();
    }

    @Provides
    @ApplicationScope
    RealmProvider provideRealm(RealmConfiguration realmConfiguration) {
        return new RealmProvider(realmConfiguration);
    }

    @Provides
    @ApplicationScope
    Mapper<Group, GroupRealm> provideToGroupRealmMapper() {
        return GroupRealm::createFromGroup;
    }

    @Provides
    @ApplicationScope
    Mapper<GroupRealm, Group> provideToGroupMapper() {
        return Group::createFromGroupRealm;
    }

    @Provides
    @ApplicationScope
    GroupRealmRepository provideGroupRealmRepository(RealmProvider realmProvider,
                                                     Mapper<Group, GroupRealm> toRealmMapper,
                                                     Mapper<GroupRealm, Group> toGroupMapper) {
        return new GroupRealmRepository(realmProvider, toRealmMapper, toGroupMapper);
    }

    @Provides
    @ApplicationScope
    Mapper<User, UserRealm> provideToUserRealmMapper() {
        return UserRealm::createFromUser;
    }

    @Provides
    @ApplicationScope
    Mapper<UserRealm, User> provideToUserMapper() {
        return User::createFromUserRealm;
    }

    @Provides
    @ApplicationScope
    UserRealmRepository provideUserRealmRepository(RealmProvider realmProvider,
                                                   Mapper<User, UserRealm> toRealmMapper,
                                                   Mapper<UserRealm, User> toUserMapper) {
        return new UserRealmRepository(realmProvider, toRealmMapper, toUserMapper);
    }

    @Provides
    @ApplicationScope
    Mapper<Message, MessageRealm> provideToMessageRealmMapper() {
        return MessageRealm::createFromMessage;
    }

    @Provides
    @ApplicationScope
    Mapper<MessageRealm, Message> provideToMessageMapper() {
        return Message::createFromMessageRealm;
    }

    @Provides
    @ApplicationScope
    MessageRealmRepository provideMessageRealmRepository(RealmProvider realmProvider,
                                                         Mapper<Message, MessageRealm> toRealmMapper,
                                                         Mapper<MessageRealm, Message> toUserMapper) {
        return new MessageRealmRepository(realmProvider, toRealmMapper, toUserMapper);
    }

}
