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

using BeWo.ServiceProxy;
using BeWo.ViewModel;

using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;
using BeWo.ViewModel.ListViewModel;

namespace BeWo.View.Detail
{
    /// <summary>
    /// Interaction logic for GroupView.xaml
    /// </summary>
    public partial class GroupOfPeopleView : BeWoView
    {
        private GroupOfPeopleListVM _ViewModel;

        public GroupOfPeopleView(GroupOfPeopleListVM pViewModel)
        {
            this.InitializeComponent();
            this.ViewModel = pViewModel;
        }

        public event EventHandler<EventArgs<GroupOfPeopleListVM>> GroupOfPeopleSavedOrUpdated;

        public override bool IsDirty
        {
            get
            {
                return this.ViewModel != null && this.ViewModel.IsDirty;
            }
        }

        public override string Title
        {
            get
            {
                return "Gruppe";
            }
        }

        private GroupOfPeopleListVM ViewModel
        {
            get
            {
                return this._ViewModel;
            }

            set
            {
                this._ViewModel = value;
                this.DataContext = value;
            }
        }

        protected override UserRightType[] GetSaveDemands()
        {
            return new[] { UserRightType.EditAll, UserRightType.GroupOfPeopleView_Edit };
        }

        protected override void Save()
        {
            foreach (var vm in this.ViewModel.VMList)
            {
                var dc = vm.CommitToDataContract();
                if (vm.IsNew)
                {
                    ServiceFacade.DoEmployeeServiceSync(s => s.InsertNewGroupOfPeople(dc));
                }
                else if (vm.IsDirty)
                {
                    ServiceFacade.DoEmployeeServiceSync(s => s.UpdateGroupOfPeople(dc));
                }    
            }
            if (ViewModel.RemovedCount  > 0)
            {
                foreach (var dc in ViewModel.GetRemoved())
                {
                    if (dc.GroupOfPeopleOid.HasValue)
                        ServiceFacade.DoEmployeeServiceSync(s => s.DeactivateGroupOfPeople(dc.GroupOfPeopleOid.Value, dc.GroupOfPeopleVersion.Value));
                }
            }

            if (this.GroupOfPeopleSavedOrUpdated != null)
            {
                this.GroupOfPeopleSavedOrUpdated(this, new EventArgs<GroupOfPeopleListVM>(ViewModel));
            }
        }

        private void ReloadViewModel(long pOid)
        {
            VMFactory.CreateGroupOfPeopleListVMAsync(
                cb => this.Dispatch(
                    delegate
                    {
                        if (this.GroupOfPeopleSavedOrUpdated != null)
                        {
                            this.GroupOfPeopleSavedOrUpdated(this, new EventArgs<GroupOfPeopleListVM>(cb));
                        }

                        //this.ViewModel = cb;
                    }));

        }

        private void button_add_Click(object sender, RoutedEventArgs e)
        {
            this.ViewModel.EditVM.SupportConceptList.AddRange(this.SupportConceptSearchView.SelectedItems.Where(em => !this.ViewModel.EditVM.SupportConceptList.Contains(em)));
        }

        private void button_remove_Click(object sender, RoutedEventArgs e)
        {
            this.ViewModel.EditVM.SupportConceptList.Remove((CompactSupportConceptDC)((Button)sender).Tag);
        }

        private void button_removegroup_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                GroupOfPeopleVM vm = btn.Tag as GroupOfPeopleVM;
                if (vm != null)
                    this.ViewModel.VMList.Remove(vm);
            }

        }


        private void btnAddGroup_Click(object sender, RoutedEventArgs e)
        {
            var vm = ViewModel.NewVM;
            vm.Name = "Neue Gruppe";
            vm.CreatedByEmployeeOid = BeWoApp.LoggedOnEmployee.EmployeeOid.Value;

            ViewModel.AddNewVMToList(true);
        }
    }
}