package nxm.sys.test;

import nxm.sys.lib.FileName;
import nxm.sys.lib.IOResource;
import nxm.sys.lib.Shell;

/** Gets the current location of the DAT directory in the SYS option tree
 *
 *  @author Jeanette McCall
 *  @since  NeXtMidas 4.0.0
 */
public class DatLocator {

  private static boolean sysDatFull = false;
  private static boolean sysTestDatExists = false;
  private static final String DAT_SYS_DIR = "nxm.sys.dat";
  private static final String DAT_TEST_DIR = "nxm.sys.test.dat";
  private static final String DAT_SYS_DIR_SLASH = DAT_SYS_DIR.replace(".", "/");
  private static final String DAT_TEST_DIR_SLASH = DAT_TEST_DIR.replace(".", "/");


  static {
    FileName dirSysDatName = new FileName(DAT_SYS_DIR);
    FileName dirSysTestDatName = new FileName(DAT_TEST_DIR);
    // Checking apenny.prm since it gets moved when using nxm.sys.test.dat
    IOResource dirSysDatRes = IOResource.getInstance(Shell.getMidasContext(), IOResource.UNKNOWN, dirSysDatName.getPath()+"dat/"+"apenny.prm", 0, null);
    IOResource dirSysTestDatRes = IOResource.getInstance(Shell.getMidasContext(), IOResource.UNKNOWN, dirSysTestDatName.getPath()+"dat/"+"apenny.prm", 0, null);
    sysDatFull = dirSysDatRes.exists();
    sysTestDatExists = dirSysTestDatRes.exists();
  }

  /** prevent instantiation */
  private DatLocator () {}

  /** Does nxm/sys/dat exist?
   *  @return Whether or not nxm/sys/dat exists
   */
  public static boolean sysDatFull () {
    return sysDatFull;
  }

  /** Does nxm/sys/test/dat exist?
   *  @return Whether or not nxm/sys/test/dat exists
   */
  public static boolean sysTestDatExists () {
    return sysTestDatExists;
  }

  /** gets dot separated path for main dat directory<br>
   *  this DOES NOT include a post pended dot<br>
   *
   *  @return nxm.sys.dat or nxm.sys.test.dat
   */
  public static String getDatDirLocation () {
    if (sysDatFull) {
      return DAT_SYS_DIR;
    }
    else if (sysTestDatExists) {
      return DAT_TEST_DIR;
    }
    else {
      return "";
    }
  }

  /** gets forward slash separated path for main dat directory<br>
   *  this DOES NOT include a post pended SLASH<br>
   *
   *  @return full dat directory path including final forward slash
   */
  public static String getDatDirSlashLocation () {
    if (sysDatFull) {
      return DAT_SYS_DIR_SLASH;
    }
    else if (sysTestDatExists) {
      return DAT_TEST_DIR_SLASH;
    }
    else {
      return "";
    }
  }

}