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.ViewModel;

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

using Microsoft.Win32;

namespace BeWo.View.Navigation
{
    public partial class UserGroupNavigationView
    {

        private IEnumerable<IFilterableDC> objectList;
        private IEnumerable<IFilterableDC> recentList;

        public UserGroupNavigationView()
        {
            InitializeComponent();
			//###CB SERIENBRIEF mainNavigationView.MailMergeButton.Visibility = Visibility.Collapsed;
        }

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

        private void mainNavigationView_CreateNewObject()
        {
            VMFactory.CreateUserGroupVMAsync(cb => this.Dispatch(() => MainControl.NavigateTo(new UserGroupView(cb) {ParentView = this})));
        }

        private bool mainNavigationView_DeleteObject(IFilterableDC obj)
        {
            var ugdc = obj as UserGroupDC;
            if (ugdc != null)
            {
                if (MessageBox.Show("Wollen Sie die Benutzergruppe '" + ugdc.Name + "' wirklich löschen?", "Löschen", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.Yes)
                {
                    ServiceFacade.DoUserServiceAsync(s => s.DeactivateUserGroup(ugdc.UserGroupOid.Value, ugdc.UserGroupVersion.Value),() => this.Dispatch(() => ReloadData(true)), true);
                    return true;
                }
            }

            return false;
        }

        private void mainNavigationView_ExcelExport(object sender, EventArgs<IEnumerable<IFilterableDC>> e)
        {
			var path = BeWoApp.GetAndCreateUserAppDataPath() + "\\Benutzergruppen.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.PrepareExcelFileUserGroupExport(mainNavigationView.DownloadLinkEngine.ExcelFileId, e.Data.Cast<UserGroupDC>().Select(c => c.UserGroupOid.Value).ToList()), sf.FileName));
        }

        private void mainNavigationView_OpenObject(IFilterableDC obj)
        {
            var ugdc = obj as UserGroupDC;

            if (ugdc != null)
            {
                VMFactory.CreateUserGroupVMAsync(ugdc.UserGroupOid.Value, cb => this.Dispatch(() => MainControl.NavigateTo(new UserGroupView(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.DoUserServiceAsync(s => s.GetAllUserGroups(), r => this.Dispatch(delegate
                                                                                                   {
                                                                                                       objectList = r;
                                                                                                       mainNavigationView.ShowSearchResult(objectList);
                                                                                                   }), true);

                ServiceFacade.DoUserServiceAsync(s => s.GetLastOpenedUserGroups(), r => this.Dispatch(delegate
                                                                                                          {
                                                                                                              recentList = r;
                                                                                                              mainNavigationView.ShowHistory(recentList);
                                                                                                          }), true);
            }
            else
            {
                mainNavigationView.ShowSearchResult(objectList);
                mainNavigationView.ShowHistory(recentList);
            }
        }
    }
}