package io.quarkus.flyway.runtime;

import javax.sql.DataSource;

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.configuration.FluentConfiguration;

class FlywayCreator {

    private static final String[] EMPTY_ARRAY = new String[0];

    private final FlywayDataSourceRuntimeConfig flywayRuntimeConfig;
    private final FlywayDataSourceBuildTimeConfig flywayBuildTimeConfig;

    public FlywayCreator(FlywayDataSourceRuntimeConfig flywayRuntimeConfig,
            FlywayDataSourceBuildTimeConfig flywayBuildTimeConfig) {
        this.flywayRuntimeConfig = flywayRuntimeConfig;
        this.flywayBuildTimeConfig = flywayBuildTimeConfig;
    }

    public Flyway createFlyway(DataSource dataSource) {
        FluentConfiguration configure = Flyway.configure();
        configure.dataSource(dataSource);
        flywayRuntimeConfig.connectRetries.ifPresent(configure::connectRetries);
        flywayRuntimeConfig.schemas.ifPresent(list -> configure.schemas(list.toArray(EMPTY_ARRAY)));
        flywayRuntimeConfig.table.ifPresent(configure::table);
        configure.locations(flywayBuildTimeConfig.locations.toArray(EMPTY_ARRAY));
        flywayRuntimeConfig.sqlMigrationPrefix.ifPresent(configure::sqlMigrationPrefix);
        flywayRuntimeConfig.repeatableSqlMigrationPrefix.ifPresent(configure::repeatableSqlMigrationPrefix);
        configure.baselineOnMigrate(flywayRuntimeConfig.baselineOnMigrate);
        configure.validateOnMigrate(flywayRuntimeConfig.validateOnMigrate);
        configure.outOfOrder(flywayRuntimeConfig.outOfOrder);
        flywayRuntimeConfig.baselineVersion.ifPresent(configure::baselineVersion);
        flywayRuntimeConfig.baselineDescription.ifPresent(configure::baselineDescription);
        configure.placeholders(flywayRuntimeConfig.placeholders);
        return configure.load();
    }
}

@@ -813,32 +813,36 @@
 ource);%0A        
+if (
 flywayRuntimeCon
@@ -861,25 +861,25 @@
 etries.i
-f
+s
 Present(
 configur
@@ -874,44 +874,120 @@
 ent(
-configure::connectRetries);%0A        
+)) %7B%0A            configure.connectRetries(flywayRuntimeConfig.connectRetries.getAsInt());%0A        %7D%0A        if (
 flyw
@@ -1011,25 +1011,25 @@
 chemas.i
-f
+s
 Present(
 list -%3E 
@@ -1024,15 +1024,24 @@
 ent(
-list -%3E
+)) %7B%0A           
  con
@@ -1059,12 +1059,41 @@
 mas(
-list
+flywayRuntimeConfig.schemas.get()
 .toA
@@ -1110,17 +1110,16 @@
 _ARRAY))
-)
 ;%0A      
@@ -1112,32 +1112,46 @@
 RRAY));%0A        
+%7D%0A        if (
 flywayRuntimeCon
@@ -1161,25 +1161,25 @@
 .table.i
-f
+s
 Present(
 configur
@@ -1162,32 +1162,49 @@
 table.isPresent(
+)) %7B%0A            
 configure::table
@@ -1200,18 +1200,59 @@
 gure
-::
+.
 table
+(flywayRuntimeConfig.table.get()
 );%0A
+        %7D%0A
     
@@ -1330,32 +1330,36 @@
 RRAY));%0A        
+if (
 flywayRuntimeCon
@@ -1378,33 +1378,33 @@
 tionPrefix.i
-f
+s
 Present(
 configure::s
@@ -1391,27 +1391,82 @@
 Present(
-configure::
+)) %7B%0A            configure.sqlMigrationPrefix(flywayRuntimeConfig.
 sqlMigra
@@ -1467,32 +1467,38 @@
 lMigrationPrefix
+.get()
 );%0A        flywa
@@ -1484,32 +1484,46 @@
 get());%0A        
+%7D%0A        if (
 flywayRuntimeCon
@@ -1556,25 +1556,25 @@
 Prefix.i
-f
+s
 Present(
 configur
@@ -1565,27 +1565,92 @@
 Present(
-configure::
+)) %7B%0A            configure.repeatableSqlMigrationPrefix(flywayRuntimeConfig.
 repeatab
@@ -1669,18 +1669,34 @@
 onPrefix
-);
+.get());%0A        %7D
 %0A       
@@ -1902,32 +1902,36 @@
 Order);%0A        
+if (
 flywayRuntimeCon
@@ -1947,33 +1947,33 @@
 ineVersion.i
-f
+s
 Present(
 configure::b
@@ -1956,35 +1956,87 @@
 n.isPresent(
-configure::
+)) %7B%0A            configure.baselineVersion(flywayRuntimeConfig.
 baselineVers
@@ -2038,16 +2038,22 @@
 eVersion
+.get()
 );%0A     
@@ -2055,16 +2055,30 @@
         
+%7D%0A        if (
 flywayRu
@@ -2114,17 +2114,17 @@
 on.i
-f
+s
 Present(
 conf
@@ -2119,16 +2119,33 @@
 Present(
+)) %7B%0A            
 configur
@@ -2149,10 +2149,9 @@
 gure
-::
+.
 base
@@ -2165,18 +2165,74 @@
 cription
-);
+(flywayRuntimeConfig.baselineDescription.get());%0A        %7D
 %0A       

Remove lambdas from Flyway runtime code