package net.sf.jasperreports.engine;

import java.util.HashMap;
import java.util.Map;

public class JRHyperlinkHelper {
  public static final String HYPERLINK_TYPE_NONE = "None";
  
  public static final String HYPERLINK_TYPE_REFERENCE = "Reference";
  
  public static final String HYPERLINK_TYPE_LOCAL_ANCHOR = "LocalAnchor";
  
  public static final String HYPERLINK_TYPE_LOCAL_PAGE = "LocalPage";
  
  public static final String HYPERLINK_TYPE_REMOTE_ANCHOR = "RemoteAnchor";
  
  public static final String HYPERLINK_TYPE_REMOTE_PAGE = "RemotePage";
  
  private static final Map builtinTypes = createBuiltinTypes();
  
  private static Map createBuiltinTypes() {
    Map types = new HashMap();
    types.put("None", new Byte((byte)1));
    types.put("Reference", new Byte((byte)2));
    types.put("LocalAnchor", new Byte((byte)3));
    types.put("LocalPage", new Byte((byte)4));
    types.put("RemoteAnchor", new Byte((byte)5));
    types.put("RemotePage", new Byte((byte)6));
    return types;
  }
  
  public static byte getHyperlinkType(JRHyperlink hyperlink) {
    return getHyperlinkType(hyperlink.getLinkType());
  }
  
  public static byte getHyperlinkType(String linkType) {
    byte type;
    if (linkType == null) {
      type = 1;
    } else {
      Byte builtinType = (Byte)builtinTypes.get(linkType);
      if (builtinType == null) {
        type = 7;
      } else {
        type = builtinType.byteValue();
      } 
    } 
    return type;
  }
  
  public static String getLinkType(byte hyperlinkType) {
    String type;
    switch (hyperlinkType) {
      case 0:
      case 1:
        type = null;
        return type;
      case 2:
        type = "Reference";
        return type;
      case 3:
        type = "LocalAnchor";
        return type;
      case 4:
        type = "LocalPage";
        return type;
      case 5:
        type = "RemoteAnchor";
        return type;
      case 6:
        type = "RemotePage";
        return type;
      case 7:
        throw new JRRuntimeException("Custom hyperlink types cannot be specified using the byte constant");
    } 
    throw new JRRuntimeException("Unknown hyperlink type " + hyperlinkType);
  }
  
  public static boolean isEmpty(JRHyperlink hyperlink) {
    return (hyperlink == null || (hyperlink.getHyperlinkType() == 1 && hyperlink.getHyperlinkTooltipExpression() == null));
  }
}
