package nxm.sys.test;

import java.io.File;
import java.io.IOException;

import nxm.sys.lib.FileUtil;
import nxm.sys.lib.Shell;
import nxm.sys.lib.Time;

/**
 * This class provides the necessary functionality to make the test_plot_layerkml
 * test perform refreshes. When the startSwapping method is called, the code will swap
 * the files every 5 seconds to simulate a changed file for the kml test.
 * @author Kevin E.
 * @since 3.9.2
 *
 */
public class LayerKmlTestHelper {

  private volatile static boolean isRunning;
  private static String baseDir = Shell.isRunningFromJarFile() ?
      Shell.getAuxPath("WRITE") : Shell.getNmRoot()+"nxm/sys/test/";
  static long swapCount = 0;
  static final String mainFile = baseDir+"kmlTest.kml";
  static final String newFile  = baseDir+"kmlTestLeft.kml";
  static final String origFile = baseDir+"kmlTestRight.kml";
  static File fileOrig = new File(origFile);
  static File fileNew  = new File(newFile);
  static File fileMain = new File(mainFile);

  /** This constructor was automatically provided by Java,
   *  but it is unneeded as all the methods are static methods.
   *  @deprecated now that Javadocs are required, it is temporarily
   *              being explicitely provided but deprecated
   */
  @Deprecated
  public LayerKmlTestHelper () {}

  private static Thread getThread () {
    Thread t = new Thread(new Runnable() {

      @Override
      public void run () {

        while (isRunning) {
          if ((swapCount % 2) == 0) {
            swapFile(fileOrig, fileMain);
          }
          else {
            swapFile(fileNew, fileMain);
          }
          int count = 0;
          // This while loop is technically a sleep but it gives the user a chance to close the thread while the sleep is happening.
          while(isRunning && count < 50) {
            Time.sleep(.1);
            count++;
          }
          swapCount++;
        }
        close();
      }
    });
    return t;
  }

  /**
   * Start swapping files (should see the google logo move back and forth.)
   */
  public static void startSwapping () {
    if (isRunning) return;  // This is static, we may be in a state where the thread was never stopped
    isRunning=true;
    Thread t = getThread();
    t.start();
  }

  private static void swapFile (File src, File dst) {
    try {
      FileUtil.copy(src, dst);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  /**
   * Put the original file back.
   */
  public static void close () {
    swapFile(fileOrig, fileMain);
  }

  /** Set is running
   *  @param isRunning is running
   */
  public static void setRunning (boolean isRunning) {
    LayerKmlTestHelper.isRunning = isRunning;
  }

  /** Is Running?
   *  @return the isRunning
   */
  public static boolean isRunning () {
    return isRunning;
  }

  /** Make sure the original is still good in case of bad termination */
  public static void setupLayerKmlTest () {
    swapFile(fileOrig, fileMain);
  }
}
