package nxm.sys.test.junit.lib;

import java.awt.Container;

import org.junit.Before;

import nxm.sys.lib.Midas;
import nxm.sys.lib.Shell;
import nxm.sys.libg.GPrimitive;
import nxm.sys.libg.MJPanel;
import nxm.sys.libg.MWindow;
import nxm.sys.prim.plot;
import nxm.sys.test.plottester;
import nxm.sys.test.junit.NMTestCase;

/** Test the GPrimitive Class
 *  @author Kevin Esterheld
 *  @since 3.9.2
 */
public class GPrimitiveTest extends NMTestCase {
  Midas midas = getMidas();

  /**
   * Creates a new instance of the class.
   * @param methodName The name of the method being called.
   */
  public GPrimitiveTest (String methodName) {
    this(methodName, null); // initialized midas context for testMakeSymLink()
  }

  /**
   * Creates a new instance of the class.
   * @param methodName The name of the method being called.
   * @param midas The midas context to use when running the tests.
   */
  public GPrimitiveTest (String methodName, Midas midas) {
    super(methodName, midas);
  }

  plottester pt;
  plot plot;
  MWindow mw;
  Container panel;
  boolean skippingBecauseHeadless = false;

  @Before
  public void setUp() {
    if (Shell.isHeadless()) {
      skippingBecauseHeadless = true;
      return;
    }
    pt       = new plottester();
    plot     = new nxm.sys.prim.plot();
    panel    = new MJPanel();
    mw       = new MWindow();
    mw.panel = panel;
    pt.MW    = mw;
    plot.MW  = mw;
  }

  /**
   * Tests that regardless of GPrimitive package, it returns true if it is a version of GPrimitive
   * (For JDK 11 modularity, there are now two versions of GPrimitive - libg.GPrimitve and lib.GPrimitve)
   * @since NeXtMidas 3.9.2
   */
  public void testIsInstance () {
    if (skippingBecauseHeadless) {
      getMidas().info("Skipping testIsInstance because HEADLESS");
      return;
    }
    assertTrue(GPrimitive.isInstance(pt));
    assertTrue(GPrimitive.isInstance(plot));
  }

  /**
   * Gets the MWindow from the GPrimitive object regardless of package
   * This method is used for JDK 11 modularity so that the code can use libg.GPrimitve
   * and lib.GPrimitve interchangeably.
   * @since NeXtMidas 3.9.2
   */
  public void testGetMWindowViaReflection () {
    if (skippingBecauseHeadless) {
      getMidas().info("Skipping testGetMWindowViaReflection because HEADLESS");
      return;
    }
    assertEquals(mw, GPrimitive.getMWindowViaReflection(plot));
    assertEquals(mw, GPrimitive.getMWindowViaReflection(pt));
  }

  /**
   * Gets the Panel (Graphics Container) from the GPrimitive object regardless of package
   * This method is used for JDK 11 modularity so that the code can use libg.GPrimitve
   * and lib.GPrimitve interchangeably.
   * @since NeXtMidas 3.9.2
   */
  public void testGetGraphicsContainerViaReflection () {
    if (skippingBecauseHeadless) {
      getMidas().info("Skipping testGetGraphicsContainerViaReflection because HEADLESS");
      return;
    }
    assertEquals(panel, GPrimitive.getGraphicsContainerViaReflection(plot));
    assertEquals(panel, GPrimitive.getGraphicsContainerViaReflection(pt));
  }
}
