using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

namespace ms.aq.cview.util {
    public class StringTypeException:Exception{}
    //bottom-level methods picked off from various places
    
    public abstract class common {
        public const string XDIM_PREFIX = "XDIM_";
        public const string YDIM_PREFIX = "YDIM_";
        public const string LIMIT_PREFIX = "_LIMPET";

        public static string debracket(string s) {
            return s.Trim().Trim(']').Trim('[').Trim('(').Trim(')');
        }
        //series # from column alias
        public static int sNumFromCAlias(string al) {
            int last_ = al.LastIndexOf('_');
            return int.Parse(al.Substring(last_+1, al.Length - last_-1));
        }

        public static string sqlifyCondition(string s) {
            /*
             * format for between is LIMIT_PREF<n1>|<n2>
             */
            if (s.StartsWith(LIMIT_PREFIX)) {
                s = s.Replace(LIMIT_PREFIX, "");
                string[] vals = s.Split('|');
                return " between " + vals[0] + " and " + vals[1] + " ";
            }
            else
                return " = " + sqlifyValue(s);
        }
        public static string sqlifyValue(string s) {
            try {
                double.Parse(s);
                return s;
            } catch {
                return "'" + s + "'";
            }
        }
        public static long FloatToTicks(double f) {
            return DateTime.FromOADate(f).Ticks;
        }
        
        public static float ObjToFloat(object obj) {
            switch (obj.GetType().Name) {
                case "Double":
                    return (float)((double)obj);
                case "DateTime":
                    DateTime dt = (DateTime)obj;
                    return (float)dt.ToOADate();
                case "Int32":
                    return (float)((int)obj);
                case "Int64":
                    return (float)((long)obj);
                case "Decimal":
                    return float.Parse(obj.ToString());
                case "DBNull":
                    return Settings.AXI_UNDEF;
                case "String":
                    throw new StringTypeException();
                default:
                    try {
                        return (float)obj;
                    } catch {
                        throw new Exception("unknown type " + obj.GetType().Name);
                    }
            }
        }
        public static StringCollection UID2Tokens(string uid) {
            string cpy = uid;
            StringCollection sc = new StringCollection();
            int i;
            while ((i = cpy.IndexOf("].[")) > -1) {
                string token = cpy.Substring(0, i + 1);
                cpy = cpy.Substring(token.Length+1, cpy.Length - token.Length-1);
                sc.Add(token);
            }
            sc.Add(cpy);
            return sc;
        }

        public static string SqlAggLookup(AggregateFunctions agg) {
            switch (agg) {
                case AggregateFunctions.Avg: return "avg";
                case AggregateFunctions.Count: return "count";
                case AggregateFunctions.Max: return "max";
                case AggregateFunctions.Sum: return "sum";
                case AggregateFunctions.Min: return "min";
                default: throw new Exception("Never here");
            }
        }
        public static AggregateFunctions StringToAgg(string s) {
            switch (s) {
                case "Avg": return AggregateFunctions.Avg;
                case "Count": return AggregateFunctions.Count;
                case "Max": return AggregateFunctions.Max;
                case "Sum": return AggregateFunctions.Sum;
                case "Min": return AggregateFunctions.Min;
                default: throw new Exception("Never here");
            }
        }
    }
}
