package nxm.sys.libg;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

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

/** Class that will hold Feature objects that are grouped together.
 *
 *  This also provides the ability to set values across all features within
 *  using a Table defining the values in Feature {@link #setFeatureAttributes(Table)}
 *  @author Kevin Esterheld
 *  @since NeXtMidas 3.9.3
 *
 */
public class FeatureGroup {

  private Map<String, Feature> features;

  /** Construct the FeatureGroup object */
  public FeatureGroup () {
    features = new ConcurrentHashMap<>();
  }

  /** Construct the FeatureGroup object and add the given feature to the group
   *
   *  @param feature The Feature to add to the FeatureGroup
   */
  public FeatureGroup (Feature feature) {
    this();
    features.put(feature.name, feature);
  }

  /** Add a feature object to this group
   *
   *  @param feature The Feature to be added.
   */
  public void addFeature (Feature feature) {
    features.put(feature.name, feature);
  }

  /** Remove a feature object to this group
   *
   *  @param feature The Feature to be removed.
   */
  public void removeFeature (Feature feature) {
    features.remove(feature.name);
  }

  /** Remove a feature object to this group by name
   *
   *  @param name The name of the Feature to be removed.
   */
  public void removeFeature (String name) {
    features.remove(name);
  }

  /** Move all features to a new group
   *
   * @param newGroup The new group to place all features
   */
  public void moveAllFeaturesToNewGroup (String newGroup) {
    features.forEach((k,v)->v.setGroup(newGroup));
  }

  /** Get the size of the group
   *
   *  @return the int size
   */
  public int getSize () {
    return features.size();
  }

  /** Set the values of the line object for all features by
   *  sending in a Table object of keys and values. For instance
   *  {Color="GREEN"}
   *
   * @param tbl The table of values in the Line object to set.
   */
  public void setLine(Table tbl) {
    features.forEach((k,v)->v.setLine(tbl));
  }

  /** Set all Features values that are defined in the given table.
   *
   *  @param tbl Key - Value table that will set the values of all the features.
   */
  public void setFeatureAttributes (Table tbl) {
    features.forEach((k,v)->v.fromTable(tbl));
  }

  /** Get Features in this group
   *  @return map of Features
   */
  @InternalUseOnly
  public Map<String, Feature> getFeatures () {
    return features;
  }

}
