package nxm.sys.libg;

import java.util.concurrent.ConcurrentHashMap;

import nxm.sys.inc.InternalUseOnly;
import nxm.sys.lib.Shell;

/** Class that will hold Feature groups
 *
 */
@InternalUseOnly("Prototype")
public class FeatureGroupMap extends ConcurrentHashMap<String,FeatureGroup> {

  private static final long serialVersionUID = 2070042463608891913L;

  /** Construct the FeatureGroup object */
  public FeatureGroupMap () {
  }


  /** Adds the feature to the group set in the Feature object.
   *
   *  @param feature The feature to add to the group
   *  @param featureGroup The FeatureGroup
   *  @since NeXtMidas 4.1.4
   */
  public void addFeatureToGroup (Feature feature, FeatureGroup featureGroup) {
    if (!this.containsKey(feature.getGroup())) {
      this.put(feature.getGroup(),featureGroup);
    }
    this.get(feature.getGroup()).addFeature(feature);
  }

  /** Move Feature from one group to another
   *
   *  @param oldGroup The group to move the feature from
   *  @param feature The feature - the new group is in the feature object
   *  @since NeXtMidas 4.1.4
   */
  @InternalUseOnly
  public void moveFeatureToGroup (String oldGroup, Feature feature) {
    FeatureGroup fg = this.get(oldGroup);
    if (fg != null) {
      fg.removeFeature(feature);
    }
    String newGroup = feature.getGroup();
    FeatureGroup fGroup = (this.contains(newGroup)) ? this.get(newGroup) : new FeatureGroup(feature);
    addFeatureToGroup(feature, fGroup);
  }

  /** Move all features from one group to another
   *
   *  @param oldGroup The source group of features
   *  @param newGroup The destination group of features
   *  @since NeXtMidas 3.9.3
   */
  @InternalUseOnly
  public void moveAllFeatures (String oldGroup, String newGroup) {
    if (oldGroup.equals(newGroup)) return;
    if (!this.containsKey(oldGroup)) {
      Shell.warning("No group "+oldGroup+" found.");
      return;
    }
    this.get(oldGroup).moveAllFeaturesToNewGroup(newGroup);
  }

}
