using System;
using System.Collections.Generic;
using System.Windows;

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

namespace BeWo.View.Detail
{
    public partial class NewsItemView : BeWoView
    {
        private NewsItemListVM _Viewmodel;

        public NewsItemView(NewsItemListVM viewModel)
        {
            this.InitializeComponent();
            this.ViewModel = viewModel;
        }

        public override bool IsDirty
        {
            get
            {
                return this.ViewModel != null ? this.ViewModel.IsDirty : false;
            }
        }

        internal override DependencyObject ValidationOnSaveRootElement
        {
            get
            {
                // workaround
                return this.button_addNewsItem;
            }
        }

        private NewsItemListVM ViewModel
        {
            get
            {
                return this._Viewmodel;
            }

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

        protected override void Save()
        {
            var lToDos = new List<Action<IEmployeeService>>();

            if (this.ViewModel.ChangedCount > 0)
            {
                lToDos.Add(s => s.UpdateNewsItems(this.ViewModel.CommitChanged()));
            }

            if (this.ViewModel.AddedCount > 0)
            {
                lToDos.Add(s => s.InsertNewNewsItems(this.ViewModel.CommitAdded()));
            }

            if (this.ViewModel.RemovedCount > 0)
            {
                lToDos.Add(s => s.DeactivateNewsItems(this.ViewModel.GetRemoved()));
            }

            ServiceFacade.DoMultipleEmployeeServicesAsync(lToDos, this.ReloadViewModel);
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            this.ViewModel.VMList.Remove(this.datagrid_news.GetCurrentValue<NewsItemVM>());
            BeWoWpfUtils.RefreshDXGrid(this.datagrid_news);
        }

        private void ReloadViewModel()
        {
            ServiceFacade.DoEmployeeServiceAsync(s => s.GetAllNewsItems(), r => this.Dispatch(delegate { this.ViewModel = new NewsItemListVM(r); }));
        }

        private void button_addNewsItem_Click(object sender, RoutedEventArgs e)
        {
            if (ValidationTrigger.Validate(this.grid_new))
            {
                this.ViewModel.AddNewVMToList();
				popupedit_TeamSearch.Text = "";
            }
        }

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

		private void TeamSearchView_TeamSelected(object sender, EventArgs<CompactTeamDC> e)
		{
			popup_Team.IsOpen = false;
			ViewModel.NewVM.Team = e.Data;
			popupedit_TeamSearch.Text = e.Data.Name;
			this.popupedit_TeamSearch.Focus();
		}

	    private void Popupedit_TeamSearch_OnDeleteClick(object sender, RoutedEventArgs e)
	    {
		    popupedit_TeamSearch.Text = "";
			ViewModel.NewVM.Team = null;
	    }
    }
}