package tv.pluto.utils;

import com.plutotv.test.lib.LogUtils;
import com.plutotv.test.lib.security.CryptoSecrets;
import lombok.experimental.UtilityClass;

@UtilityClass
public class Route {
  private static String environment;

  public static String getEnvironment() {
    if (environment.isEmpty()) {
      LogUtils.logSevereMessageThenFail("Environment is not set. Ending Test.");
    }
    return environment;
  }

  public static void setEnvironment(String appUrl) {
    environment = getEnvFromAppUrl(appUrl);
    LogUtils.logInfoMessage("Setting environment to " + environment);
  }

  private static String getEnvFromAppUrl(String appUrl) {
    CustomCoreEnvironmentUtils customCoreEnvironmentUtils = new CustomCoreEnvironmentUtils();

    return customCoreEnvironmentUtils.getCustomEnvironment(appUrl);
  }

  private static String getGrpcServerUrl() {
    return PropertiesHolder.getProperty(getEnvironment() + "_GRPC_SERVER_URL");
  }

  private static String getGrpcPort() {
    return PropertiesHolder.getProperty(getEnvironment() + "_GRPC_PORT");
  }

  public static String getGrpcServer() {
    return getGrpcServerUrl() + ":" + getGrpcPort();
  }

  public static String getDecodedAuthToken() {
    return CryptoSecrets.decryptString(
        PropertiesHolder.getProperty(getEnvironment() + "_AUTH_TOKEN"));
  }

  public static boolean isNonProdEnvironment() {
    return getEnvironment().equalsIgnoreCase("NONPROD");
  }
}
