using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;
using BeWo.ServiceProxy;

namespace BeWo.View.Controls
{
    /// <summary>
    /// Interaktionslogik für MultiSupportConceptSelectionControl.xaml
    /// </summary>
    public partial class MultiSupportConceptSelectionControl
    {
        public event EventHandler EditGroupButtonClicked;

        private ObservableSortCollection<CompactSupportConceptDC> mSelectedSupportConcepts;

        public MultiSupportConceptSelectionControl()
        {
            this.InitializeComponent();

            this.UpdateListBox();
            GroupSearchView.EditGroupButtonClicked += new EventHandler(GroupSearchView_EditGroupButtonClicked);
        }

        void GroupSearchView_EditGroupButtonClicked(object sender, EventArgs e)
        {
            if (EditGroupButtonClicked != null)
            {
                EditGroupButtonClicked(sender, e);
                popupGroup.IsOpen = false;
            }
        }

        public virtual bool AutoGetAllAfterLoaded
        {
            get { return this.SearchView != null && SearchView.AutoGetAllAfterLoaded; }
            set
            {
                SearchView.AutoGetAllAfterLoaded = value;
                GroupSearchView.AutoGetAllAfterLoaded = value;
            }
        }

        public List<CompactSupportConceptDC> SupportConceptList
        {
            get { return this.SearchView != null ? SearchView.SupportConcepts : null; }
            set { SearchView.SupportConcepts = value; }
        }

        public event EventHandler<EventArgs<CompactSupportConceptDC>> ItemSelected;

        public CompactSupportConceptDC SelectedItem
        {
            get
            {
                return this.listBoxSupportConcepts != null
                           ? this.listBoxSupportConcepts.SelectedItem as CompactSupportConceptDC
                           : null;
            }
        }

        public ObservableSortCollection<CompactSupportConceptDC> SelectedSupportConcepts
        {
            get
            {
                if (this.mSelectedSupportConcepts == null)
                {
                    this.mSelectedSupportConcepts = new ObservableSortCollection<CompactSupportConceptDC>();
                    this.mSelectedSupportConcepts.CollectionChanged += this.mSelectedSupportConcepts_CollectionChanged;
                }

                return this.mSelectedSupportConcepts;
            }
        }

        private void SupportConceptSearchView_ItemSelected(object sender, EventArgs<CompactSupportConceptDC> e)
        {
            this.popupSupportConcepts.IsOpen = false;
            if (this.SelectedSupportConcepts != null && !this.SelectedSupportConcepts.Contains(e.Data))
            {
                this.SelectedSupportConcepts.Add(e.Data);

                this.listBoxSupportConcepts.ScrollIntoView(e.Data);
            }
        }

        private void GroupSearchView_ItemSelected(object sender, EventArgs<CompactGroupOfPeopleDC> e)
        {
            this.popupGroup.IsOpen = false;


            if (this.SelectedSupportConcepts != null)
            {
                ServiceFacade.DoEmployeeServiceAsync(s => s.LoadGroupOfPeople(e.Data.GroupOfPeopleOid),
                    cb => this.Dispatch(delegate
                                           {
                                               if (cb.SupportConceptList != null)
                                               {
                                                   foreach (var sc in cb.SupportConceptList)
                                                   {
                                                       if (!this.SelectedSupportConcepts.Contains(sc))
                                                       {
                                                           this.SelectedSupportConcepts.Add(sc);
                                                       }
                                                   }
                                               }

                                           }));
            }
        }

        internal void UpdateListBox()
        {
            if (listBoxSupportConcepts == null) 
                return;

            listBoxSupportConcepts.ItemsSource = null;
            listBoxSupportConcepts.ItemsSource = SelectedSupportConcepts;
        }

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

        private void btnAddGroup_Click(object sender, RoutedEventArgs e)
        {

            ServiceFacade.DoEmployeeServiceAsync(s => s.GetAllGroupsCompact(BeWoApp.LoggedOnEmployee.EmployeeOid),
                r =>
                {
                    List<CompactGroupOfPeopleDC> list =
                        r.Where(g => g.GroupType == GroupTypeId.ServiceRecordSupportConcepts).ToList();


                    list.Sort((x, y) => x.Name.CompareTo(y.Name));
                    this.Dispatch(delegate
                        {
                            GroupSearchView.GroupOfPeoples = list;        
                        });
                    

                });

            this.popupGroup.IsOpen = true;

        }

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

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

        private void listBoxSupportConcepts_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CompactSupportConceptDC dc = null;
            if (e.AddedItems != null && e.AddedItems.Count > 0)
            {
                dc = e.AddedItems[0] as CompactSupportConceptDC;
            }

            if (this.ItemSelected != null)
            {
                this.ItemSelected(this, new EventArgs<CompactSupportConceptDC>(dc));
            }
        }

        private void mSelectedSupportConcepts_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (this.SearchView != null)
                this.SearchView.HiddenSupportConcepts = this.mSelectedSupportConcepts.ToList();
        }

        public void UpdateSupportConcepts(CompactCustomerDC customer)
        {
            var oldList = mSelectedSupportConcepts;

            foreach (var item in oldList.Where(item => item.Customer.Equals(customer)))
                item.Customer = customer;

            mSelectedSupportConcepts = oldList;
        }
    }
}