using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace MainTestApplication
{
    public partial class Financial : UserControl
    {
        FinancialDataList _financialData;

        public Financial()
        {
            InitializeComponent();
            // populate financial grid
            Dispatcher.BeginInvoke(PopulateFinancialGrid);
            // store sample description in Tag property
            Tag =
                "This demo shows a financial application with real time data updates, " +
                "flashing cells, and sparklines. It highlights C1FlexGrid's performance. " +
                "Check the Custom Cells checkbox to see how we display flashes and sparklines " +
                "for a better user experience. This is accomplished using the C1FlexGrid's " +
                "CellFactory interface for easily customizing cells.";
        }

        private void PopulateFinancialGrid()
        {
            _financialData = FinancialData.GetFinancialData();
            var view = new PagedCollectionView(_financialData);
            _flexFinancial.ItemsSource = view;
            _flexFinancial.Columns.Frozen = 1;
            _flexFinancial.Columns[0].AllowDragging = false;

            // configure search box
            _srchCompanies.View = view;
            var props = _srchCompanies.FilterProperties;
            props.Add(typeof(FinancialData).GetProperty("Name"));
            props.Add(typeof(FinancialData).GetProperty("Symbol"));

            // show company info
            UpdateCompanyStatus();
            view.CollectionChanged += financial_CollectionChanged;
        }

        private void financial_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            UpdateCompanyStatus();
        }

        // update song status
        private void UpdateCompanyStatus()
        {
            var view = _flexFinancial.ItemsSource as ICollectionView;
            var companies = view.OfType<FinancialData>();
            _txtCompanies.Text = string.Format("{0:n0} companies selected.",
                (from c in companies select c.Symbol).Distinct().Count());
        }

        // control update frequency
        private void _chkAutoUpdate_Click(object sender, RoutedEventArgs e)
        {
            _financialData.AutoUpdate = ((CheckBox)sender).IsChecked.Value;
        }

        private void _cmbUpdateInterval_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_financialData != null)
            {
                var cmb = sender as ComboBox;
                var val = ((ComboBoxItem)cmb.SelectedItem).Content as string;
                val = val.Split(' ')[0].Trim();
                _financialData.UpdateInterval = int.Parse(val);
            }
        }

        private void _cmbBatchSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_financialData != null)
            {
                var cmb = sender as ComboBox;
                var val = ((ComboBoxItem)cmb.SelectedItem).Content as string;
                val = val.Split(' ')[0].Trim();
                _financialData.BatchSize = int.Parse(val);
            }
        }

        private void _chkOwnerDrawFinancial_Click(object sender, RoutedEventArgs e)
        {
            _flexFinancial.CellFactory = ((CheckBox)sender).IsChecked.Value
                ? new FinancialCellFactory()
                : null;
        }
    }
}
