package yi.iu.ft.local;

import java.util.Vector;
import java.util.Iterator;
import yi.iu.region.Region;

/**
 * An instance of LocalFeatureSet is the set of one kind of local feature for
 * one (image) case.  For example, all the color regions for one image.
 */
public class LocalFeatureSet implements Globalizable, Regionalizable{

  String m_localFeatureName;
  GlobalizationTempl m_globalizationTempl = null;
  Vector m_instances = null;            // elements are RegionBased, IsLocalFeature
  /**
   * Always keep a local feature instance to simplify the processing.  For example,
   * query the attribute number of the local features
   */
  IsLocalFeature m_dumbInstance = null;

  public LocalFeatureSet(Vector instances) throws GlobalizationException{
    m_instances = instances;
    if (m_instances == null || m_instances.size() == 0) {
      throw new GlobalizationException("new LocalFeatureSet with empty local feature list");
    }
    IsLocalFeature f = (IsLocalFeature)m_instances.elementAt(0);
    setLocalFeature(f.getClass().getName());
  }

  public LocalFeatureSet(String localFeatureName) throws GlobalizationException{
    setLocalFeature(localFeatureName);
  }

  protected void setLocalFeature(String localFeatureName) throws GlobalizationException{
    m_localFeatureName = localFeatureName;
    try {
      Class c = Class.forName(m_localFeatureName);
      m_dumbInstance = (IsLocalFeature)c.newInstance();
    } catch (Exception e) {
      throw new GlobalizationException("Can't use " + localFeatureName + " as local feature");
    }
  }

  public int getInstanceNum() { return m_instances.size(); }

  public Iterator listInstances() { return m_instances.iterator(); }

  public int getLocalAttributeNum() {
    return m_dumbInstance.getAttributeNum();
  }

  public Iterator listLocalAttributes() {
    return m_dumbInstance.listAttributes();
  }

  public void addInstance(IsLocalFeature f) {
    m_instances.add(f);
  }

  public int getAttributeNum() {
    if (m_globalizationTempl == null) {
      m_globalizationTempl = m_dumbInstance.getDefaultGlobalizationTemp();
    }
    return m_globalizationTempl.getNumHistAttr();
  }

  public void setGlobalizationTempl(GlobalizationTempl t) {
    m_globalizationTempl = t;
  }

//  public double[] getGlobalized() throws GlobalizationException{
//    if (m_globalizationTempl == null) m_globalizationTempl = m_dumbInstance.getDefaultGlobalizationTemp();
//    return yi.iu.ft.local.Globalize.makeGlobalizedHist(this, m_globalizationTempl);
//  }
//
//  public double[] getGlobalized(boolean[] constraints) throws GlobalizationException{
//    if (m_globalizationTempl == null) m_globalizationTempl = m_dumbInstance.getDefaultGlobalizationTemp();
//    return yi.iu.ft.local.Globalize.makeGlobalizedHist(this, m_globalizationTempl, constraints);
//  }
//
  public Iterator listRegions() {
    Vector regions = new Vector();
    for (Iterator i = m_instances.iterator(); i.hasNext();) {
        RegionBased f = (RegionBased)i.next();
        Region r = f.getRegion();
        regions.add(r);
    }
    return regions.iterator();
  }

  public int numRegions() {
    return m_instances.size();
  }

  public Globalizable spatialRelations(Regionalizable r) throws GlobalizationException{
    LocalFeatureSet s = new LocalFeatureSet("yi.iu.ft.local.RegionSpatialRelation");
    Iterator i = listRegions();
    while (i.hasNext()) {
      Iterator j = r.listRegions();
      RegionSpatialRelation f = new RegionSpatialRelation((RegionBased)i.next(), (RegionBased)j.next());
      s.addInstance(f);
    }
    return s;
  }

  public GlobalHistogram getGlobalized() {
    return null;
  }

}