package nl.idgis.publisher.admin;

import static nl.idgis.publisher.database.QDataSource.dataSource;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

import akka.actor.ActorRef;
import akka.actor.Props;

import nl.idgis.publisher.database.messages.DataSourceInfo;
import nl.idgis.publisher.database.messages.QDataSourceInfo;

import nl.idgis.publisher.domain.response.Page;
import nl.idgis.publisher.domain.web.DataSource;
import nl.idgis.publisher.domain.web.DataSourceStatusType;
import nl.idgis.publisher.domain.web.Status;

import nl.idgis.publisher.harvester.messages.GetActiveDataSources;

public class DataSourceAdmin extends AbstractAdmin {
	
	private ActorRef harvester;

	public DataSourceAdmin(ActorRef database, ActorRef harvester) {
		super(database); 
		
		this.harvester = harvester;
	}
	
	public static Props props(ActorRef database, ActorRef harvester) {
		return Props.create(DataSourceAdmin.class, database, harvester);
	}

	@Override
	protected void preStartAdmin() {
		addList(DataSource.class, this::handleListDataSources);
		addGet(DataSource.class, this::handleGetDataSource);
	}
	
	private CompletableFuture<Set<String>> activeDataSources() {
		return f.ask(harvester, new GetActiveDataSources(), Set.class).thenApply(Function.identity());
	}
	
	private DataSource toDataSource(DataSourceInfo dataSourceInfo, Set<String> activeDataSources) {
		String id = dataSourceInfo.getId();
		return new DataSource(
			id, 
			dataSourceInfo.getName(),
			new Status (activeDataSources.contains(id) 
				? DataSourceStatusType.OK
				: DataSourceStatusType.NOT_CONNECTED, new Timestamp (new Date ().getTime ())));
	}
	
	private CompletableFuture<Page<DataSource>> handleListDataSources () {
		return
			db.query().from(dataSource)
			.orderBy(dataSource.identification.asc())
			.list(new QDataSourceInfo(dataSource.identification, dataSource.name))
			.thenCompose(dataSourceInfos -> 
				activeDataSources().thenApply(activeDataSources -> {
					Page.Builder<DataSource> pageBuilder = new Page.Builder<>();
					
					dataSourceInfos.list().stream()
						.map(dataSourceInfo -> toDataSource(dataSourceInfo, activeDataSources))
						.forEach(pageBuilder::add);
					
					return pageBuilder.build ();
				}));
	}
	
	private CompletableFuture<Optional<DataSource>> handleGetDataSource (String dataSourceId) {
		 return activeDataSources().thenCompose(activeDataSources ->
			db.query().from(dataSource)
			.where(dataSource.identification.eq(dataSourceId))
			.singleResult(new QDataSourceInfo(dataSource.identification, dataSource.name))
			.thenApply(queryResult -> 
				queryResult.map(dataSourceInfo -> 
					toDataSource(dataSourceInfo, activeDataSources))));
	}

}

@@ -1270,32 +1270,65 @@
 ource);%0D%0A%09%7D%0D%0A%09%0D%0A
+%09@SuppressWarnings(%22unchecked%22)%0D%0A
 %09private Complet
@@ -1453,27 +1453,20 @@
 ply(
-Function.identity()
+resp -%3E resp
 );%0D%0A

make the compiler happy