package ejbclient85;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.jboss.as.quickstarts.ejb.multi.server.app.AppOne;
import org.jboss.as.quickstarts.ejb.multi.server.app.AppTwo;

public class OneServerJBossApiClient {

  private static final Logger LOGGER = Logger.getLogger(OneServerJBossApiClient.class.getName());
  final String user;
  final String password;
  final String port;
  final InitialContext context;
  final Context woContext;

  public OneServerJBossApiClient(String user, String password, String port, Boolean debug) throws NamingException {
    this.user = user;
    this.password = password;
    this.port = port;

    Level l = debug == null ? Level.SEVERE : debug.booleanValue() ? Level.ALL : Level.INFO;
    Logger.getLogger("").setLevel(l);
    Logger.getLogger("").getHandlers()[0].setLevel(Level.ALL);
    Logger.getLogger(OneServerJBossApiClient.class.getPackage().getName()).setLevel(
        debug != null ? Level.FINEST : Level.INFO);

    Properties p = new Properties();
    // p.put("endpoint.name", "nonClustered-client-endpoint");
    p.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
    // p.put("deployment.node.selector", SimpleLoadFactorNodeSelector.class.getName());
    p.put("remote.connections", "appOneA");
    p.put("remote.connection.appOneA.port", this.port);
    p.put("remote.connection.appOneA.host", "localhost");
    p.put("remote.connection.appOneA.username", this.user);
    p.put("remote.connection.appOneA.password", this.password);
    p.put("remote.connection.appOneA.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT", "false");
    p.put("org.jboss.ejb.client.scoped.context", "true");

    // EJBClientConfiguration cc = new PropertiesBasedEJBClientConfiguration(p);
    // ContextSelector<EJBClientContext> selector = new ConfigBasedEJBClientContextSelector(cc);
    // EJBClientContext.setSelector(selector);

    p.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    LOGGER.info("PARAMS : " + p);
    this.context = new InitialContext(p);
    this.woContext = null; //(Context) context.lookup("ejb:");
  }

  private void close() throws NamingException {
//    this.woContext.close();
    this.context.close();
  }

  /**
   * Invoke with EJB Client API.
   */
  private void invokeApp1WithEJBClientAPI() throws NamingException {
    final String rcal = "ejb:appone/ejb//AppOneBean!" + AppOne.class.getName();
    final AppOne remote = (AppOne) context.lookup(rcal);
    LOGGER.info("Calling EJB...");
    final String result = remote.invoke("TEST");
    LOGGER.info("The EJB call returns : " + result);
  }

  private void invokeApp2WithEJBClientAPI() throws NamingException {
    final String rcal = "ejb:apptwo/ejb//AppTwoBean!" + AppTwo.class.getName();
    final AppTwo remote = (AppTwo) context.lookup(rcal);
    LOGGER.info("Calling EJB...");
    final String result = remote.invoke("TEST");
    LOGGER.info("The EJB call returns : " + result);
  }

  /**
   * Main method.
   * 
   * @throws InterruptedException
   */
  public static void main(String[] args) throws NamingException, InterruptedException {
    Boolean debug = null;

    if (args.length > 0 && "-D".equals(args[0])) {
      debug = Boolean.TRUE;
    } else if (args.length > 0 && "-d".equals(args[0])) {
      debug = Boolean.FALSE;
    }

    OneServerJBossApiClient client2 = new OneServerJBossApiClient("quickuser2", "quick+123", "4447", debug);
    client2.invokeApp2WithEJBClientAPI();
    client2.invokeApp1WithEJBClientAPI();

    client2.close();

    OneServerJBossApiClient client1 = new OneServerJBossApiClient("quickuser1", "quick123+", "4447", debug);
    client1.invokeApp1WithEJBClientAPI();
    client1.close();
  }
}