package jxl.biff.formula;

import common.Assert;
import common.Logger;
import java.util.Stack;
import jxl.WorkbookSettings;
import jxl.biff.IntegerHelper;

class BuiltInFunction extends Operator implements ParsedThing {
  private static Logger logger = Logger.getLogger(BuiltInFunction.class);
  
  private Function function;
  
  private WorkbookSettings settings;
  
  public BuiltInFunction(WorkbookSettings ws) {
    this.settings = ws;
  }
  
  public BuiltInFunction(Function f, WorkbookSettings ws) {
    this.function = f;
    this.settings = ws;
  }
  
  public int read(byte[] data, int pos) {
    int index = IntegerHelper.getInt(data[pos], data[pos + 1]);
    this.function = Function.getFunction(index);
    Assert.verify((this.function != Function.UNKNOWN), "function code " + index);
    return 2;
  }
  
  public void getOperands(Stack s) {
    ParseItem[] items = new ParseItem[this.function.getNumArgs()];
    int i;
    for (i = this.function.getNumArgs() - 1; i >= 0; i--) {
      ParseItem pi = s.pop();
      items[i] = pi;
    } 
    for (i = 0; i < this.function.getNumArgs(); i++)
      add(items[i]); 
  }
  
  public void getString(StringBuffer buf) {
    buf.append(this.function.getName(this.settings));
    buf.append('(');
    int numArgs = this.function.getNumArgs();
    if (numArgs > 0) {
      ParseItem[] operands = getOperands();
      operands[0].getString(buf);
      for (int i = 1; i < numArgs; i++) {
        buf.append(',');
        operands[i].getString(buf);
      } 
    } 
    buf.append(')');
  }
  
  public void adjustRelativeCellReferences(int colAdjust, int rowAdjust) {
    ParseItem[] operands = getOperands();
    for (int i = 0; i < operands.length; i++)
      operands[i].adjustRelativeCellReferences(colAdjust, rowAdjust); 
  }
  
  void columnInserted(int sheetIndex, int col, boolean currentSheet) {
    ParseItem[] operands = getOperands();
    for (int i = 0; i < operands.length; i++)
      operands[i].columnInserted(sheetIndex, col, currentSheet); 
  }
  
  void columnRemoved(int sheetIndex, int col, boolean currentSheet) {
    ParseItem[] operands = getOperands();
    for (int i = 0; i < operands.length; i++)
      operands[i].columnRemoved(sheetIndex, col, currentSheet); 
  }
  
  void rowInserted(int sheetIndex, int row, boolean currentSheet) {
    ParseItem[] operands = getOperands();
    for (int i = 0; i < operands.length; i++)
      operands[i].rowInserted(sheetIndex, row, currentSheet); 
  }
  
  void rowRemoved(int sheetIndex, int row, boolean currentSheet) {
    ParseItem[] operands = getOperands();
    for (int i = 0; i < operands.length; i++)
      operands[i].rowRemoved(sheetIndex, row, currentSheet); 
  }
  
  byte[] getBytes() {
    ParseItem[] operands = getOperands();
    byte[] data = new byte[0];
    for (int i = 0; i < operands.length; i++) {
      byte[] opdata = operands[i].getBytes();
      byte[] arrayOfByte1 = new byte[data.length + opdata.length];
      System.arraycopy(data, 0, arrayOfByte1, 0, data.length);
      System.arraycopy(opdata, 0, arrayOfByte1, data.length, opdata.length);
      data = arrayOfByte1;
    } 
    byte[] newdata = new byte[data.length + 3];
    System.arraycopy(data, 0, newdata, 0, data.length);
    newdata[data.length] = !useAlternateCode() ? Token.FUNCTION.getCode() : Token.FUNCTION.getCode2();
    IntegerHelper.getTwoBytes(this.function.getCode(), newdata, data.length + 1);
    return newdata;
  }
  
  int getPrecedence() {
    return 3;
  }
}
