package jxl.biff.drawing;

import common.Logger;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import jxl.WorkbookSettings;
import jxl.biff.ByteData;
import jxl.biff.IntegerHelper;
import jxl.write.biff.File;

public class SheetDrawingWriter {
  private static Logger logger = Logger.getLogger(SheetDrawingWriter.class);
  
  private ArrayList drawings;
  
  private boolean drawingsModified;
  
  private Chart[] charts = new Chart[0];
  
  private WorkbookSettings workbookSettings;
  
  public void setDrawings(ArrayList dr, boolean mod) {
    this.drawings = dr;
    this.drawingsModified = mod;
  }
  
  public void write(File outputFile) throws IOException {
    if (this.drawings.size() == 0 && this.charts.length == 0)
      return; 
    boolean modified = this.drawingsModified;
    int numImages = this.drawings.size();
    for (Iterator i = this.drawings.iterator(); i.hasNext() && !modified; ) {
      DrawingGroupObject d = i.next();
      if (d.getOrigin() != Origin.READ)
        modified = true; 
    } 
    if (numImages > 0 && !modified) {
      DrawingGroupObject d2 = this.drawings.get(0);
      if (!d2.isFirst())
        modified = true; 
    } 
    if (numImages == 0 && this.charts.length == 1 && this.charts[0].getMsoDrawingRecord() == null)
      modified = false; 
    if (!modified) {
      writeUnmodified(outputFile);
      return;
    } 
    Object[] spContainerData = new Object[numImages + this.charts.length];
    int length = 0;
    EscherContainer firstSpContainer = null;
    int j;
    for (j = 0; j < numImages; j++) {
      DrawingGroupObject drawing = this.drawings.get(j);
      EscherContainer spc = drawing.getSpContainer();
      byte[] data = spc.getData();
      spContainerData[j] = data;
      if (j == 0) {
        firstSpContainer = spc;
      } else {
        length += data.length;
      } 
    } 
    for (j = 0; j < this.charts.length; j++) {
      EscherContainer escherContainer = this.charts[j].getSpContainer();
      byte[] data = escherContainer.getData();
      data = escherContainer.setHeaderData(data);
      spContainerData[j + numImages] = data;
      if (j == 0 && numImages == 0) {
        firstSpContainer = escherContainer;
      } else {
        length += data.length;
      } 
    } 
    DgContainer dgContainer = new DgContainer();
    Dg dg = new Dg(numImages + this.charts.length);
    dgContainer.add(dg);
    SpgrContainer spgrContainer = new SpgrContainer();
    SpContainer spContainer = new SpContainer();
    Spgr spgr = new Spgr();
    spContainer.add(spgr);
    Sp sp = new Sp(ShapeType.MIN, 1024, 5);
    spContainer.add(sp);
    spgrContainer.add(spContainer);
    spgrContainer.add(firstSpContainer);
    dgContainer.add(spgrContainer);
    byte[] firstMsoData = dgContainer.getData();
    int len = IntegerHelper.getInt(firstMsoData[4], firstMsoData[5], firstMsoData[6], firstMsoData[7]);
    IntegerHelper.getFourBytes(len + length, firstMsoData, 4);
    len = IntegerHelper.getInt(firstMsoData[28], firstMsoData[29], firstMsoData[30], firstMsoData[31]);
    IntegerHelper.getFourBytes(len + length, firstMsoData, 28);
    if (numImages > 0 && ((DrawingGroupObject)this.drawings.get(0)).isFormObject()) {
      byte[] msodata2 = new byte[firstMsoData.length - 8];
      System.arraycopy(firstMsoData, 0, msodata2, 0, msodata2.length);
      firstMsoData = msodata2;
    } 
    MsoDrawingRecord msoDrawingRecord = new MsoDrawingRecord(firstMsoData);
    outputFile.write((ByteData)msoDrawingRecord);
    if (numImages > 0) {
      DrawingGroupObject firstDrawing = this.drawings.get(0);
      firstDrawing.writeAdditionalRecords(outputFile);
    } else {
      Chart chart = this.charts[0];
      ObjRecord objRecord = chart.getObjRecord();
      outputFile.write((ByteData)objRecord);
      outputFile.write(chart);
    } 
    for (int k = 1; k < spContainerData.length; k++) {
      byte[] bytes = (byte[])spContainerData[k];
      if (k < numImages && ((DrawingGroupObject)this.drawings.get(k)).isFormObject()) {
        byte[] bytes2 = new byte[bytes.length - 8];
        System.arraycopy(bytes, 0, bytes2, 0, bytes2.length);
        bytes = bytes2;
      } 
      msoDrawingRecord = new MsoDrawingRecord(bytes);
      outputFile.write((ByteData)msoDrawingRecord);
      if (k < numImages) {
        DrawingGroupObject d = this.drawings.get(k);
        d.writeAdditionalRecords(outputFile);
      } else {
        Chart chart = this.charts[k - numImages];
        ObjRecord objRecord = chart.getObjRecord();
        outputFile.write((ByteData)objRecord);
        outputFile.write(chart);
      } 
    } 
    for (Iterator iterator1 = this.drawings.iterator(); iterator1.hasNext(); ) {
      DrawingGroupObject dgo2 = iterator1.next();
      dgo2.writeTailRecords(outputFile);
    } 
  }
  
  private void writeUnmodified(File outputFile) throws IOException {
    if (this.charts.length == 0 && this.drawings.size() == 0)
      return; 
    if (this.charts.length == 0 && this.drawings.size() != 0) {
      for (Iterator iterator2 = this.drawings.iterator(); iterator2.hasNext(); ) {
        DrawingGroupObject d = iterator2.next();
        outputFile.write((ByteData)d.getMsoDrawingRecord());
        d.writeAdditionalRecords(outputFile);
      } 
      for (Iterator iterator1 = this.drawings.iterator(); iterator1.hasNext(); ) {
        DrawingGroupObject d = iterator1.next();
        d.writeTailRecords(outputFile);
      } 
      return;
    } 
    if (this.drawings.size() == 0 && this.charts.length != 0) {
      Chart curChart = null;
      for (int k = 0; k < this.charts.length; k++) {
        curChart = this.charts[k];
        if (curChart.getMsoDrawingRecord() != null)
          outputFile.write((ByteData)curChart.getMsoDrawingRecord()); 
        if (curChart.getObjRecord() != null)
          outputFile.write((ByteData)curChart.getObjRecord()); 
        outputFile.write(curChart);
      } 
      return;
    } 
    int numDrawings = this.drawings.size();
    int length = 0;
    EscherContainer[] spContainers = new EscherContainer[numDrawings + this.charts.length];
    boolean[] isFormObject = new boolean[numDrawings + this.charts.length];
    int i;
    for (i = 0; i < numDrawings; i++) {
      DrawingGroupObject d = this.drawings.get(i);
      spContainers[i] = d.getSpContainer();
      if (i > 0)
        length += spContainers[i].getLength(); 
      if (d.isFormObject())
        isFormObject[i] = true; 
    } 
    for (i = 0; i < this.charts.length; i++) {
      spContainers[i + numDrawings] = this.charts[i].getSpContainer();
      length += spContainers[i + numDrawings].getLength();
    } 
    DgContainer dgContainer = new DgContainer();
    Dg dg = new Dg(numDrawings + this.charts.length);
    dgContainer.add(dg);
    SpgrContainer spgrContainer = new SpgrContainer();
    SpContainer spContainer = new SpContainer();
    Spgr spgr = new Spgr();
    spContainer.add(spgr);
    Sp sp = new Sp(ShapeType.MIN, 1024, 5);
    spContainer.add(sp);
    spgrContainer.add(spContainer);
    spgrContainer.add(spContainers[0]);
    dgContainer.add(spgrContainer);
    byte[] firstMsoData = dgContainer.getData();
    int len = IntegerHelper.getInt(firstMsoData[4], firstMsoData[5], firstMsoData[6], firstMsoData[7]);
    IntegerHelper.getFourBytes(len + length, firstMsoData, 4);
    len = IntegerHelper.getInt(firstMsoData[28], firstMsoData[29], firstMsoData[30], firstMsoData[31]);
    IntegerHelper.getFourBytes(len + length, firstMsoData, 28);
    if (isFormObject[0] == true) {
      byte[] cbytes = new byte[firstMsoData.length - 8];
      System.arraycopy(firstMsoData, 0, cbytes, 0, cbytes.length);
      firstMsoData = cbytes;
    } 
    MsoDrawingRecord msoDrawingRecord = new MsoDrawingRecord(firstMsoData);
    outputFile.write((ByteData)msoDrawingRecord);
    DrawingGroupObject dgo = this.drawings.get(0);
    dgo.writeAdditionalRecords(outputFile);
    for (int j = 1; j < spContainers.length; j++) {
      byte[] bytes = spContainers[j].getBytes();
      byte[] bytes2 = spContainers[j].setHeaderData(bytes);
      if (isFormObject[j] == true) {
        byte[] cbytes = new byte[bytes2.length - 8];
        System.arraycopy(bytes2, 0, cbytes, 0, cbytes.length);
        bytes2 = cbytes;
      } 
      msoDrawingRecord = new MsoDrawingRecord(bytes2);
      outputFile.write((ByteData)msoDrawingRecord);
      if (j < numDrawings) {
        dgo = this.drawings.get(j);
        dgo.writeAdditionalRecords(outputFile);
      } else {
        Chart chart = this.charts[j - numDrawings];
        ObjRecord objRecord = chart.getObjRecord();
        outputFile.write((ByteData)objRecord);
        outputFile.write(chart);
      } 
    } 
    for (Iterator iterator = this.drawings.iterator(); iterator.hasNext(); ) {
      DrawingGroupObject dgo2 = iterator.next();
      dgo2.writeTailRecords(outputFile);
    } 
  }
  
  public void setCharts(Chart[] ch) {
    this.charts = ch;
  }
  
  public Chart[] getCharts() {
    return this.charts;
  }
  
  public SheetDrawingWriter(WorkbookSettings ws) {}
}
