using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using BeWo.Core;
using BeWo.ServiceProxy;
using BeWo.View.Detail;
using BeWo.View.Navigation.Sorter;
using BeWo.ViewModel;

using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;
using Microsoft.Win32;

namespace BeWo.View.Navigation
{
    /// <summary>
    /// Interaktionslogik für TeamNavigationView.xaml
    /// </summary>
    public partial class TeamNavigationView : BeWoView
    {
        private IEnumerable<IFilterableDC> objectList = null;
        private IEnumerable<IFilterableDC> recentList = null;

        public TeamNavigationView()
        {
            this.InitializeComponent();

            //###CB SERIENBRIEF mainNavigationView.MailMergeButton.Visibility = Visibility.Collapsed;

			this.mainNavigationView.Sorter = new TeamSorter();
	        this.mainNavigationView.ShowArchivedObjectsVisible = false;

        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            ReloadData(false);
        }

        private void mainNavigationView_CreateNewObject()
        {
            VMFactory.CreateTeamVMAsync(cb => this.Dispatch(delegate { this.MainControl.NavigateTo(new TeamView(cb) { ParentView = this }); }));
        }

        private bool mainNavigationView_DeleteObject(IFilterableDC obj)
        {
            CompactTeamDC dc = obj as CompactTeamDC;
            if (dc != null)
            {
                if (MessageBox.Show("Wollen Sie das Team '" + dc.Name + "' wirklich löschen?", "Löschen", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.Yes)
                {
                    ServiceFacade.DoEmployeeServiceAsync(s => s.DeactivateTeam(dc.TeamOid, dc.TeamVersion));
                    return true;
                }
            }

            return false;
        }

        private void mainNavigationView_ExcelExport(object sender, EventArgs<IEnumerable<IFilterableDC>> e)
        {
			var path = BeWoApp.GetAndCreateUserAppDataPath() + "\\Teams.xls";

            var sf = new SaveFileDialog { FileName = Path.GetFileName(path), Filter = "Microsoft Excel 97-2003-Arbeitsblatt|*.xls|Alle Dateien|*.*" };
            if (sf.ShowDialog() != true)
                return;

            ServiceFacade.DoDownloadServiceSync(s => BeWoUtils.WriteExcelFile(s.PrepareExcelFileTeamExport(mainNavigationView.DownloadLinkEngine.ExcelFileId, e.Data.Cast<CompactTeamDC>().Select(c => c.TeamOid).ToList()), sf.FileName));
        }

        private void mainNavigationView_OpenObject(IFilterableDC obj)
        {
            CompactTeamDC dc = obj as CompactTeamDC;
            if (dc != null)
            {
                VMFactory.CreateTeamVMAsync(dc.TeamOid, cb => this.Dispatch(delegate { this.MainControl.NavigateTo(new TeamView(cb) { ParentView = this }); }));
            }
        }

        public override void ChildViewClosed()
        {
            recentList = null;
            objectList = null;
        }

        private void MainNavigationView_OnReloadData()
        {
            ReloadData(true);
        }

        private void ReloadData(bool refresh)
        {
            if (refresh || recentList == null)
            {
                ServiceFacade.DoEmployeeServiceAsync(s => s.GetAllTeamsCompact(), r => this.Dispatch(delegate
                                                                                                         {
                                                                                                             objectList = r.Cast<IFilterableDC>();
                                                                                                             this.mainNavigationView.ShowSearchResult(objectList);
                                                                                                         }));

                ServiceFacade.DoEmployeeServiceAsync(s => s.GetLastOpenedTeams(), r => this.Dispatch(delegate
                                                                                                         {
                                                                                                             recentList = r.Cast<IFilterableDC>();
                                                                                                             this.mainNavigationView.ShowHistory(recentList);
                                                                                                         }));
            }
            else
            {
                this.mainNavigationView.ShowSearchResult(objectList);
                this.mainNavigationView.ShowHistory(recentList);
            }
        }
    }
}