package com.github.dreambrother.hackernews.service;

import com.github.dreambrother.hackernews.client.HackernewsClient;
import com.github.dreambrother.hackernews.dao.PublishedItemsDao;
import com.github.dreambrother.hackernews.dto.HackernewsItem;

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

public class HackernewsServiceImpl implements HackernewsService {

    private int minPopularity;
    private HackernewsClient hackernewsClient;
    private PublishedItemsDao publishedItemsDao;

    @Override
    public List<HackernewsItem> getMostPopularNonPublishedNews() {
        List<Long> publishedItems = publishedItemsDao.getLastPublishedIds();
        List<Long> newsIds = new ArrayList<>(hackernewsClient.fetchNewsIds());
        newsIds.removeAll(publishedItems);

        List<HackernewsItem> filtered = new ArrayList<>();
        for (Long id : newsIds) {
            HackernewsItem item = hackernewsClient.fetchNewsItem(id);
            if (item.getScore() >= minPopularity) {
                filtered.add(item);
            }
        }
        return filtered;
    }

    public void setPublishedItemsDao(PublishedItemsDao publishedItemsDao) {
        this.publishedItemsDao = publishedItemsDao;
    }

    public void setMinPopularity(int minPopularity) {
        this.minPopularity = minPopularity;
    }

    public void setHackernewsClient(HackernewsClient hackernewsClient) {
        this.hackernewsClient = hackernewsClient;
    }
}

@@ -666,19 +666,27 @@
 %3CLong%3E n
-ews
+onPoblished
 Ids = ne
@@ -742,19 +742,27 @@
        n
-ews
+onPoblished
 Ids.remo
@@ -814,24 +814,30 @@
 wsItem%3E 
-filtered
+mostPopularIds
  = new A
@@ -874,19 +874,27 @@
 g id : n
-ews
+onPoblished
 Ids) %7B%0A 
@@ -1030,24 +1030,30 @@
         
-filtered
+mostPopularIds
 .add(ite
@@ -1099,16 +1099,22 @@
 urn 
-filtered
+mostPopularIds
 ;%0A  

refactoring. rename vars