package jxl.biff.formula;

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

class VariableArgFunction extends Operator implements ParsedThing {
  private static Logger logger = Logger.getLogger(VariableArgFunction.class);
  
  private Function function;
  
  private int arguments;
  
  private boolean readFromSheet;
  
  private WorkbookSettings settings;
  
  public VariableArgFunction(WorkbookSettings ws) {
    this.readFromSheet = true;
    this.settings = ws;
  }
  
  public VariableArgFunction(Function f, int a, WorkbookSettings ws) {
    this.function = f;
    this.arguments = a;
    this.readFromSheet = false;
    this.settings = ws;
  }
  
  public int read(byte[] data, int pos) throws FormulaException {
    this.arguments = data[pos];
    int index = IntegerHelper.getInt(data[pos + 1], data[pos + 2]);
    this.function = Function.getFunction(index);
    if (this.function == Function.UNKNOWN)
      throw new FormulaException(FormulaException.unrecognizedFunction, index); 
    return 3;
  }
  
  public void getOperands(Stack s) {
    ParseItem[] items = new ParseItem[this.arguments];
    int i;
    for (i = this.arguments - 1; i >= 0; i--) {
      ParseItem pi = s.pop();
      items[i] = pi;
    } 
    for (i = 0; i < this.arguments; i++)
      add(items[i]); 
  }
  
  public void getString(StringBuffer buf) {
    buf.append(this.function.getName(this.settings));
    buf.append('(');
    if (this.arguments > 0) {
      ParseItem[] operands = getOperands();
      if (this.readFromSheet) {
        operands[0].getString(buf);
        for (int i = 1; i < this.arguments; i++) {
          buf.append(',');
          operands[i].getString(buf);
        } 
      } else {
        operands[this.arguments - 1].getString(buf);
        for (int i = this.arguments - 2; i >= 0; 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); 
  }
  
  Function getFunction() {
    return this.function;
  }
  
  byte[] getBytes() {
    handleSpecialCases();
    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 + 4];
    System.arraycopy(data, 0, newdata, 0, data.length);
    newdata[data.length] = !useAlternateCode() ? Token.FUNCTIONVARARG.getCode() : Token.FUNCTIONVARARG.getCode2();
    newdata[data.length + 1] = (byte)this.arguments;
    IntegerHelper.getTwoBytes(this.function.getCode(), newdata, data.length + 2);
    return newdata;
  }
  
  int getPrecedence() {
    return 3;
  }
  
  private void handleSpecialCases() {
    if (this.function == Function.SUMPRODUCT) {
      ParseItem[] operands = getOperands();
      for (int i = operands.length - 1; i >= 0; i--) {
        if (operands[i] instanceof Area)
          operands[i].setAlternateCode(); 
      } 
    } 
  }
}
