package jxl.biff.drawing;

import common.Logger;
import java.util.ArrayList;
import java.util.Iterator;

class EscherContainer extends EscherRecord {
  private static Logger logger = Logger.getLogger(EscherContainer.class);
  
  private boolean initialized;
  
  private ArrayList children;
  
  public EscherContainer(EscherRecordData erd) {
    super(erd);
    this.initialized = false;
    this.children = new ArrayList();
  }
  
  protected EscherContainer(EscherRecordType type) {
    super(type);
    setContainer(true);
    this.children = new ArrayList();
  }
  
  public EscherRecord[] getChildren() {
    if (!this.initialized)
      initialize(); 
    Object[] ca = this.children.toArray((Object[])new EscherRecord[this.children.size()]);
    return (EscherRecord[])ca;
  }
  
  public void add(EscherRecord child) {
    this.children.add(child);
  }
  
  public void remove(EscherRecord child) {
    this.children.remove(child);
  }
  
  private void initialize() {
    int curpos = getPos() + 8;
    int endpos = Math.min(getPos() + getLength(), getStreamLength());
    EscherRecord newRecord = null;
    while (curpos < endpos) {
      EscherRecordData erd = new EscherRecordData(getEscherStream(), curpos);
      EscherRecordType type = erd.getType();
      if (type == EscherRecordType.DGG) {
        newRecord = new Dgg(erd);
      } else if (type == EscherRecordType.DG) {
        newRecord = new Dg(erd);
      } else if (type == EscherRecordType.BSTORE_CONTAINER) {
        newRecord = new BStoreContainer(erd);
      } else if (type == EscherRecordType.SPGR_CONTAINER) {
        newRecord = new SpgrContainer(erd);
      } else if (type == EscherRecordType.SP_CONTAINER) {
        newRecord = new SpContainer(erd);
      } else if (type == EscherRecordType.SPGR) {
        newRecord = new Spgr(erd);
      } else if (type == EscherRecordType.SP) {
        newRecord = new Sp(erd);
      } else if (type == EscherRecordType.CLIENT_ANCHOR) {
        newRecord = new ClientAnchor(erd);
      } else if (type == EscherRecordType.CLIENT_DATA) {
        newRecord = new ClientData(erd);
      } else if (type == EscherRecordType.BSE) {
        newRecord = new BlipStoreEntry(erd);
      } else if (type == EscherRecordType.OPT) {
        newRecord = new Opt(erd);
      } else if (type == EscherRecordType.SPLIT_MENU_COLORS) {
        newRecord = new SplitMenuColors(erd);
      } else if (type == EscherRecordType.CLIENT_TEXT_BOX) {
        newRecord = new ClientTextBox(erd);
      } else {
        newRecord = new EscherAtom(erd);
      } 
      this.children.add(newRecord);
      curpos += newRecord.getLength();
    } 
    this.initialized = true;
  }
  
  byte[] getData() {
    if (!this.initialized)
      initialize(); 
    byte[] data = new byte[0];
    for (Iterator i = this.children.iterator(); i.hasNext(); ) {
      EscherRecord er = i.next();
      byte[] childData = er.getData();
      if (childData != null) {
        byte[] newData = new byte[data.length + childData.length];
        System.arraycopy(data, 0, newData, 0, data.length);
        System.arraycopy(childData, 0, newData, data.length, childData.length);
        data = newData;
      } 
    } 
    return setHeaderData(data);
  }
}
