package org.netmelody.cieye.server.configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.netmelody.cieye.core.observation.KnownOffendersDirectory;
import org.netmelody.cieye.server.CiEyeServerInformationFetcher;
import org.netmelody.cieye.server.LandscapeFetcher;
import org.netmelody.cieye.server.PictureFetcher;

public final class ServerConfiguration {

    private final SettingsInitialiser settings = new SettingsInitialiser();
    
    private final ServerInformation information = new ServerInformation(settings.settingsLocation());
    private final RecordedKnownOffenders detective = new RecordedKnownOffenders(settings.picturesFile());
    private final RecordedObservationTargets targets = new RecordedObservationTargets(settings.viewsFile());
    private final Album album = new Album(settings.picturesDirectory());

    private static final class Refresher implements Runnable {
        private final Refreshable refreshable;
        public Refresher(Refreshable refreshable) {
            this.refreshable = refreshable;
        }
        @Override
        public void run() {
            refreshable.refresh();
        }
    }
    
    public ServerConfiguration() {
        final ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
        executor.scheduleWithFixedDelay(new Refresher(detective), 1L, 1L, TimeUnit.MINUTES);
        executor.scheduleWithFixedDelay(new Refresher(targets), 1L, 1L, TimeUnit.MINUTES);
    }
    
    public KnownOffendersDirectory detective() {
        return this.detective;
    }

    public LandscapeFetcher observationTargetDirectory() {
        return targets;
    }

    public CiEyeServerInformationFetcher information() {
        return information;
    }
    
    public PictureFetcher album() {
        return album;
    }
}

@@ -1430,32 +1430,33 @@
 etective), 1L, 1
+0
 L, TimeUnit.MINU
@@ -1443,38 +1443,38 @@
 , 10L, TimeUnit.
-MINUTE
+SECOND
 S);%0A        exec
@@ -1530,16 +1530,17 @@
 ), 1L, 1
+0
 L, TimeU
@@ -1547,14 +1547,14 @@
 nit.
-MINUTE
+SECOND
 S);%0A

check config file more often