/*
 * Copyright (c) 2011-2019 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.
 * Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG.
 */
package org.terracotta.dynamic_config.cli.command;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import org.slf4j.LoggerFactory;
import org.terracotta.common.struct.Measure;
import org.terracotta.common.struct.TimeUnit;
import org.terracotta.dynamic_config.cli.converter.TimeUnitConverter;

import java.util.stream.Stream;

@Parameters(commandNames = MainCommand.NAME)
public class MainCommand extends Command {
  public static final String NAME = "main";

  @Parameter(names = {"-v", "--verbose"}, description = "Verbose mode. Default: false")
  private boolean verbose = false;

  @Parameter(names = {"-r", "--request-timeout"}, description = "Request timeout. Default: 10s", converter = TimeUnitConverter.class)
  private Measure<TimeUnit> requestTimeout = Measure.of(10, TimeUnit.SECONDS);

  @Parameter(names = {"-t", "--connection-timeout"}, description = "Connection timeout. Default: 30s", converter = TimeUnitConverter.class)
  private Measure<TimeUnit> connectionTimeout = Measure.of(30, TimeUnit.SECONDS);

  @Parameter(names = {"-srd", "--security-root-directory"}, description = "Security root directory")
  private String securityRootDirectory;

  @Override
  public void run() {
    if (verbose) {
      Logger rootLogger = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
      rootLogger.setLevel(Level.INFO);
      Appender<ILoggingEvent> detailAppender = rootLogger.getAppender("STDOUT-DETAIL");

      Stream.of(
          "org.terracotta.dynamic_config",
          "org.terracotta.nomad",
          "org.terracotta.migration",
          "org.terracotta.persistence.sanskrit"
      ).forEach(name -> {
        Logger logger = (Logger) LoggerFactory.getLogger(name);
        logger.setLevel(Level.TRACE);
        //Detach the STDOUT appender which logs in a minimal pattern and attached STDOUT-DETAIL appender
        logger.detachAppender("STDOUT");
        logger.addAppender(detailAppender);
      });
    }
  }

  public Measure<TimeUnit> getRequestTimeout() {
    return requestTimeout;
  }

  public Measure<TimeUnit> getConnectionTimeout() {
    return connectionTimeout;
  }

  public String getSecurityRootDirectory() {
    return securityRootDirectory;
  }
}

@@ -2082,17 +2082,28 @@
 tta.
-migration
+persistence.sanskrit
 %22,%0A 
@@ -2127,36 +2127,26 @@
 racotta.
-persistence.sanskrit
+diagnostic
 %22%0A      

Fix packages for CLI verbose mode