using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

using BS.Shared.DataContracts;
using BS.Shared.Extensions;

namespace BeWo.View.Detail.Report
{
    public partial class SupportConceptAnalysisListViewItem : UserControl
    {
        public static readonly DependencyProperty OverviewItemProperty = DependencyProperty.Register("OverviewItem", typeof(KeyValuePair<string, List<SupportConceptOverviewDC>>), typeof(SupportConceptAnalysisListViewItem), new UIPropertyMetadata(new KeyValuePair<string, List<SupportConceptOverviewDC>>(), (s, e) => ((SupportConceptAnalysisListViewItem)s).DataContext = e.NewValue));

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

        public KeyValuePair<string, List<SupportConceptOverviewDC>> OverviewItem
        {
            get
            {
                return (KeyValuePair<string, List<SupportConceptOverviewDC>>)this.GetValue(OverviewItemProperty);
            }

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

        private void ButtonOpenServiceRecordView_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                SupportConceptOverviewDC dc = btn.Tag as SupportConceptOverviewDC;

                if (dc != null)
                {
                    BeWoApp.MainControl.NavigateToServiceRecordView(dc.SupportConceptOid);
                }
            }
        }

        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 (this.listview.SelectedItem != null)
            {
                (this.listview.ItemContainerGenerator.ContainerFromItem(this.listview.SelectedItem) as ListViewItem).IsSelected = false;
            }
        }
    }
}