using C1.Xaml.FlexGrid;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace FlexGridSamples
{
    public partial class Unbound : Page
    {
        public Unbound()
        {
            InitializeComponent();

            // populate unbound grid
            Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(PopulateUnboundGrid));

            // store sample description in Tag property
            Tag =
                "In addition to working with IEnumerable data sources, the C1FlexGrid also supports " +
                "an unbound mode, where cell values are stored in the grid itself and are accessed " +
                "using familiar indexing notation. This demo also shows how you can set up merged multi-cell " +
                "row and column headers.";
        }

        // fill unbound grid with data
        private void PopulateUnboundGrid()
        {
            // allow merging
            var fg = _flexUnbound;
            fg.AllowMerging = AllowMerging.All;

            // add rows/columns to the unbound grid
            for (int i = 0; i < 12; i++) // three years, four quarters per year
            {
                fg.Columns.Add(new Column());
            }
            for (int i = 0; i < 500; i++)
            {
                fg.Rows.Add(new Row());
            }

            // populate the unbound grid with some stuff
            for (int r = 0; r < fg.Rows.Count; r++)
            {
                for (int c = 0; c < fg.Columns.Count; c++)
                {
                    fg[r, c] = string.Format("cell [{0},{1}]", r, c);
                }
            }

            // add some group rows above the data
            for (int offset = 0; offset < fg.Rows.Count - 10; offset += 10)
            {
                for (int i = 0; i < 3; i++)
                {
                    fg.Rows.Insert(offset + i, new GroupRow() { Level = i });
                    fg[offset + i, 0] = string.Format("level {0}", i);
                }
            }

            // set unbound column headers
            var ch = fg.ColumnHeaders;
            ch.Rows.Add(new Row()); // one header row for years, one for quarters
            for (int c = 0; c < ch.Columns.Count; c++)
            {
                ch[0, c] = 2009 + c / 4; // year
                ch[1, c] = string.Format("Q {0}", c % 4 + 1); // quarter
            }

            // allow merging the first fixed row
            ch.Rows[0].AllowMerging = true;

            // set unbound row headers
            var rh = fg.RowHeaders;
            rh.Columns.Add(new Column());
            for (int c = 0; c < rh.Columns.Count; c++)
            {
                rh.Columns[c].Width = new GridLength(60);
                for (int r = 0; r < rh.Rows.Count; r++)
                {
                    rh[r, c] = string.Format("hdr {0},{1}", c == 0 ? r / 2 : r, c);
                }
            }

            // allow merging the first fixed column
            rh.Columns[0].AllowMerging = true;

            // listen to column resized event
            _flexUnbound.ResizedColumn += _flexUnbound_ResizedColumn;
        }

        // make all unbound grid columns the same width after they are resized
        private void _flexUnbound_ResizedColumn(object sender, CellRangeEventArgs e)
        {
            var col = _flexUnbound.Columns[e.Column];
            if (col.ActualWidth > 0)
            {
                _flexUnbound.Columns.DefaultSize = col.ActualWidth;
            }
            col.Width = new GridLength(0, GridUnitType.Auto);
        }
    }
}
