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

using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;

using BeWo.Core;
using BeWo.ServiceProxy;
using BeWo.ViewModel;

namespace BeWo.View.Controls
{
    /// <summary>
    /// Interaktionslogik für MultiEmployeeSelectionControl.xaml
    /// </summary>
    public partial class MultiEmployeeSelectionControl : UserControl
    {
        private ObservableSortCollection<CompactEmployeeDC> mSelectedEmployees;

        public MultiEmployeeSelectionControl()
        {
            this.InitializeComponent();

            this.UpdateListBox();
        }

        public ObservableSortCollection<CompactEmployeeDC> SelectedEmployees
        {
            get 
            {
                return mSelectedEmployees ?? (mSelectedEmployees = new ObservableSortCollection<CompactEmployeeDC>());
            }

            set { mSelectedEmployees = value; }
        }

        private void EmployeeSearchView_ItemSelected(object sender, EventArgs<CompactEmployeeDC> e)
        {
            this.popupEmployees.IsOpen = false;
            if (!this.SelectedEmployees.Contains(e.Data))
            {
                this.SelectedEmployees.Add(e.Data);

                this.listBoxEmployees.SelectedItem = e.Data;
                this.listBoxEmployees.ScrollIntoView(e.Data);
            }
        }

        internal void UpdateListBox()
        {
            if (this.listBoxEmployees != null)
                this.listBoxEmployees.ItemsSource = this.SelectedEmployees;
        }

        private void btnAddEmployee_Click(object sender, RoutedEventArgs e)
        {
            this.popupEmployees.IsOpen = true;
        }

        private void btnRemoveEmployee_Click(object sender, RoutedEventArgs e)
        {
            CompactEmployeeDC dc = ((Button)sender).Tag as CompactEmployeeDC;

            if (dc != null)
            {
                this.SelectedEmployees.Remove(dc);
            }
        }

        private void DoubleClickHandler(object sender, MouseButtonEventArgs e)
        {
            var dc = ((ListBoxItem)sender).Content as CompactEmployeeDC;

            if (dc == null || !BeWoApp.LoggedOnUser.HasRight(UserRightType.EmployeeView_View))
                return;

            VMFactory.CreateEmployeeVMAsync(dc.EmployeeOid,
                    cb => this.Dispatch(() => BeWoUtils.OpenModalViewWindow<EmployeeVM, EmployeeDC, BeWoView>(cb, null, () => this.Dispatch(
                            delegate
                                {
                                    var updatedEmployee = new CompactEmployeeDC();
                                    ServiceFacade.DoEmployeeServiceSync(s => updatedEmployee = s.LoadCompactEmployee(dc.EmployeeOid));

                                    var newList = new ObservableSortCollection<CompactEmployeeDC>();
                                    foreach (var old in mSelectedEmployees)
                                        newList.Add(old.Equals(updatedEmployee) ? updatedEmployee : old);

                                    mSelectedEmployees = null;
                                    mSelectedEmployees = newList;

                                    UpdateListBox();

                                    BeWoUtils.UpdateAllViews(dc);
                                }
                        ))));
        }
    }
}