using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using log4net;

namespace Axiom.Utils
{
  public class Utility
  {
    private static ILog logger = LogManager.GetLogger(typeof(Utility));
    private Utility()
    {
    }
    public static Stream StreamFromString(string s)
    {
      var stream = new MemoryStream();
      var writer = new StreamWriter(stream);
      writer.Write(s);
      writer.Flush();
      stream.Position = 0;
      return stream;
    }
    public static String ListToString(List<String> list)
    {
      if (null == list || 0 == list.Count) return "";
      StringBuilder sb = new StringBuilder();
      for (int index = 0; index < list.Count; index++)
      {
        sb.Append(list[index]);
        if (index < list.Count-1) sb.Append(",");
      }
      return sb.ToString();
    }
    public static String DateTimeToStringYYYYMMDD(DateTime dateTime)
    {
      return dateTime.ToString("yyyyMMdd");
    }
    public static String DateTimeToStringMMHDDHYYYY(DateTime dateTime)
    {
      return dateTime.ToString("MM-dd-yyyy");
    }
    public static String DateTimeToStringMMSDDSYYYYHHMMSS(DateTime dateTime)
    {
      return dateTime.ToString("MM/dd/yyyy hh:mm:ss tt");
    }
    public static String DateTimeToTicks(DateTime dateTime)
    {
      return dateTime.Ticks.ToString();
    }
    public static String KeepAfter(String strString,char ch)
    {
      int startPos = strString.IndexOf(ch);
      if (-1 == startPos) return strString;
      return strString.Substring(startPos + 1);
    }
    public static String KeepBefore(String strString, char ch)
    {
      int startPos = strString.IndexOf(ch);
      if (-1 == startPos) return strString;
      return strString.Substring(0,startPos);
    }
    public static DateTime ParseDate(String strDate)
    {
      logger.InfoFormat("Parsing date {0}",strDate);
      System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("en-US");
      String[] formats = new[] { "yyyy/MM/dd","M-d-yyyy", "dd-MM-yyyy", "MM-dd-yyyy", "M.d.yyyy", "dd.MM.yyyy", "MM.dd.yyyy","yyyyMMdd" }.Union(cultureInfo.DateTimeFormat.GetAllDateTimePatterns()).ToArray();
      strDate = strDate.Trim();
      DateTime dateTime=DateTime.ParseExact(strDate, formats, new System.Globalization.CultureInfo("en-US"), DateTimeStyles.AssumeLocal);
      return dateTime;
    }
    public static String AddQuotes(String item)
    {
      return "\"" + item + "\"";
    }
    public static String RemoveCC(String item)
    {
      StringBuilder sb = new StringBuilder();
      foreach (char ch in item)
      {
        if (!Char.IsControl(ch)) sb.Append(ch);
      }
      return sb.ToString();
    }
    public static String SqlString(String value)
    {
      StringBuilder sb = new StringBuilder();
      foreach (char ch in value)
      {
        sb.Append(ch);
        if (ch == '\'') sb.Append("'");
      }
      return sb.ToString();
    }
    public static String IsNull(String s1,String s2)
    {
      if (null == s1) return s2;
      return s1;
    }
    public static String NullIfOrCat(String s1,String s2,String s3)
    {
      if (string.IsNullOrEmpty(s1)) return s2;
      return s1+s3;
    }
    public static String Pad(string str, char filler, int length)
    {
      int stringLength = str.Length;
      if (stringLength >= length) return str;
      StringBuilder sb = new StringBuilder();

      while (stringLength < length)
      {
        sb.Append(filler);                                            
        stringLength++;
      }
      return sb.ToString() + str;
    } 
    public static String CreateInClause(String commaSepList,String dataType)
    {
      StringBuilder sb = new StringBuilder();
      sb.Append("(");
      String[] items = commaSepList.Split(',');
      for (int index = 0; index < items.Length; index++)
      {
        sb.Append("'").Append(items[index]).Append("'");
        if (index < items.Length - 1) sb.Append(",");
      }
      sb.Append(")");
      return sb.ToString();
    }
  }
}
