package nc.bd.itf.tools;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pub.lang.UFDouble;

public class BFPubTools
{
  public static UFDouble ZERO = UFDouble.ZERO_DBL;

  public static boolean isEmpty(String value)
  {
    return (value == null) || (value.trim().length() == 0);
  }

  public static UFBoolean getUFBoolean_NullAs(Object value, UFBoolean bDefautValue)
  {
    if ((value == null) || (value.toString().trim().equals("")))
      return bDefautValue;
    if ((value instanceof UFBoolean)) {
      return (UFBoolean)value;
    }
    return new UFBoolean(value.toString().trim());
  }

  public static UFDate getUFDate(Object value)
  {
    if ((value == null) || (value.toString().trim().equals("")))
      return null;
    if ((value instanceof UFDate)) {
      return (UFDate)value;
    }
    return new UFDate(value.toString().trim());
  }

  public static UFDateTime getUFDateTime(Object value)
  {
    if ((value == null) || (value.toString().trim().equals("")))
      return null;
    if ((value instanceof UFDateTime)) {
      return (UFDateTime)value;
    }
    return new UFDateTime(value.toString().trim());
  }

  public static UFDouble getUFDouble_NullAsZero(Object value)
  {
    if ((value == null) || (value.toString().trim().equals("")) || (value.toString().trim().equals("~")))
      return ZERO;
    if ((value instanceof UFDouble))
      return (UFDouble)value;
    if ((value instanceof BigDecimal)) {
      return new UFDouble((BigDecimal)value);
    }
    return new UFDouble(value.toString().trim());
  }

  public static UFDouble getUFDouble_ValueAsValue(double dValue)
  {
    if (dValue == 0.0D) {
      return ZERO;
    }
    return new UFDouble(dValue);
  }

  public static UFDouble getUFDouble_ValueAsValue(Object value)
  {
    if ((value == null) || (value.toString().trim().equals("")))
      return null;
    if ((value instanceof UFDouble))
      return (UFDouble)value;
    if ((value instanceof BigDecimal)) {
      return new UFDouble((BigDecimal)value);
    }
    return new UFDouble(value.toString().trim());
  }

  public static UFDouble getUFDouble_ZeroAsNull(double dValue)
  {
    if (dValue == 0.0D) {
      return null;
    }
    return new UFDouble(dValue);
  }

  public static UFDouble getUFDouble_ZeroAsNull(Object value)
  {
    UFDouble dValue = getUFDouble_NullAsZero(value);
    if (dValue.compareTo(ZERO) == 0) {
      return null;
    }
    return dValue;
  }

  public static String getString_TrimZeroLenAsNull(Object value)
  {
    if ((value == null) || (value.toString().trim().length() == 0)) {
      return null;
    }
    return value.toString().trim();
  }

  public static String getString_TrimZeroLenAs(Object value, String str)
  {
    if ((value == null) || (value.toString().trim().length() == 0)) {
      return str;
    }
    return value.toString().trim();
  }

  public static String getString_TrimAsNull(Object value) {
    if ((value == null) || (value.toString().trim().length() == 0)) {
      return "";
    }
    return value.toString().trim();
  }

  public static String getInStr(Collection<?> coll)
  {
    String intStr = null;
    if ((coll != null) && (coll.size() > 0)) {
      StringBuilder sb = new StringBuilder();
      String[] values = (String[])coll.toArray(new String[0]);
      for (int i = 0; i < values.length; i++) {
        sb.append("'").append(values[i]).append("'");
        if (i < values.length - 1)
          sb.append(",");
      }
      intStr = sb.toString();
    }
    return intStr;
  }

  public static String getInSqlWithOutAnd(String sFieldName, ArrayList<?> alValue, int start, int num)
  {
    if ((sFieldName == null) || (sFieldName.trim().length() == 0) || 
      (alValue == null) || (start < 0) || (num < 0))
      return null;
    StringBuffer sbSQL = new StringBuffer(200);
    sbSQL.append(" (").append(sFieldName).append(" IN (");
    int end = start + num;
    for (int i = start; i < end; i++) {
      if ((alValue.get(i) != null) && 
        (alValue.get(i).toString().trim().length() > 0)) {
        sbSQL.append("'").append(alValue.get(i)).append("'");
        if ((i != alValue.size() - 1) && ((i <= 0) || (i % 200 != 0)))
          sbSQL.append(",");
      } else {
        return null;
      }
      if ((i > 0) && (i % 200 == 0))
        sbSQL.append(" ) OR ").append(sFieldName).append(" IN ( ");
    }
    sbSQL.append(" ) )");
    return sbSQL.toString();
  }
  public static String getInSqlWithOutAnd(String sFieldName, List<?> alValue, int start, int num)
  {
    if ((sFieldName == null) || (sFieldName.trim().length() == 0) || 
      (alValue == null) || (start < 0) || (num < 0))
      return null;
    StringBuffer sbSQL = new StringBuffer(200);
    sbSQL.append(" (").append(sFieldName).append(" IN (");
    int end = start + num;
    for (int i = start; i < end; i++) {
      if ((alValue.get(i) != null) && 
        (alValue.get(i).toString().trim().length() > 0)) {
        sbSQL.append("'").append(alValue.get(i)).append("'");
        if ((i != alValue.size() - 1) && ((i <= 0) || (i % 200 != 0)))
          sbSQL.append(",");
      } else {
        return null;
      }
      if ((i > 0) && (i % 200 == 0))
        sbSQL.append(" ) OR ").append(sFieldName).append(" IN ( ");
    }
    sbSQL.append(" ) )");
    return sbSQL.toString();
  }
  public static String getInSqlWithOutAnd(String sFieldName, String[] saValue, int start, int num)
  {
    if ((sFieldName == null) || (sFieldName.trim().length() == 0) || 
      (saValue == null) || (start < 0) || (num < 0) || 
      (saValue.length < start + num))
      return null;
    StringBuffer sbSQL = new StringBuffer(200);
    sbSQL.append(" (").append(sFieldName).append(" IN ( ");
    int end = start + num;
    for (int i = start; i < end; i++) {
      if ((saValue[i] != null) && (saValue[i].trim().length() > 0)) {
        sbSQL.append("'").append(saValue[i]).append("'");
        if ((i != saValue.length - 1) && ((i <= 0) || (i % 200 != 0)))
          sbSQL.append(",");
      } else {
        return null;
      }
      if ((i > 0) && (i % 200 == 0))
        sbSQL.append(" ) OR ").append(sFieldName).append(" IN ( ");
    }
    sbSQL.append(" ) )");
    return sbSQL.toString();
  }

  public static String getInSqlWithOutAnd(String sFieldName, Object[] oValue, int start, int num) {
    if ((sFieldName == null) || (sFieldName.trim().length() == 0) || 
      (oValue == null) || (start < 0) || (num < 0) || 
      (oValue.length < start + num))
      return null;
    StringBuffer sbSQL = new StringBuffer(200);
    sbSQL.append(" (").append(sFieldName).append(" IN ( ");
    int end = start + num;
    for (int i = start; i < end; i++) {
      String sValue = getString_TrimZeroLenAsNull(oValue);
      if (oValue[i] != null) {
        sbSQL.append("'").append(sValue).append("'");
        if ((i != oValue.length - 1) && ((i <= 0) || (i % 200 != 0)))
          sbSQL.append(",");
      } else {
        return null;
      }
      if ((i > 0) && (i % 200 == 0))
        sbSQL.append(" ) OR ").append(sFieldName).append(" IN ( ");
    }
    sbSQL.append(" ) )");
    return sbSQL.toString();
  }
}