/*
 * Copyright (C) 2020 Grakn Labs
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

package grakn.test.behaviour.connection.database;

import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import static grakn.common.util.Collections.list;
import static grakn.common.util.Collections.set;
import static grakn.test.behaviour.connection.ConnectionSteps.THREAD_POOL_SIZE;
import static grakn.test.behaviour.connection.ConnectionSteps.grakn;
import static grakn.test.behaviour.connection.ConnectionSteps.threadPool;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class DatabaseSteps {

    @When("connection create database: {word}")
    public void connection_create_database(String name) {
        connection_create_databases(list(name));
    }

    @When("connection create database(s):")
    public void connection_create_databases(List<String> names) {
        for (String name : names) {
            grakn.databases().create(name);
        }
    }

    @When("connection create databases in parallel:")
    public void connection_create_databases_in_parallel(List<String> names) {
        assertTrue(THREAD_POOL_SIZE >= names.size());

        CompletableFuture[] creations = new CompletableFuture[names.size()];
        int i = 0;
        for (String name : names) {
            creations[i++] = CompletableFuture.supplyAsync(() -> grakn.databases().create(name), threadPool);
        }

        CompletableFuture.allOf(creations).join();
    }

    @When("connection delete database(s):")
    public void connection_delete_databases(List<String> names) {
        for (String databaseName : names) {
            grakn.databases().get(databaseName).delete();
        }
    }

    @When("connection delete databases in parallel:")
    public void connection_delete_databases_in_parallel(List<String> names) {
        assertTrue(THREAD_POOL_SIZE >= names.size());

        CompletableFuture[] deletions = new CompletableFuture[names.size()];
        int i = 0;
        for (String name : names) {
            deletions[i++] = CompletableFuture.supplyAsync(
                    () -> {
                        grakn.databases().get(name).delete();
                        return null;
                    },
                    threadPool
            );
        }

        CompletableFuture.allOf(deletions).join();
    }

    @Then("connection has database(s):")
    public void connection_has_databases(List<String> names) {
        assertEquals(set(names),
                     grakn.databases().getAll().stream()
                             .map(database -> database.name())
                             .collect(Collectors.toSet()));
    }

    @Then("connection does not have database(s):")
    public void connection_does_not_have_databases(List<String> names) {
        for (String name : names) {
            assertNull(grakn.databases().get(name));
        }
    }
}

@@ -1446,23 +1446,23 @@
  create 
-databas
+keyspac
 e: %7Bword
@@ -1608,23 +1608,23 @@
  create 
-databas
+keyspac
 e(s):%22)%0A
@@ -1815,23 +1815,23 @@
  create 
-databas
+keyspac
 es in pa
@@ -2313,23 +2313,23 @@
  delete 
-databas
+keyspac
 e(s):%22)%0A
@@ -2542,23 +2542,23 @@
  delete 
-databas
+keyspac
 es in pa
@@ -3183,23 +3183,23 @@
 ion has 
-databas
+keyspac
 e(s):%22)%0A
@@ -3517,23 +3517,23 @@
 ot have 
-databas
+keyspac
 e(s):%22)%0A

Revert keyspace steps syntax