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

using BeWo.View.Master;

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

namespace BeWo.View.Report.Finance
{
    public partial class OutstandingReceivablesListViewItem : UserControl
    {
        public static readonly DependencyProperty OverviewItemProperty = DependencyProperty.Register("OverviewItem", typeof(FinanceOverviewItemDC), typeof(OutstandingReceivablesListViewItem), new UIPropertyMetadata(new FinanceOverviewItemDC(), (s, e) => ((OutstandingReceivablesListViewItem)s).DataContext = e.NewValue));

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

        public FinanceOverviewItemDC OverviewItem
        {
            get
            {
                return (FinanceOverviewItemDC)this.GetValue(OverviewItemProperty);
            }

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

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

                if (dc != null && dc.CostBearer != null)
                {
                    FinanceView view = BeWoApp.MainControl.ActiveView as FinanceView;

                    view.OpenBookingForCostBearer2SupportConcept(dc.CostBearer.CostBearer2SupportConceptOid);
                }
            }
        }

        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;
            }
        }
    }
}