using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using Telerik.Web.UI;


public class GuidTestCollection 
{
    public int SelectCount()
    {
        NWDataContext nw = new NWDataContext();
        return nw.Tests.Count();
    }

    public List<GuidTest> Select()
    {
        List<GuidTest> newList = new List<GuidTest>();
        using (NWDataContext nw = new NWDataContext()) 
        {
            var result = nw.Tests.ToList();
            result.ForEach(r => newList.Add(new GuidTest
                                            {
                                                Id = r.Id,
                                                TestText = r.TestText,
                                                Guid = r.Guid.ToString()
                                            }));
            return newList;
        }
    }

    public List<GuidTest> Select(int startRowIndex, int maximumRows)
    {
        List<GuidTest> newList = new List<GuidTest>();
        using (NWDataContext nw = new NWDataContext())
        {
            var result = nw.Tests.Skip(startRowIndex).Take(maximumRows).ToList();
            result.ForEach(r => newList.Add(new GuidTest
            {
                Id = r.Id,
                TestText = r.TestText,
                Guid = r.Guid.ToString()
            }));
            return newList;
        }
    }
}

/// <summary>
/// Summary description for GuidTest
/// </summary>
public class GuidTest
{
    public GuidTest() { }

	public GuidTest(Guid _id, string _TestText, string _Guid) 
    {
        this.Id = _id;
        this.TestText = _TestText;
        this.Guid = _Guid;
    }

    public Guid Id
    {
        get;
        set;
    }

    public string TestText
    {
        get;
        set;
    }

    public string Guid
    {
        get;
        set;
    }
}

public class GridGenericSorter<T> : IComparer<T>
{
    GridSortExpression _expression = null;
    PropertyDescriptor _descriptor = null;

    public GridGenericSorter(GridSortExpression expression)
    {
        _descriptor = TypeDescriptor.GetProperties(GetType().GetGenericArguments()[0]).Find(expression.FieldName, true);
        _expression = expression;
    }

    public int Compare(T o1, T o2)
    {
        IComparable comparable1 = (IComparable)_descriptor.GetValue(o1);
        IComparable comparable2 = (IComparable)_descriptor.GetValue(o2);

        if (_expression.SortOrder == GridSortOrder.Ascending)
        {
            return comparable1.CompareTo(comparable2);
        }
        else
        {
            return comparable2.CompareTo(comparable1);
        }
    }
}

public class GridGenericFilterer<T>
{
    GridFilterExpression _expression = null;
    PropertyDescriptor _descriptor = null;
    Type _propertyType = null;
    GridKnownFunction _filterFunction;

    public GridGenericFilterer(GridFilterExpression expression)
    {
        _expression = expression;
        _descriptor = TypeDescriptor.GetProperties(GetType().GetGenericArguments()[0]).Find(expression.FieldName, true);
        _propertyType = _descriptor.PropertyType;
        _filterFunction = (GridKnownFunction)Enum.Parse(typeof(GridKnownFunction), _expression.FilterFunction);
    }

    public bool Filter(T item)
    {
        object value = _descriptor.GetValue(item);
        string stringValue = value.ToString();

        if (_propertyType == typeof(String))
        {
            if (_filterFunction == GridKnownFunction.Contains)
            {
                return stringValue.Contains(_expression.FieldValue);
            }
            else if (_filterFunction == GridKnownFunction.DoesNotContain)
            {
                return !stringValue.Contains(_expression.FieldValue);
            }
            else if (_filterFunction == GridKnownFunction.StartsWith)
            {
                return stringValue.StartsWith(_expression.FieldValue);
            }
            else if (_filterFunction == GridKnownFunction.EndsWith)
            {
                return stringValue.EndsWith(_expression.FieldValue);
            }
            else if (_filterFunction == GridKnownFunction.EqualTo)
            {
                return stringValue.Equals(_expression.FieldValue);
            }
            else if (_filterFunction == GridKnownFunction.NotEqualTo)
            {
                return !stringValue.Equals(_expression.FieldValue);
            }
            else if (_filterFunction == GridKnownFunction.IsEmpty)
            {
                return stringValue.Equals(String.Empty);
            }
            else if (_filterFunction == GridKnownFunction.NotIsEmpty)
            {
                return !stringValue.Equals(String.Empty);
            }
            else if (_filterFunction == GridKnownFunction.GreaterThan)
            {
                return stringValue.CompareTo(_expression.FieldValue) > 0;
            }
            else if (_filterFunction == GridKnownFunction.GreaterThanOrEqualTo)
            {
                return stringValue.CompareTo(_expression.FieldValue) >= 0;
            }
            else if (_filterFunction == GridKnownFunction.LessThan)
            {
                return stringValue.CompareTo(_expression.FieldValue) < 0;
            }
            else if (_filterFunction == GridKnownFunction.LessThanOrEqualTo)
            {
                return stringValue.CompareTo(_expression.FieldValue) <= 0;
            }
        }
        else if (_propertyType == typeof(Boolean))
        {
            if (_filterFunction == GridKnownFunction.EqualTo)
            {
                return Boolean.Parse(stringValue).Equals(Boolean.Parse(_expression.FieldValue));
            }
            else if (_filterFunction == GridKnownFunction.NotEqualTo)
            {
                return !Boolean.Parse(stringValue).Equals(Boolean.Parse(_expression.FieldValue));
            }
        }
        else if (_propertyType == typeof(DateTime))
        {
            if (_filterFunction == GridKnownFunction.EqualTo)
            {
                return DateTime.Parse(stringValue).Equals(DateTime.Parse(_expression.FieldValue));
            }
            else if (_filterFunction == GridKnownFunction.NotEqualTo)
            {
                return !DateTime.Parse(stringValue).Equals(DateTime.Parse(_expression.FieldValue));
            }
            else if (_filterFunction == GridKnownFunction.GreaterThan)
            {
                return DateTime.Parse(stringValue).CompareTo(DateTime.Parse(_expression.FieldValue)) > 0;
            }
            else if (_filterFunction == GridKnownFunction.GreaterThanOrEqualTo)
            {
                return DateTime.Parse(stringValue).CompareTo(DateTime.Parse(_expression.FieldValue)) >= 0;
            }
            else if (_filterFunction == GridKnownFunction.LessThan)
            {
                return DateTime.Parse(stringValue).CompareTo(DateTime.Parse(_expression.FieldValue)) < 0;
            }
            else if (_filterFunction == GridKnownFunction.LessThanOrEqualTo)
            {
                return DateTime.Parse(stringValue).CompareTo(DateTime.Parse(_expression.FieldValue)) <= 0;
            }
        }
        else if (IsNumeric(_propertyType))
        {
            if (_filterFunction == GridKnownFunction.EqualTo)
            {
                return Decimal.Parse(stringValue).Equals(Decimal.Parse(_expression.FieldValue));
            }
            else if (_filterFunction == GridKnownFunction.NotEqualTo)
            {
                return !Decimal.Parse(stringValue).Equals(Decimal.Parse(_expression.FieldValue));
            }
            else if (_filterFunction == GridKnownFunction.GreaterThan)
            {
                return Decimal.Parse(stringValue).CompareTo(Decimal.Parse(_expression.FieldValue)) > 0;
            }
            else if (_filterFunction == GridKnownFunction.GreaterThanOrEqualTo)
            {
                return Decimal.Parse(stringValue).CompareTo(Decimal.Parse(_expression.FieldValue)) >= 0;
            }
            else if (_filterFunction == GridKnownFunction.LessThan)
            {
                return Decimal.Parse(stringValue).CompareTo(Decimal.Parse(_expression.FieldValue)) < 0;
            }
            else if (_filterFunction == GridKnownFunction.LessThanOrEqualTo)
            {
                return Decimal.Parse(stringValue).CompareTo(Decimal.Parse(_expression.FieldValue)) <= 0;
            }
        }

        if (_filterFunction == GridKnownFunction.IsNull)
        {
            return value.Equals(null);
        }
        else if (_filterFunction == GridKnownFunction.NotIsNull)
        {
            return !value.Equals(null);
        }

        return true;
    }

    static bool IsNumeric(Type type)
    {
        if (!type.IsEnum)
        {
            switch (Type.GetTypeCode(type))
            {
                case TypeCode.Char:
                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                case TypeCode.UInt64:
                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                    return true;
            }
        }
        return false;
    }
}
