package jxl.biff.formula;

import common.Logger;
import jxl.WorkbookSettings;
import jxl.biff.IntegerHelper;
import jxl.biff.StringHelper;

class StringValue extends Operand implements ParsedThing {
  private static final Logger logger = Logger.getLogger(StringValue.class);
  
  private String value;
  
  private WorkbookSettings settings;
  
  public StringValue(WorkbookSettings ws) {
    this.settings = ws;
  }
  
  public StringValue(String s) {
    this.value = s;
  }
  
  public int read(byte[] data, int pos) {
    int length = IntegerHelper.getInt(data[pos], data[pos + 1]);
    int consumed = 2;
    if ((data[pos + 1] & 0x1) == 0) {
      this.value = StringHelper.getString(data, length, pos + 2, this.settings);
      consumed += length;
    } else {
      this.value = StringHelper.getUnicodeString(data, length, pos + 2);
      consumed += length * 2;
    } 
    return consumed;
  }
  
  byte[] getBytes() {
    byte[] data = new byte[this.value.length() * 2 + 3];
    data[0] = Token.STRING.getCode();
    data[1] = (byte)this.value.length();
    data[2] = 1;
    StringHelper.getUnicodeBytes(this.value, data, 3);
    return data;
  }
  
  public void getString(StringBuffer buf) {
    buf.append("\"");
    buf.append(this.value);
    buf.append("\"");
  }
}
