package de.otto.flummi.extensions;

import de.otto.flummi.IndicesAdminClient;

import java.util.Comparator;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;

import static java.util.stream.Collectors.toSet;

public class RollingIndexBehavior {

    private final IndicesAdminClient client;
    private final String aliasName;
    private final String indexPrefixName;
    private final int survivor;

    public RollingIndexBehavior(IndicesAdminClient client, String aliasName, String indexPrefixName, int survivor) {
        this.client = client;
        this.aliasName = aliasName;
        this.indexPrefixName = indexPrefixName;
        this.survivor = survivor;
    }

    public String createNewIndex() {
        String indexName = newIndexName();
        client.prepareCreate(indexName).execute();
        return indexName;
    }

    public void abort(String newIndexName) {
        client.prepareDelete(newIndexName).execute();
    }

    public Set<String> commit(String name) {
        client.pointAliasToCurrentIndex(aliasName, name);
        return this.deleteOldIndices(aliasName, indexPrefixName, survivor);
    }

    private String newIndexName() {
        return indexPrefixName+"_"+System.currentTimeMillis();
    }

    Set<String> deleteOldIndices(String alias, String prefix, int survivor) {
        if(survivor < 1) {
            throw new IllegalArgumentException("must have one survivor");
        }

        Optional<String> aliasToIndex = client.getIndexNameForAlias(alias);
        Set<String> names =
                client.getAllIndexNames()
                        .stream()
                        .filter(startsWith(prefix))
                        .sorted(Comparator.reverseOrder()) // TODO: here we should have Index objects and sort by created date value
                        .skip(survivor)
                        .filter(skipAlias(aliasToIndex))
                        .collect(toSet());
        client.prepareDelete(names.stream()).execute();
        return names;
    }

    private static Predicate<? super String> startsWith(String prefix) {
        return (s) -> s.startsWith(prefix);
    }

    private static Predicate<String> skipAlias(Optional<String> indexName) {
        return (s) -> !(indexName.isPresent() && s.equals(indexName.get()));
    }
}

@@ -150,16 +150,52 @@
 il.Set;%0A
+import java.util.function.Function;%0A
 import j
@@ -465,16 +465,78 @@
 urvivor;
+%0A    private final Function%3CString, String%3E indexNameFunction;
 %0A%0A    pu
@@ -634,32 +634,76 @@
 me, int survivor
+, Function%3CString, String%3E indexNameFunction
 ) %7B%0A        this
@@ -834,24 +834,301 @@
 = survivor;%0A
+        this.indexNameFunction = indexNameFunction;%0A    %7D%0A%0A    public RollingIndexBehavior(IndicesAdminClient client, String aliasName, String indexPrefixName, int survivor) %7B%0A        this(client, aliasName, indexPrefixName, survivor, (s) -%3E s+%22_%22+System.currentTimeMillis());%0A
     %7D%0A%0A    p
@@ -1638,48 +1638,42 @@
 ndex
-PrefixName+%22_%22+System.currentTimeMillis(
+NameFunction.apply(indexPrefixName
 );%0A 

add index name function to RollingIndexBehavior