// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.finapp;

import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;

final class DatabaseModule extends AbstractModule {

  @Override
  protected void configure() {}

  @Provides
  @Singleton
  SpannerDaoInterface provideSpannerDao(
      @ArgsModule.SpannerHost String spannerHost,
      @ArgsModule.SpannerPort int spannerPort,
      @ArgsModule.SpannerProjectId String spannerProjectId,
      @ArgsModule.SpannerInstanceId String spannerInstanceId,
      @ArgsModule.SpannerDatabaseId String spannerDatabaseId,
      @ArgsModule.SpannerUseJdbc boolean spannerUseJdbc) {
    if (spannerUseJdbc) {
      return new SpannerDaoJDBCImpl(spannerProjectId, spannerInstanceId, spannerDatabaseId);
    } else {
      SpannerOptions spannerOptions = SpannerOptions.getDefaultInstance();
      Spanner spanner = spannerOptions.toBuilder().build().getService();
      DatabaseClient client =
          spanner.getDatabaseClient(
              DatabaseId.of(spannerProjectId, spannerInstanceId, spannerDatabaseId));
      return new SpannerDaoImpl(client);
    }
  }
}

@@ -609,16 +609,130 @@
 inapp;%0A%0A
+import com.google.api.gax.grpc.GrpcTransportChannel;%0Aimport com.google.api.gax.rpc.FixedTransportChannelProvider;%0A
 import c
@@ -771,16 +771,16 @@
 Client;%0A
-
 import c
@@ -1015,16 +1015,54 @@
 ngleton;
+%0Aimport io.grpc.ManagedChannelBuilder;
 %0A%0Afinal 
@@ -1789,16 +1789,26 @@
 panner =
+%0A         
  spanner
@@ -1830,16 +1830,453 @@
 er()
-.build()
+%0A              .setChannelProvider(%0A                  // Configure GRPC channel explicitly, to simplify deployment on GKE. The default%0A                  // configuration requires a grpclb to be available.%0A                  FixedTransportChannelProvider.create(%0A                      GrpcTransportChannel.create(%0A                          ManagedChannelBuilder.forAddress(spannerHost, spannerPort).build())))%0A              .build()%0A              
 .get

Revert #3 (#40)