using System.Linq;
using System.Windows;
using System.Windows.Controls;

using BeWo.ViewModel.ListViewModel;

namespace BeWo.View
{
    public partial class VarFieldView : UserControl
    {
        public static readonly DependencyProperty VarFieldListProperty = DependencyProperty.Register(
            "VarFieldList", 
            typeof(VarFieldListVM), 
            typeof(VarFieldView), 
            new UIPropertyMetadata(
                (s, e) =>
                    {
                        var c = s as VarFieldView;
                        var d = e.NewValue as VarFieldListVM;

                        if (c != null && d != null)
                        {
                            c.lv_root.ItemsSource = d.VMList.GroupBy(vm => vm.Group).ToDictionary(g => new ItemKey { GroupName = g.Key, ColumnCount = g.Max(i => i.XCoordinate), RowCount = g.Max(i => i.YCoordinate) }, g => g.ToList());
                        }
                    }));

        public VarFieldView()
        {
            this.InitializeComponent();
        }

        public VarFieldListVM VarFieldList
        {
            get
            {
                return (VarFieldListVM)this.GetValue(VarFieldListProperty);
            }

            set
            {
                this.SetValue(VarFieldListProperty, value);
            }
        }

        public class ItemKey
        {
            public int ColumnCount { get; set; }

            public string GroupName { get; set; }

            public int RowCount { get; set; }
        }
    }
}