/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * license agreements; and to You under the Apache License, version 2.0:
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * This file is part of the Apache Pekko project, which was derived from Akka.
 */

/*
 * Copyright (C) since 2016 Lightbend Inc. <https://www.lightbend.com>
 */

package docs.javadsl;

import java.util.Date;
import java.util.List;
import org.apache.pekko.NotUsed;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.stream.connectors.geode.GeodeSettings;
import org.apache.pekko.stream.connectors.geode.RegionSettings;
import org.apache.pekko.stream.connectors.geode.javadsl.Geode;
import org.apache.pekko.stream.connectors.geode.javadsl.GeodeWithPoolSubscription;
import org.apache.pekko.stream.connectors.testkit.javadsl.LogCapturingExtension;
import org.apache.pekko.stream.javadsl.Source;
import org.apache.pekko.testkit.javadsl.TestKit;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ExtendWith(LogCapturingExtension.class)
public class GeodeBaseTestCase {

  protected static final Logger LOGGER = LoggerFactory.getLogger(GeodeFlowTestCase.class);

  protected static ActorSystem system;
  private String geodeDockerHostname = "localhost";

  {
    String geodeItHostname = System.getenv("IT_GEODE_HOSTNAME");
    if (geodeItHostname != null) geodeDockerHostname = geodeItHostname;
  }

  // #region
  protected final RegionSettings<Integer, Person> personRegionSettings =
      RegionSettings.create("persons", Person::id);
  protected final RegionSettings<Integer, Animal> animalRegionSettings =
      RegionSettings.create("animals", Animal::id);

  // #region

  @BeforeAll
  public static void setup() {
    system = ActorSystem.create();
  }

  static Source<Person, NotUsed> buildPersonsSource(Integer... ids) {
    return Source.from(List.of(ids))
        .map((i) -> new Person(i, "Person Java %d".formatted(i), new Date()));
  }

  static Source<Animal, NotUsed> buildAnimalsSource(Integer... ids) {
    return Source.from(List.of(ids)).map((i) -> new Animal(i, "Animal Java %d".formatted(i), 1));
  }

  protected Geode createGeodeClient() {
    String hostname = this.geodeDockerHostname;
    // #connection
    GeodeSettings settings =
        GeodeSettings.create(hostname, 10334).withConfiguration(c -> c.setPoolIdleTimeout(10));
    Geode geode = new Geode(settings);
    system.registerOnTermination(() -> geode.close());
    // #connection
    return geode;
  }

  protected GeodeWithPoolSubscription createGeodeWithPoolSubscription() {
    GeodeSettings settings = GeodeSettings.create(geodeDockerHostname, 10334);
    // #connection-with-pool
    GeodeWithPoolSubscription geode = new GeodeWithPoolSubscription(settings);
    // #connection-with-pool
    return geode;
  }

  @AfterAll
  public static void teardown() {
    TestKit.shutdownActorSystem(system);
  }
}
