using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using C1.Silverlight.FlexGrid;

namespace MainTestApplication
{
    public partial class Editing : UserControl
    {
        public Editing()
        {
            InitializeComponent();
            // populate edit grid
            Dispatcher.BeginInvoke(PopulateEditGrid);
            // store sample description in Tag property
            Tag = 
                "The C1FlexGrid supports Excel-style editing. Just type values into cells and " +
                "use the enter or arrow keys to move to the next one. Or press F2 and enter full-edit " +
                "mode, where arrow keys navigate within the editor. Auto-complete and value-mapped " +
                "columns are also built-in: simply create a ColumnValueConverter and assign it to " +
                "the columns ValueConverter property and you're done. Transactioned edits are " +
                "supported too. The Customer class used here implements IEditableObject, so you can " +
                "undo changes by pressing the Escape key before you move to a new row. (Note: the " +
                "gray columns are read-only).";
        }

        private void PopulateEditGrid()
        {
            // create the data
            var data = Customer.GetCustomerList(100);
            var view = new PagedCollectionView(data);
            _flexEdit.ItemsSource = view;

            // hide read-only "Country" column 
            var col = _flexEdit.Columns["Country"];
            col.Visible = false;

            // map countryID column so it shows country names instead of their IDs
            Dictionary<int, string> dct = new Dictionary<int, string>();
            foreach (var country in Customer.GetCountries())
            {
                dct[dct.Count] = country;
            }
            col = _flexEdit.Columns["CountryID"];
            col.ValueConverter = new ColumnValueConverter(dct);
            col.HorizontalAlignment = HorizontalAlignment.Left;
            col.Width = new GridLength(120);

            // provide auto-complete lists for first and last name columns
            col = _flexEdit.Columns["First"];
            col.ValueConverter = new ColumnValueConverter(Customer.GetFirstNames(), false);
            col = _flexEdit.Columns["Last"];
            col.ValueConverter = new ColumnValueConverter(Customer.GetLastNames(), false);

            // make read-only columns look disabled
            var readOnlyBrush = new SolidColorBrush(Color.FromArgb(0xe0, 0xe0, 0xe0, 0xe0));
            foreach (var c in _flexEdit.Columns)
            {
                if (c.PropertyInfo != null && !c.PropertyInfo.CanWrite)
                {
                    c.Background = readOnlyBrush;
                }
            }
        }
    }
}
