using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Data;
using C1.Silverlight.CollectionView;

namespace CollectionViewDemo2010
{

    public class TestData : INotifyPropertyChanged
    {
        private string _description;

        public TestData()
        {
        }

        public TestData(TestData otherData): this()
        {
            _description = otherData.Description;
            _id = otherData.ID;
            _company = otherData.Company;
            _location = otherData.Location;
        }

        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                OnPropertyChanged("Description");
            }
        }
        private string _id;
        public string ID
        {
            get { return _id; }
            set
            {
                _id = value;
                OnPropertyChanged("ID");
            }
        }
        private string _company;
        public string Company
        {
            get { return _company; }
            set
            {
                _company = value;
                OnPropertyChanged("Company");
            }
        }
        private string _location;
        public string Location
        {
            get { return _location; }
            set
            {
                _location = value;
                OnPropertyChanged("Location");
            }
        }
        public void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        static List<TestData> data = null;
        private static List<TestData> GetList(int itemCount)
        {
            if (data != null && data.Count == itemCount)
            {
                List<TestData> ret = new List<TestData>();
                foreach (TestData curData in data)
                    ret.Add(new TestData(curData));
                return ret;
            }
            data = new List<TestData>();
            for (int i = 0; i < itemCount; i++)
            {
                if (i < itemCount * 0.2)
                {
                    data.Add(new TestData() { ID = "ID" + i, Description = "Desc", Company = "TR" + i, Location = "Loc" + i });
                }
                else if (i < itemCount * 0.4)
                {
                    data.Add(new TestData() { ID = "ID", Description = "Desc" + i, Company = "TR" + i, Location = "Loc" });
                }
                else if (i < itemCount * 0.6)
                {
                    data.Add(new TestData() { ID = "iD", Description = "Desc" + i, Company = "TR" + i, Location = "Loc" + i });
                }
                else if (i < itemCount * 0.8)
                {
                    data.Add(new TestData() { ID = "iD", Description = "Desc" + i, Company = "TRI" + i, Location = "Loc" + i });
                }
                else
                {
                    data.Add(new TestData() { ID = "ID" + i, Description = "Desc" + i, Company = "TR", Location = "Loc" + i });
                }
            }

            return data;
        }

        public static ICollectionView GetData(int itemCount)
        {
            var cvs = new CollectionViewSource();
            cvs.Source = GetList(itemCount);
            var view = cvs.View;
            //SetupCV(view);
            var g = view.GroupDescriptions;

            return view;
        }

//#if C1WPF
        public static ICollectionView GetC1Data(int itemCount)
        {
            ICollectionView view = new C1CollectionView(GetList(itemCount));
            //SetupCV(view);

            return view;
        }

        public static void SetupCV(ICollectionView view, IList<string> properties)
        {
            using (IDisposable d = view.DeferRefresh())
            {
                var g = view.GroupDescriptions;
                var s = view.SortDescriptions;
                s.Clear();
                g.Clear();
                foreach (string prop in properties)
                {
                    g.Add(new PropertyGroupDescription(prop));
                    s.Add(new SortDescription(prop, ListSortDirection.Ascending));
                }
            }
        }
//#endif
    }
}
