package fi.nls.oskari.control.statistics.plugins.user;

import fi.nls.oskari.control.statistics.data.*;
import fi.nls.oskari.control.statistics.plugins.StatisticalDatasourcePlugin;
import fi.nls.oskari.control.statistics.plugins.db.StatisticalDatasource;
import fi.nls.oskari.domain.User;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.service.OskariComponentManager;
import fi.nls.oskari.service.ServiceRuntimeException;
import fi.nls.oskari.util.ConversionHelper;
import org.oskari.statistics.user.StatisticalIndicatorService;

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

public class UserIndicatorsStatisticalDatasourcePlugin extends StatisticalDatasourcePlugin {
    private final static Logger LOG = LogFactory.getLogger(UserIndicatorsStatisticalDatasourcePlugin.class);
    //private static UserIndicatorService userIndicatorService = new UserIndicatorServiceImpl();
    private StatisticalIndicatorService service;

    public UserIndicatorsStatisticalDatasourcePlugin() {
    }

    @Override
    public void init(StatisticalDatasource source) {
        super.init(source);
        service = OskariComponentManager.getComponentOfType(StatisticalIndicatorService.class);
    }

    @Override
    public IndicatorSet getIndicatorSet(User user) {
        IndicatorSet set = new IndicatorSet();
        if (user != null) {
            set.setIndicators(getIndicators(user));
            set.setComplete(true);
        }
        return set;
    }

    public void update() {
        // NO-OP - getIndicatorSet responds immediately
    }

    private List<StatisticalIndicator> getIndicators(User user) {
        // Getting the general information of all the indicator layers.
        if (user == null) {
            return new ArrayList<>();
        }
        long uid = user.getId();
        return service.findByUser(uid);
        /*
        List<UserIndicator> userIndicators = userIndicatorService.findAllOfUser(uid);
        List<StatisticalIndicator> indicators = new ArrayList<>();
        for (UserIndicator userIndicator : userIndicators) {
            indicators.add(toUserStatisticalIndicator(userIndicator));
        }
        return indicators;
        */
    }

    @Override
    public boolean canCache() {
        // Because the results are based on the user doing the query, we should not cache here.
        return false;
    }

    /**
     * Override as default impl expects indicators are stored in redis
     *
     * @param user
     * @param indicatorId
     * @return
     */
    @Override
    public StatisticalIndicator getIndicator(User user, String indicatorId) {
        try {
            return service.findById(ConversionHelper.getInt(indicatorId, -1), user.getId());
        } catch (ServiceRuntimeException ex) {
            LOG.warn("Indicator not found:", ex.getMessage());
        }
        return null;
    }

    @Override
    public Map<String, IndicatorValue> getIndicatorValues(StatisticalIndicator indicator,
                                                          StatisticalIndicatorDataModel params,
                                                          StatisticalIndicatorLayer regionset) {
        return service.getData(ConversionHelper.getInt(indicator.getId(), -1),
                regionset.getOskariLayerId(),
                ConversionHelper.getInt(params.getDimension("year").getValue(), -1));
    }
}

@@ -847,105 +847,8 @@
 s);%0A
-    //private static UserIndicatorService userIndicatorService = new UserIndicatorServiceImpl();%0A
     
@@ -892,16 +892,16 @@
 ervice;%0A
+
 %0A    pub
@@ -1147,32 +1147,441 @@
 .class);%0A    %7D%0A%0A
+    @Override%0A    public boolean canModify(User user) %7B%0A        return true;%0A    %7D%0A%0A    @Override%0A    public void saveIndicator(StatisticalIndicator indicator, User user) %7B%0A        // TODO: save indicator details%0A    %7D%0A    @Override%0A    public void saveIndicatorData(StatisticalIndicator indicator, long regionsetId, Map%3CString, IndicatorValue%3E data, User user) %7B%0A        // TODO: save indicator data%0A    %7D%0A%0A%0A
     @Override%0A  
@@ -2163,24 +2163,24 @@
 er.getId();%0A
+
         retu
@@ -2211,352 +2211,8 @@
 d);%0A
-        /*%0A        List%3CUserIndicator%3E userIndicators = userIndicatorService.findAllOfUser(uid);%0A        List%3CStatisticalIndicator%3E indicators = new ArrayList%3C%3E();%0A        for (UserIndicator userIndicator : userIndicators) %7B%0A            indicators.add(toUserStatisticalIndicator(userIndicator));%0A        %7D%0A        return indicators;%0A        */%0A
     

Add placeholders for override impl on saving functions