package nxm.minio.intr;

import nxm.minio.lib.MinIOResource;
import nxm.sys.inc.ProvisionalUseOnly;
import nxm.sys.lib.Intrinsic;
import nxm.sys.lib.Shell;

/** Supports MinIO aliases, along with their:
 *  url, accessKey, and secretKey
 *
 *  @author Jeanette McCall
*/
@ProvisionalUseOnly("prototype")
public class Alias extends Intrinsic {

  /** default constructor */
  public Alias () {}

  @Override
  public int open () {
    // Get parameters
    String func  = MA.getU("FUNC");

    if ("CONFIG".equals(func)) {
      MinIOResource.loadAliases(MA.getCS("P1"));
    }
    else if ("SET".equals(func)) {
      String url = MA.getCS("P2");
      String alias = MA.getCS("P1",url);
      String accessKey = MA.getCS("P3");
      String secretKey = MA.getCS("P4");
      if (url.isEmpty() || accessKey.isEmpty()) {
        Shell.warning("ALIAS SET requires P2 (URL) and P3 (access key) be set");
      }
      else {
        MinIOResource.addAlias(alias, url, accessKey, secretKey);
      }
    }
    else if ("REMOVE".equals(func)) {
      String alias = MA.getCS("P1");
      MinIOResource.removeAlias(alias);
    }
    else if ("LIST".equals(func)) {       // The list function
      String alias = MA.getCS("P1");
      if (alias.isEmpty()) MinIOResource.listAliases();
      else MinIOResource.listAlias(alias);
    }
    else if ("IMPORT".equals(func)) {   // The import function
      String alias = MA.getCS("P1");
      if (!alias.isEmpty()) MinIOResource.importAlias(alias,MA.getCS("P2"));
      else M.warning("ALIAS IMPORT requires alais (P1) be specified");
    }
    else if ("EXPORT".equals(func)) {   // The export function
      String alias = MA.getCS("P1");
      M.info("ALIAS EXPORT is not currently supported");
    }
    else {
      M.warning("ALIAS Unsupported function: "+func);
    }
    return FINISH;
  }

}
