package cn.momia.mapi.api.index;

import cn.momia.api.operate.ConfigServiceApi;
import cn.momia.api.operate.dto.Banner;
import cn.momia.api.operate.dto.Event;
import cn.momia.api.operate.dto.Icon;
import cn.momia.common.webapp.config.Configuration;
import cn.momia.mapi.api.AbstractApi;
import org.springframework.beans.factory.annotation.Autowired;

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

public class AbstractIndexApi extends AbstractApi {
    @Autowired private ConfigServiceApi configServiceApi;

    protected List<Banner> getBanners(int cityId, int platform, String clientVersion) {
        List<Banner> banners = configServiceApi.listBanners(cityId);
        List<Banner> filteredBanners = new ArrayList<Banner>();
        for (Banner banner : banners) {
            if (banner.isInvalid(platform, clientVersion)) continue;
            banner.setCover(completeImg(banner.getCover()));
            banner.setAction(buildAction(banner.getAction(), platform));
            filteredBanners.add(banner);
        }

        int maxCount = Configuration.getInt("PageSize.Banner");
        return filteredBanners.size() > maxCount ? filteredBanners.subList(0, maxCount) : filteredBanners;
    }

    protected List<Icon> getIcons(int cityId, int platform, String clientVersion) {
        List<Icon> icons = configServiceApi.listIcons(cityId);
        List<Icon> filteredIcons = new ArrayList<Icon>();
        for (Icon icon : icons) {
            if (icon.isInvalid(platform, clientVersion)) continue;
            icon.setImg(completeImg(icon.getImg()));
            icon.setAction(buildAction(icon.getAction(), platform));
            filteredIcons.add(icon);
        }

        int maxCount= Configuration.getInt("PageSize.Icon");
        return filteredIcons.size() > maxCount ? filteredIcons.subList(0, maxCount) : filteredIcons;
    }

    protected List<Event> getEvents(int cityId, int platform, String clientVersion) {
        List<Event> events = configServiceApi.listEvents(cityId);
        List<Event> filteredEvents = new ArrayList<Event>();
        for (Event event : events) {
            if (event.isInvalid(platform, clientVersion)) continue;
            event.setImg(completeImg(event.getImg()));
            event.setAction(buildAction(event.getAction(), platform));
            filteredEvents.add(event);
        }

        int maxCount = Configuration.getInt("PageSize.Event");
        if (filteredEvents.size() > maxCount) filteredEvents = filteredEvents.subList(0, maxCount);

        return filteredEvents.size() % 2 != 0 ? filteredEvents.subList(0, filteredEvents.size() - 1) : filteredEvents;
    }
}

@@ -109,91 +109,14 @@
 dto.
-Banner;%0Aimport cn.momia.api.operate.dto.Event;%0Aimport cn.momia.api.operate.dto.Icon
+Config
 ;%0Aim
@@ -445,24 +445,31 @@
 tected List%3C
+Config.
 Banner%3E getB
@@ -530,32 +530,39 @@
  %7B%0A        List%3C
+Config.
 Banner%3E banners 
@@ -610,24 +610,31 @@
        List%3C
+Config.
 Banner%3E filt
@@ -661,16 +661,23 @@
 rayList%3C
+Config.
 Banner%3E(
@@ -692,16 +692,23 @@
    for (
+Config.
 Banner b
@@ -1174,24 +1174,31 @@
 tected List%3C
+Config.
 Icon%3E getIco
@@ -1259,24 +1259,31 @@
        List%3C
+Config.
 Icon%3E icons 
@@ -1329,24 +1329,31 @@
        List%3C
+Config.
 Icon%3E filter
@@ -1376,16 +1376,23 @@
 rayList%3C
+Config.
 Icon%3E();
@@ -1405,16 +1405,23 @@
    for (
+Config.
 Icon ico
@@ -1854,24 +1854,31 @@
 tected List%3C
+Config.
 Event%3E getEv
@@ -1941,24 +1941,31 @@
        List%3C
+Config.
 Event%3E event
@@ -2014,24 +2014,31 @@
        List%3C
+Config.
 Event%3E filte
@@ -2063,16 +2063,23 @@
 rayList%3C
+Config.
 Event%3E()
@@ -2093,16 +2093,23 @@
    for (
+Config.
 Event ev

refactor operator