/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */
package datasource.explorer;

import shared.ResourceUtils;
import ilog.cpl.css.function.IlpCSSFunction;
import ilog.cpl.graphic.IlpGraphicView;
import ilog.cpl.model.IlpAttribute;
import ilog.cpl.model.IlpRepresentationObject;
import ilog.cpl.service.IlpContext;

public class ByteFunction implements IlpCSSFunction {
  /**
   * Returns the name of the function, that will be known in the
   * style sheet.
   */
  public String getName() {
    return "byte";
  }
  
  /**
   * Returns the delimiters of the function parameters. It is
   * strongly recommanded to return at least the comma (","). The
   * default implementation returns ",".
   * @return a string containing all the accepted delimiters. A null
   * or empty return value means there is no delimiter, so all the
   * parameters are returned as a single token.
   */
  public String getDelimiters() {
    return ",";
  }
  
  /**
   * Returns true whether the delimiters are returned also as token. 
   * @return true means the delimiters are returned as token, false
   * means the delimiters are discarded (default).
   */
  public boolean returnDelimitersAsToken() {
    return false;
  }
  
  /**
   * Calls the function.
   * @param args The parameters of the functions
   * @param type the expected returned type. May be null. It is wise
   * to return an object compatible with the expected type, or a
   * String.
   * @param appc the application context.
   * @param view the graphic view
   * @param ro the current representation object
   * @param attribute the business attribute
   * @return the value computed by this function
   */
  public Object call(Object[] args, Class type, IlpContext appc, 
                     IlpGraphicView view, IlpRepresentationObject ro, IlpAttribute attribute) {
    if (args.length > 0) {
      Object attrValue = args[0];
      if (attrValue instanceof Number) {
        long value = ((Number)attrValue).longValue();
        if (value < 1024) {
          if (value == 0)
            return "";
          return ResourceUtils.getString("label.byte.minValue");
        } else if (value < 1024*1024) {
          value = value >> 10;
          return String.valueOf(value)+" " + ResourceUtils.getString("label.byte.kb");
        } else {
          value = value >> 20;
          return String.valueOf(value)+" " + ResourceUtils.getString("label.byte.mb");
        }
      } else
        return attrValue;
    }
    throw new IllegalArgumentException ("bad parameters");
  }
}