using C1.Xaml.FlexGrid;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace ControlExplorer.Controls.FlexGrid
{
    public sealed partial class Editing : UserControl
    {
        public Editing()
        {
            this.InitializeComponent();
            SetupGrid();
        }

        private void SetupGrid()
        {
            // create the data
            var data = Customer.GetCustomerList(100);
            flexgrid.ItemsSource = data;

            // 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;
            }
            var col = flexgrid.Columns["CountryID"];
            col.ValueConverter = new ColumnValueConverter(dct);
            col.HorizontalAlignment = HorizontalAlignment.Left;
            col.Header = "Country";
            col.Width = new GridLength(140);

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

            col = flexgrid.Columns["Hired"];
            col.Width = new GridLength(140);

            col = flexgrid.Columns["Weight"];
            col.Width = new GridLength(100);

            col = flexgrid.Columns["Active"];
            col.Width = new GridLength(100);


            // make read-only columns look disabled
            foreach (var c in flexgrid.Columns)
            {
                if (c.PropertyInfo != null && !c.PropertyInfo.CanWrite)
                {
                    c.Foreground = new SolidColorBrush(Colors.Gray);
                }
            }
        }
    }
}
