#region COPYRIGHT
//
//     THIS IS GENERATED BY TEMPLATE
//     
//     AUTHOR  :     ROYE
//     DATE       :     2010
//
//     COPYRIGHT (C) 2010, TIANXIAHOTEL TECHNOLOGIES CO., LTD. ALL RIGHTS RESERVED.
//
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TX.Framework.WindowUI.Controls
{
    public abstract class ListViewSorter
    {
        public int OnSort(object x, object y, SortOrder sortOrder)
        {
            ListViewItem.ListViewSubItem left = GetLeft(x);
            ListViewItem.ListViewSubItem right = GetLeft(y);

            int result = HandleEmptyStrings(left, right, sortOrder);
            if (result != 0)
            {
                return result;
            }

            result = OnInnerSort(left, right, sortOrder);
            if (sortOrder == SortOrder.Descending)
            {
                result *= -1;
            }
            return result;
        }

        protected virtual int OnInnerSort(ListViewItem.ListViewSubItem left, ListViewItem.ListViewSubItem right, SortOrder sortOrder)
        {
            throw new NotImplementedException();
        }

        public ListViewItem.ListViewSubItem GetLeft(object x)
        {
            ListViewItem.ListViewSubItem item = x as ListViewItem.ListViewSubItem;
            return item;
        }

        public ListViewItem.ListViewSubItem GetRight(object y)
        {
            ListViewItem.ListViewSubItem item = y as ListViewItem.ListViewSubItem;
            return item;
        }

        protected int HandleEmptyStrings(ListViewItem.ListViewSubItem left, ListViewItem.ListViewSubItem right, SortOrder sortOrder)
        {
            if (sortOrder == SortOrder.Ascending)
            {
                if (left.Text == string.Empty)
                {
                    return -1;
                }
                if (right.Text == string.Empty)
                {
                    return 1;
                }
            }
            else if (sortOrder == SortOrder.Descending)
            {
                if (left.Text == string.Empty)
                {
                    return 1;
                }
                if (right.Text == string.Empty)
                {
                    return -1;
                }
            }
            return 0;
        }
    }

    public class ListViewInt32Sorter : ListViewSorter
    {
        protected override int OnInnerSort(ListViewItem.ListViewSubItem left, ListViewItem.ListViewSubItem right, SortOrder sortOrder)
        {
            return left.Text.ToInt32() - right.Text.ToInt32();
        }
    }

    public class ListViewDateSorter : ListViewSorter
    {
        protected override int OnInnerSort(ListViewItem.ListViewSubItem left, ListViewItem.ListViewSubItem right, SortOrder sortOrder)
        {
            return left.Text.ToDateTime().CompareTo(right.Text.ToDateTime());
        }
    }

    public class ListViewStringSorter : ListViewSorter
    {
        protected override int OnInnerSort(ListViewItem.ListViewSubItem left, ListViewItem.ListViewSubItem right, SortOrder sortOrder)
        {
            return String.Compare(left.Text, right.Text, true);
        }
    }

}
