package jxl.biff.drawing;

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

public class DrawingData implements EscherStream {
  private static Logger logger = Logger.getLogger(DrawingData.class);
  
  private int numDrawings = 0;
  
  private byte[] drawingData = null;
  
  private boolean initialized = false;
  
  private EscherRecord[] spContainers;
  
  private void initialize() {
    EscherRecordData er = new EscherRecordData(this, 0);
    Assert.verify(er.isContainer());
    EscherContainer dgContainer = new EscherContainer(er);
    EscherRecord[] children = dgContainer.getChildren();
    children = dgContainer.getChildren();
    EscherContainer spgrContainer = null;
    for (int i = 0; i < children.length && spgrContainer == null; i++) {
      EscherRecord child = children[i];
      if (child.getType() == EscherRecordType.SPGR_CONTAINER)
        spgrContainer = (EscherContainer)child; 
    } 
    Assert.verify((spgrContainer != null));
    EscherRecord[] spgrChildren = spgrContainer.getChildren();
    boolean nestedContainers = false;
    for (int j = 0; j < spgrChildren.length && !nestedContainers; j++) {
      if (spgrChildren[j].getType() == EscherRecordType.SPGR_CONTAINER)
        nestedContainers = true; 
    } 
    if (!nestedContainers) {
      this.spContainers = spgrChildren;
    } else {
      ArrayList sps = new ArrayList();
      getSpContainers(spgrContainer, sps);
      this.spContainers = new EscherRecord[sps.size()];
      this.spContainers = (EscherRecord[])sps.toArray((Object[])this.spContainers);
    } 
    this.initialized = true;
  }
  
  private void getSpContainers(EscherContainer spgrContainer, ArrayList sps) {
    EscherRecord[] spgrChildren = spgrContainer.getChildren();
    for (int i = 0; i < spgrChildren.length; i++) {
      if (spgrChildren[i].getType() == EscherRecordType.SP_CONTAINER) {
        sps.add(spgrChildren[i]);
      } else if (spgrChildren[i].getType() == EscherRecordType.SPGR_CONTAINER) {
        getSpContainers((EscherContainer)spgrChildren[i], sps);
      } else {
        logger.warn("Spgr Containers contains a record other than Sp/Spgr containers");
      } 
    } 
  }
  
  public void addData(byte[] data) {
    addRawData(data);
    this.numDrawings++;
  }
  
  public void addRawData(byte[] data) {
    if (this.drawingData == null) {
      this.drawingData = data;
      return;
    } 
    byte[] newArray = new byte[this.drawingData.length + data.length];
    System.arraycopy(this.drawingData, 0, newArray, 0, this.drawingData.length);
    System.arraycopy(data, 0, newArray, this.drawingData.length, data.length);
    this.drawingData = newArray;
    this.initialized = false;
  }
  
  final int getNumDrawings() {
    return this.numDrawings;
  }
  
  EscherContainer getSpContainer(int drawingNum) {
    if (!this.initialized)
      initialize(); 
    EscherContainer spContainer = (EscherContainer)this.spContainers[drawingNum + 1];
    Assert.verify((spContainer != null));
    return spContainer;
  }
  
  public byte[] getData() {
    return this.drawingData;
  }
}
