package rss.feed.reader.db.local;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.realm.RealmList;
import io.realm.RealmResults;
import rss.feed.reader.api.model.Channel;
import rss.feed.reader.api.model.News;
import rss.feed.reader.db.local.models.ChannelObject;
import rss.feed.reader.db.local.models.NewsObject;
import rss.feed.reader.db.local.models.StringObject;

/**
 * Utility class that used to convert Realm-object to data object and vise-versa.
 * <p>
 * Created by Orest Guziy on 22.01.17.
 */

public final class Converter {

    private Converter() {
    }

    public static ChannelObject createFrom(Channel channel) {
        return new ChannelObject(channel.getId(), channel.getTitle(), channel.getUrl(), channel.getPosition());
    }

    public static List<ChannelObject> createFrom(List<Channel> channels) {
        List<ChannelObject> items = new ArrayList<>();

        for (Channel channel : channels) {
            items.add(createFrom(channel));
        }

        return items;
    }

    public static List<Channel> buildFrom(RealmResults<ChannelObject> results) {
        List<Channel> data = new ArrayList<>();

        for (ChannelObject item : results) {
            data.add(buildFrom(item));
        }

        return data;
    }

    private static Channel buildFrom(ChannelObject item) {
        Channel channel = new Channel();

        channel.setId(item.getId());
        channel.setTitle(item.getTitle());
        channel.setUrl(item.getUrl());
        channel.setPosition(item.getPosition());

        return channel;
    }

    private static NewsObject createFrom(String channelUrl, News newsItem) {
        return new NewsObject(channelUrl, newsItem);
    }

    public static List<NewsObject> createFrom(String channelUrl, List<News> news) {
        List<NewsObject> newsObjects = new ArrayList<>();

        for (News newsItem : news) {
            newsObjects.add(createFrom(channelUrl, newsItem));
        }

        return newsObjects;
    }

    public static List<News> buildNewsFrom(RealmResults<NewsObject> results) {
        List<News> data = new ArrayList<>();

        for (NewsObject item : results) {
            data.add(buildNewsItemFrom(item));
        }

        return data;
    }

    private static News buildNewsItemFrom(NewsObject newsObject) {
        return new News(newsObject.getNewsUrl(),
                newsObject.getLogoUrl(),
                newsObject.getTitle(),
                newsObject.getCategories(),
                newsObject.getDate(),
                newsObject.getDescription());
    }

    public static RealmList<StringObject> createListFrom(List<String> data) {
        if (data == null || data.isEmpty()) {
            return new RealmList<>();
        }

        RealmList<StringObject> list = new RealmList<>();
        for (String value : data) {
            list.add(new StringObject(value));
        }

        return list;
    }

    public static List<String> buildListFrom(RealmList<StringObject> data) {
        if (data == null || data.isEmpty()) {
            return Collections.emptyList();
        }

        List<String> list = new ArrayList<>();
        for (StringObject entry : data) {
            list.add(entry.value);
        }

        return list;
    }
}
