using System;
using System.Collections.Generic;
using System.Linq;
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 BeWo.ServiceProxy;
using BeWo.ViewModel.ListViewModel;

namespace BeWo.View.Detail
{
    /// <summary>
    /// Interaktionslogik für AdditionalServiceGroupOfPeopleView.xaml
    /// </summary>
    public partial class AdditionalServiceGroupOfPeopleView : BeWoView
    {
        private AdditionalServiceGroupOfPeopleListVM _ViewModel;

        public AdditionalServiceGroupOfPeopleView(AdditionalServiceGroupOfPeopleListVM pViewModel)
        {
            InitializeComponent();
            ViewModel = pViewModel;

            CommandBindings.Add(new CommandBinding(ApplicationCommands.Save, (s, e) => { SaveData(); BeWoApp.MainControl.CloseCurrentPopUp(); }));
        }

        public AdditionalServiceGroupOfPeopleListVM ViewModel
        {
            get { return _ViewModel; }

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

        public event EventHandler<EventArgs<AdditionalServiceGroupOfPeopleListVM>> GroupOfPeopleSavedOrUpdated;

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

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

        protected override void Save()
        {
            foreach (var vm in ViewModel.VMList)
            {
                var dc = vm.CommitToDataContract();
                if (vm.IsNew)
                    ServiceFacade.DoOperationsServiceAsync(s => s.InsertNewAdditionalServiceGroupsOfPeople(new List<AdditionalServiceGroupOfPeopleDC> { dc }));
                else if (vm.IsDirty)
                    ServiceFacade.DoOperationsServiceAsync(s => s.UpdateAdditionalServiceGroupOfPeople(dc));
            }
        }

        private void button_remove_Click(object sender, RoutedEventArgs e)
        {
            var b = (Button) sender;
            var customer = (b.Tag as CompactCustomerDC);
            ViewModel.EditVM.CustomerList.Remove(customer);

            var list = CustomerSearchView.Customers;
            if(!list.Contains(customer))
                list.Add(customer);

            list.Sort((x, y) => (x.LastName + ", " + x.FirstName).CompareTo(y.LastName + ", " + y.FirstName));
            CustomerSearchView.Customers = list;
        }

        private void button_add_Click(object sender, RoutedEventArgs e)
        {
            var c = CustomerSearchView.SelectedItem;

            if (c == null || ViewModel.EditVM == null || ViewModel.EditVM.CustomerList.Contains(c)) 
                return;

            ViewModel.EditVM.CustomerList.Add(c);
            var list = CustomerSearchView.Customers;
            list.Remove(c);
            list.Sort((x, y) => (x.LastName + ", " + x.FirstName).CompareTo(y.LastName + ", " + y.FirstName));

            CustomerSearchView.Customers = list;
        }

        private void button_removegroup_Click(object sender, RoutedEventArgs e)
        {
            var vm = ViewModel.EditVM;
            if (ViewModel.VMList.Contains(vm))
                ViewModel.VMList.Remove(vm);
        }

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

            ViewModel.AddNewVMToList(true);
        }

        private void groupListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            long? oid = null;
            var liste = new List<CompactCustomerDC>();

            if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.ViewAll) && !BeWoApp.LoggedOnUser.HasRight(UserRightType.CustomerView_View))
                oid = BeWoApp.LoggedOnUser.Employee.EmployeeOid;

            ServiceFacade.DoCustomerServiceSync(s => liste = s.GetAllActiveCustomersCompact(CustomerSearchView.FetchReferenceNumbers, oid));

            liste.Sort((x, y) => (x.LastName + ", " + x.FirstName).CompareTo(y.LastName + ", " + y.FirstName));

            if (ViewModel.EditVM != null)
                foreach (var item in ViewModel.EditVM.CustomerList.ToList().Where(item => liste.Contains(item)))
                    liste.Remove(item);

            if (ViewModel.NewVM != null)
                foreach (var item in ViewModel.NewVM.CustomerList.ToList().Where(item => liste.Contains(item)))
                    liste.Remove(item);

            CustomerSearchView.Customers = liste;
        }
    }
}
