/* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. */
/* ------------------------------------------------------ */
/* ------------------- amx-converter.js ----------------- */
/* ------------------------------------------------------ */

(function(){
// TODO finish the migration from "amx.*" to "adf.mf.api.amx.*" and "adf.mf.internal.amx.*"
  function getJSPattern(pattern, dateStyle, timeStyle, type, locale){
    if (pattern){
      return pattern;
    }else{
      var datePattern, timePattern;
      var localeSymbols = getLocaleSymbols(locale);
      if (!type){
        type = "date";
      }
      if (type == "both" || type == "date"){
        if (!dateStyle){
          dateStyle = "default";
        }
        switch (dateStyle){
          case "full":
            datePattern = localeSymbols.getFullDatePatternString();
            break;
          case "long":
            datePattern = localeSymbols.getLongDatePatternString();
            break;
          case "default":
          case "medium":
          default:
            datePattern = localeSymbols.getMediumDatePatternString();
            break;
          case "short":
            datePattern = localeSymbols.getShortDatePatternString();
            break;
        }
      }
      if (type == "both" || type == "time"){
        if (!timeStyle){
          timeStyle = "default";
        }
        switch (timeStyle){
          case "full":
            timePattern = localeSymbols.getFullTimePatternString();
            break;
          case "long":
            timePattern = localeSymbols.getLongTimePatternString();
            break;
          case "default":
          case "medium":
          default:
            timePattern = localeSymbols.getMediumTimePatternString();
            break;
          case "short":
            timePattern = localeSymbols.getShortTimePatternString();
            break;
        }
      }
      if (datePattern && timePattern){
        pattern = datePattern + " " + timePattern;
      }else if (datePattern){
        pattern = datePattern;
      }else if (timePattern){
        pattern = timePattern;
      }
      return pattern;
    }
  }

  amx.createNumberConverter = function($node,label){
    if (TrNumberConverter){
      var pattern = null;
      var type = $node.attr("type");
      var locale = null;
      var messages = null;
      var integerOnly = $node.attr("integerOnly");
      var groupingUsed = $node.attr("groupingUsed");
      var currencyCode = $node.attr("currencyCode");
      var currencySymbol = $node.attr("currencySymbol");
      var maxFractionDigits = $node.attr("maxFractionDigits");
      var maxIntegerDigits = $node.attr("maxIntegerDigits");
      var minFractionDigits = $node.attr("minFractionDigits");
      var minIntegerDigits = $node.attr("minIntegerDigits");
      if (!type){
        type = "number";
      }
      if (integerOnly){
        integerOnly = (integerOnly == "true");
      }
      if (groupingUsed){
        groupingUsed = (groupingUsed == "true");
      }
      var numberConverter = new TrNumberConverter(pattern, type, locale, messages,
        integerOnly, groupingUsed, currencyCode, currencySymbol, maxFractionDigits,
        maxIntegerDigits, minFractionDigits, minIntegerDigits);
      var converter = {};
      converter.getAsString = function(obj){
        adf.mf.api.amx.logInfoResource(adf.mf.log.level.INFO, "getAsString", "MSG_CONVERTING", obj);
        var str;
        if (typeof obj === "undefined" || obj === null || obj === ""){
          str = "";
        }else if (typeof obj[".null"] !== "undefined" && obj[".null"]){
          str = "";
        }else if ($.isNumeric(obj)){
          str = numberConverter.getAsString(obj, label);
        }else{
          str = obj;
        }
        adf.mf.api.amx.logInfoResource(adf.mf.log.level.INFO, "getAsString", "MSG_CONVERTED", obj, str);
        return str;
      }
      converter.getAsObject = function(str){
        adf.mf.api.amx.logInfoResource(adf.mf.log.level.INFO, "getAsObject", "MSG_CONVERTING", str);
        var obj;
        if (typeof str === "undefined" || str === null){
          obj = "";
        }else{
          obj = numberConverter.getAsObject(str, label);
          if (typeof obj === "undefined" || obj === null){
            obj = "";
          }
        }
        adf.mf.api.amx.logInfoResource(adf.mf.log.level.INFO, "getAsObject", "MSG_CONVERTED", str, obj);
        return obj;
      }
      return converter;
    }else{
      return undefined;
    }
  }

  amx.createDateTimeConverter = function($node, label)
  {
    if (TrDateTimeConverter)
    {
      var pattern = $node.attr("pattern");
      var locale = null;
      var examplestring = null;
      var type = $node.attr("type");
      var messages = null;
      var dateStyle = $node.attr("dateStyle");
      var timeStyle = $node.attr("timeStyle");
      pattern = getJSPattern(pattern, dateStyle, timeStyle, type, locale);
      var dateTimeConverter = new TrDateTimeConverter(pattern, locale, examplestring, type, messages);
      var converter = {};
      converter.getAsString = function(obj)
      {
        adf.mf.api.amx.logInfoResource(adf.mf.log.level.INFO, "getAsString", "MSG_CONVERTING", obj);
        var str;
        if (typeof obj === "undefined" || obj === null || obj === "")
        {
          str = "";
        }
        else if (typeof obj[".null"] !== "undefined" && obj[".null"])
        {
          str = "";
        }
        else if (typeof obj == "string")
        {
          var dateParse = Date.parse(obj);
          if (isNaN(dateParse))
          {
            str = obj;
          }
          else
          {
            var newDate = new Date(dateParse);
            str = dateTimeConverter.getAsString(newDate, label);
          }
        }
        else if (obj instanceof Date)
        {
          str = dateTimeConverter.getAsString(obj, label);
        }
        else if (typeof obj[".type"] !== "undefined" && obj[".type"] == "java.util.Date")
        {
          obj = new Date(obj["time"]);
          str = dateTimeConverter.getAsString(obj, label);
        }
        else
        {
          str = obj;
        }
        adf.mf.api.amx.logInfoResource(adf.mf.log.level.INFO, "getAsString", "MSG_CONVERTED", obj, str);
        return str;
      }
      converter.getAsObject = function(str)
      {
        adf.mf.api.amx.logInfoResource(adf.mf.log.level.INFO, "getAsObject", "MSG_CONVERTING", str);
        var obj;
        if (typeof str === "undefined" || str === null)
        {
          obj = "";
        }
        else
        {
          obj = dateTimeConverter.getAsObject(str, label);
          if (typeof obj === "undefined" || obj === null)
          {
            obj = "";
          }
        }
        adf.mf.api.amx.logInfoResource(adf.mf.log.level.INFO, "getAsObject", "MSG_CONVERTED", str, obj);
        return obj;
      }
      return converter;
    }
    else
    {
      return undefined;
    }
  }
})();
