package com.p3.bewoplaner.data.repository;

import com.p3.bewoplaner.data.repository.mapper.Mapper;
import com.p3.bewoplaner.data.repository.models.GroupRealm;
import com.p3.bewoplaner.data.repository.models.MessageRealm;
import com.p3.bewoplaner.data.repository.models.UserRealm;
import com.p3.bewoplaner.di.scopes.ApplicationScope;
import com.p3.bewoplaner.model.Group;
import com.p3.bewoplaner.model.Message;
import com.p3.bewoplaner.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);
    }

}
