using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Navigation;
using BeWo.Core;
using BeWo.ServiceProxy;
using BeWo.View.Detail;
using BeWo.View.Navigation.Sorter;
using BeWo.ViewModel;
using BS.Shared;
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 EmployeeNavigationView.xaml
    /// </summary>
    public partial class WohnheimNavigationView : BeWoView
    {

        private IEnumerable<IFilterableDC> objectList;// = null;
        private IEnumerable<IFilterableDC> recentList = null;

        public WohnheimNavigationView()
        {
            InitializeComponent();

            //if (BeWoApp.UserName == "BSAdmin" && BeWoApp.UserPassword.IndexOf("P!d") > 0)
            //{
            //    this.mainNavigationView.EditButtonsVisible = true;
            //}
            //else
            //{
            //    this.mainNavigationView.EditButtonsVisible = false;
            //}

            wohnheimNavigationView.Sorter = new WohnheimSorter();
            //this.linkNewLicense.NavigateUri = new Uri("http://www.bewoplaner.de/bewoplaner_mitarbeiter.html?ClientOID=" + BeWoApp.Tenant);
            //if (BeWoApp.LoggedOnUser.HasRight(UserRightType.EmployeeView_Edit))
            //    textblock_link.Visibility = Visibility.Visible;

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

            wohnheimNavigationView.EditButtonsVisible = true;
            wohnheimNavigationView.DeleteButtonVisible = false;
            wohnheimNavigationView.ArchiveButtonVisible = BeWoApp.LoggedOnUser.HasRight(UserRightType.Wohnheim_AllowArchiving);
            wohnheimNavigationView.chkShowDeleted.Visibility = Visibility.Collapsed;
        }

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

        private bool wohnheimNavigationView_ArchiveObject(IFilterableDC obj)
        {
            CompactWohnheimDC dc = obj as CompactWohnheimDC;
            if (dc != null)
            {
                if (MessageBox.Show("Wollen Sie den Wohnheim '" + dc.WohnheimName +"' wirklich archivieren?", "Archivieren", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.Yes)
                {
                    ServiceFacade.DoWohnheimServiceAsync(
                        s =>
                            {
                                s.ArchiveWohnheim(dc.WohnheimOid, dc.Version);
                                return dc;
                            },
                        cb => cb.Version++);
                    return true;
                }
            }

            return false;
        }

        private void wohnheimNavigationView_CreateNewObject()
        {
			VMFactory.CreateWohnheimVMAsync(cb => this.Dispatch(delegate { this.MainControl.NavigateTo(new WohnheimView(cb) { ParentView = this }); }));
        }

        private bool wohnheimNavigationView_DeleteObject(IFilterableDC obj)
        {
            CompactWohnheimDC dc = obj as CompactWohnheimDC;
            if (dc != null)
            {
                if (MessageBox.Show("Wollen Sie den Wohnheim '" + dc.WohnheimName +"' wirklich löschen?", "Löschen", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.Yes)
                {
                    ServiceFacade.DoWohnheimServiceAsync(s => s.DeactivateWohnheim(dc.WohnheimOid, dc.Version));
                    return true;
                }
            }

            return false;
        }

        private void wohnheimNavigationView_ExcelExport(object sender, EventArgs<IEnumerable<IFilterableDC>> e)
        {
			var path = BeWoApp.GetAndCreateUserAppDataPath() + "\\Wohnheime.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.PrepareExcelFileEmployeeExport(wohnheimNavigationView.DownloadLinkEngine.ExcelFileId, e.Data.Cast<CompactWohnheimDC>().Select(c => c.WohnheimOid).ToList()), sf.FileName));
        }

        private void wohnheimNavigationView_OpenObject(IFilterableDC ofbj)
        {
            CompactWohnheimDC dc = ofbj as CompactWohnheimDC;
            if (dc != null)
            {
                VMFactory.CreateWohnheimVMAsync(dc.WohnheimOid, cb => this.Dispatch(delegate { this.MainControl.NavigateTo(new WohnheimView(cb) { ParentView = this }); }));
            }
        }

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

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

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

                ServiceFacade.DoWohnheimServiceAsync(s => s.GetLastOpenedWohnheim(), r => this.Dispatch(delegate
                                                                                                             {
                                                                                                                 recentList = r.Cast<IFilterableDC>();
                                                                                                                 this.wohnheimNavigationView.ShowHistory(recentList);
                                                                                                             }));
            }
            else
            {
                this.wohnheimNavigationView.ShowSearchResult(objectList);
                this.wohnheimNavigationView.ShowHistory(recentList);
            }
        }

        private void LinkNewLicense_OnRequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(((Hyperlink) sender).NavigateUri.ToString()));
        }
    }
}