package net.sf.jasperreports.engine.fill;

import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.JRSubreportReturnValue;
import net.sf.jasperreports.engine.JRVariable;
import net.sf.jasperreports.engine.util.JRClassLoader;

public class JRFillSubreportReturnValue implements JRSubreportReturnValue {
  protected final JRSubreportReturnValue parent;
  
  protected JRIncrementer incrementer = null;
  
  protected final JRBaseFiller filler;
  
  protected JRFillSubreportReturnValue(JRSubreportReturnValue returnValue, JRFillObjectFactory factory, JRBaseFiller filler) {
    factory.put(returnValue, this);
    this.parent = returnValue;
    this.filler = filler;
  }
  
  public String getSubreportVariable() {
    return this.parent.getSubreportVariable();
  }
  
  public String getToVariable() {
    return this.parent.getToVariable();
  }
  
  public String getIncrementerFactoryClassName() {
    return this.parent.getIncrementerFactoryClassName();
  }
  
  public byte getCalculation() {
    return this.parent.getCalculation();
  }
  
  public JRIncrementer getIncrementer() {
    if (this.incrementer == null) {
      JRIncrementerFactory incrementerFactory;
      String incrementerFactoryClassName = getIncrementerFactoryClassName();
      if (incrementerFactoryClassName == null) {
        JRVariable toVariable = this.filler.getVariable(getToVariable());
        incrementerFactory = JRDefaultIncrementerFactory.getFactory(toVariable.getValueClass());
      } else {
        try {
          Class incrementerFactoryClass = JRClassLoader.loadClassForName(incrementerFactoryClassName);
          incrementerFactory = JRIncrementerFactoryCache.getInstance(incrementerFactoryClass);
        } catch (ClassNotFoundException e) {
          throw new JRRuntimeException("Increment class " + incrementerFactoryClassName + " not found.", e);
        } 
      } 
      this.incrementer = incrementerFactory.getIncrementer(getCalculation());
    } 
    return this.incrementer;
  }
  
  public Object clone() {
    return null;
  }
}
