package nxm.sys.test.junit.libg;

import java.awt.Color;
import java.util.Map;

import nxm.sys.inc.MidasColor;
import nxm.sys.lib.Midas;
import nxm.sys.lib.Shell;
import nxm.sys.lib.Table;
import nxm.sys.libg.Feature;
import nxm.sys.libg.FeatureGroup;
import nxm.sys.libg.MPlot;
import nxm.sys.test.junit.NMTestCase;

/** Feature Group Testing
 *  Initial purpose is to show how to use Feature Groups
 *
 *  @author Jeanette McCall
 *  @since NeXtMidas 4.0.0
 *
 */
public class FeatureGroupTest extends NMTestCase {
  /** public constructor to run this JUnit test case outside of NeXtMidas (e.g. Eclipse) */
  public FeatureGroupTest (String methodName) { this(methodName, null); }

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

  //////////////////////////////////////////////////////////////////////////////

  /** test where split metaFeats into two group */
  public void testStandardUsageExample1 () {
    if (Shell.isHeadless()) {
      getMidas().info("Skipping testStandardUsageExample1 because HEADLESS");
      return;
    }
    MPlot MP = new MPlot("testMPlot", null);
    MP.open();
    Color regular = MidasColor.MODERATE_BLUE;
    Color darker  = regular.darker();

    // example with two different groups, since you set color to darker on odds
    Feature[] metaFeats = new Feature[20];
    for (int ii=0;ii<metaFeats.length;ii=ii+2) {
      metaFeats[ii] = new Feature();
      metaFeats[ii].setName("METAFEAT" + ii);
      metaFeats[ii].setGroup("META_EVEN");
      MP.addFeature("METAFEAT"+ii, metaFeats[ii]);
    }
    for (int ii=1;ii<metaFeats.length;ii=ii+2) {
      metaFeats[ii] = new Feature();
      metaFeats[ii].setName("METAFEAT" + ii);
      metaFeats[ii].setGroup("META_ODD");
      MP.addFeature("METAFEAT"+ii, metaFeats[ii]);
    }
    Map<String, FeatureGroup> featureGroups = MP.getFeatureGroups();

    Table lineTable = new Table();

    // set the attributes of the even group
    // (set line color in a different step than other attributes)
    FeatureGroup evenMetaGroup = featureGroups.get("META_EVEN");
    lineTable.setKey("COLOR", regular);
    evenMetaGroup.setLine(lineTable);
    Table generalAttributesTable = new Table("{ENABLE=-GLOBAL}");
    evenMetaGroup.setFeatureAttributes(generalAttributesTable);

    // set different attributes for the odd group
    // (set all the attributes with one table)
    FeatureGroup oddMetaGroup = featureGroups.get("META_ODD");
    generalAttributesTable.put("ENABLE", "GLOBAL");
    lineTable.setKey("COLOR", darker);
    generalAttributesTable.put("LINE", lineTable);;
    oddMetaGroup.setLine(lineTable);

    // check the attributes of the even group
    for (int ii=0;ii<metaFeats.length;ii=ii+2) {
      assertEquals("Even metaFeat["+ii+"] color set properly",regular,metaFeats[ii].getLine().getColor());
      assertEquals("Even metaFeat["+ii+"] ENABLE=-GLOBAL set properly",
          false,(metaFeats[ii].getEnable()&MPlot.EN_GLOBAL)==1);
    }

    // check the attributes of the odd group
    for (int ii=1;ii<metaFeats.length;ii=ii+2) {
      assertEquals("Odd metaFeat["+ii+"] color set properly",darker,metaFeats[ii].getLine().getColor());
      assertEquals("Even metaFeat["+ii+"] ENABLE=GLOBAL set properly",
          true,(metaFeats[ii].getEnable()&MPlot.EN_GLOBAL)==1);
    }

  }

  /** test where keep metaFeats as one group */
  public void testStandardUsageExample2 () {
    if (Shell.isHeadless()) {
      getMidas().info("Skipping testStandardUsageExample2 because HEADLESS");
      return;
    }
    MPlot MP = new MPlot("testMPlot", null);
    MP.open();
    Color regular = MidasColor.MODERATE_BLUE;
    Color darker  = regular.darker();

    // example with two different groups, since you set color to darker on odds
    Feature[] metaFeats = new Feature[20];
    for (int ii=0;ii<metaFeats.length;ii++) {
      metaFeats[ii] = new Feature();
      metaFeats[ii].setName("METAFEAT" + ii);
      metaFeats[ii].setGroup("METAGROUP");
      Color metaColor = (ii%2==0) ? regular : darker;
      metaFeats[ii].getLine().setColor(metaColor);
      MP.addFeature("METAFEAT"+ii, metaFeats[ii]);
    }

    Map<String, FeatureGroup> featureGroups = MP.getFeatureGroups();

    // set any common attributes here
    FeatureGroup metaGroup = featureGroups.get("METAGROUP");
    Table generalAttributesTable = new Table("{ENABLE=-GLOBAL}");
    metaGroup.setFeatureAttributes(generalAttributesTable);


    // check the attributes of the even group
    for (int ii=0;ii<metaFeats.length;ii=ii+2) {
      assertEquals("Even metaFeat["+ii+"] color set properly",regular,metaFeats[ii].getLine().getColor());
      assertEquals("Even metaFeat["+ii+"] ENABLE=-GLOBAL set properly",
          false,(metaFeats[ii].getEnable()&MPlot.EN_GLOBAL)==1);
    }

    // check the attributes of the odd group
    for (int ii=1;ii<metaFeats.length;ii=ii+2) {
      assertEquals("Odd metaFeat["+ii+"] color set properly",darker,metaFeats[ii].getLine().getColor());
      assertEquals("Even metaFeat["+ii+"] ENABLE=GLOBAL set properly",
          false,(metaFeats[ii].getEnable()&MPlot.EN_GLOBAL)==1);
    }

  }
}
