using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BeWo.ServiceProxy;
using BS.Shared.Extensions;
using System.Windows.Threading;

namespace BeWo.View.Report.MitarbeiterKoelnRing
{
    public partial class FLSOverviewItem : UserControl
    {
        public FLSOverviewRow OverviewRow
        {
            get { return (FLSOverviewRow)GetValue(OverviewRowProperty); }
            set { SetValue(OverviewRowProperty, value); }
        }

        public static readonly DependencyProperty OverviewRowProperty =
            DependencyProperty.Register("OverviewRow", 
                                        typeof(FLSOverviewRow), 
                                        typeof(FLSOverviewItem),
                                        new UIPropertyMetadata(new FLSOverviewRow(), new PropertyChangedCallback(OverviewRowChanged)));

        public FLSOverviewItem()
        {
            InitializeComponent();
            //itemListView.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
        }

        private static void OverviewRowChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            FLSOverviewItem item = (FLSOverviewItem)obj;
            FLSOverviewRow row = (FLSOverviewRow)e.NewValue;
            item.InitItemSource(row);
            
        }

        internal void InitItemSource(FLSOverviewRow row)
        {
            if (itemListView.ItemsSource == null)
                itemListView.ItemsSource = row.DetailRows;

        }


        private void Expander_Collapsed(object sender, RoutedEventArgs e)
        {
            var lExpander = (Expander)sender;
            var lPanel = lExpander.GetVisualParent<VirtualizingStackPanel>();
            if (lPanel != null)
            {
                lExpander.Dispatch(delegate
                {
                    ScrollViewer lViewer = lPanel.ScrollOwner;
                    lPanel.ScrollOwner = null;
                    lPanel.ScrollOwner = lViewer;
                    lPanel.InvalidateMeasure();
                });
            }
        }

        private void listview_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (itemListView.SelectedItem != null)
                (itemListView.ItemContainerGenerator.ContainerFromItem(itemListView.SelectedItem)
                    as ListViewItem).IsSelected = false;
        }
    }
}

