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

using BS.Shared.Core;
using BS.Shared.DataContracts;

namespace BeWo.View.Controls
{
    /// <summary>
    /// Interaktionslogik für AdditionalServiceGroupOfPeopleSelectionControl.xaml
    /// </summary>
    public partial class AdditionalServiceGroupOfPeopleSelectionControl : UserControl
    {
        private AdditionalServiceGroupOfPeopleDC mSelectedAdditionalServiceGroupOfPeopleDC;

        public AdditionalServiceGroupOfPeopleSelectionControl()
        {
            InitializeComponent();
            SearchView.ControlInitialized += SearchView_ControlInitialized;
        }

        void SearchView_ControlInitialized(object sender, EventArgs e)
        {
            if (ControlInitialized != null)
                ControlInitialized(this, new EventArgs());
        }

        public event EventHandler ControlInitialized;

        public event EventHandler<EventArgs<AdditionalServiceGroupOfPeopleDC>> ItemSelected;

        public AdditionalServiceGroupOfPeopleDC SelectedItem
        {
            get { return mSelectedAdditionalServiceGroupOfPeopleDC; }
            set
            {
                mSelectedAdditionalServiceGroupOfPeopleDC = value;
                UpdateControls();
            }
        }

        public string SearchText
        {
            get { return SearchView.SearchText; }
            set { SearchView.SearchText = value; }
        }

        private void AdditionalServiceGroupOfPeopleSearchView_ItemSelected(object sender, EventArgs<AdditionalServiceGroupOfPeopleDC> e)
        {
            popupAdditionalServiceGroupsOfPeople.IsOpen = false;
            SetSelectedAdditionalServiceGroupOfPeople(e.Data);
        }

        private void SetSelectedAdditionalServiceGroupOfPeople(AdditionalServiceGroupOfPeopleDC dc)
        {
            mSelectedAdditionalServiceGroupOfPeopleDC = dc;

            UpdateControls();

            if (ItemSelected != null)
                ItemSelected(this, new EventArgs<AdditionalServiceGroupOfPeopleDC>(mSelectedAdditionalServiceGroupOfPeopleDC));
        }

        private void UpdateControls()
        {
            img.Visibility = mSelectedAdditionalServiceGroupOfPeopleDC == null ? Visibility.Collapsed : Visibility.Visible;
            if (ItemContent != null)
                ItemContent.DataContext = mSelectedAdditionalServiceGroupOfPeopleDC;
        }

        private void btnSelectAdditionalServiceGroupOfPeople_Click(object sender, RoutedEventArgs e)
        {
            popupAdditionalServiceGroupsOfPeople.IsOpen = true;
        }

        internal void SetSelectedAdditionalServiceGroupOfPeopleOid(long pOid)
        {
            var items = SearchView.AdditionalServiceGroupsOfPeople;

            if (items == null) 
                return;

            AdditionalServiceGroupOfPeopleDC found = null;

            foreach (var sc in items.Where(sc => found == null).Where(sc => sc != null && sc.AdditionalServiceGroupOfPeopleOid == pOid))
                found = sc;

            if (found != null)
                SetSelectedAdditionalServiceGroupOfPeople(found);
        }
    }
}
