using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

using BeWo.Controls;
using BeWo.Core;
using Microsoft.Office.Interop.Word;
using Style = System.Windows.Style;

namespace BeWo.View.Detail
{
    public partial class ModalViewWindow
    {
        private bool savedOrUpdated;

        public BeWoView DetailView { get; set; }
        public BeWoView ParentView { get; set; }
        public Grid RootGrid { get { return rootGrid; } }

        public ModalViewWindow(BeWoView detailView, BeWoView parentView)
        {
            InitializeComponent();
            DetailView = detailView;
            ParentView = parentView;

            rootGrid.Children.Add(DetailView);
            InitCommandBindings();

            if (BeWoApp.MainControl.ModalPopUpBackGround == null)
                BeWoApp.MainControl.ShowModalBackground();

            MinWidth = detailView.MinWidth + 16;
            MinHeight = 555;

            Owner = parentView == null ? BeWoApp.CurrentBeWo.MainWindow : GetWindow(parentView);

            Loaded += OnLoad;

            Closing += delegate
                           {
                               if(BeWoApp.MainControl.OpenedModalViewWindows.First().Equals(this)) 
                                   BeWoApp.MainControl.CloseCurrentPopUp();
                           };

            ((GroupBox) (((ShadowChrome) ((Grid) detailView.Content).Children[0]).Child)).Style = BeWoApp.CurrentBeWo.Resources["MainContentGroupBoxWithoutMaximizeBtnStyle"] as Style;
        }

        private void OnLoad(object sender, RoutedEventArgs e)
        {
            if (BeWoApp.MainControl.OpenedModalViewWindows.Count > 1)
            {
                Left = (BeWoApp.MainControl.ActualWidth/2) - (ActualWidth/2) + 10*(BeWoApp.MainControl.OpenedModalViewWindows.Count - 1);
                Top = (BeWoApp.MainControl.ActualHeight/2) - (ActualHeight/2) + 50*(BeWoApp.MainControl.OpenedModalViewWindows.Count - 1);
            }
            else
            {
                Left = (BeWoApp.MainControl.ActualWidth / 2) - (ActualWidth / 2);
                Top = (BeWoApp.MainControl.ActualHeight / 2) - (ActualHeight / 2);
            }
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);

            if (!e.Cancel && Owner != null)
            {
                Owner.Focus();
            }
        }

        private void InitCommandBindings()
        {
            CommandBindings.Add(
                new CommandBinding(
                    ApplicationCommands.Save,
                    (s, e) =>
                    {
                        if (ParentView != null)
                            if (ParentView.IsDirty)
                            {
                                MessageBox.Show("Bevor Sie an diesem Objekt eine Änderung durchführen können, müssen Sie das vorherige Objekt erst speichern!", "Speichern", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                                return;
                            }

                        if (!DetailView.SaveData())
                            return;

                        savedOrUpdated = true;

                        DialogResult = savedOrUpdated;

						Close();
                    },
                    (s, e) => e.CanExecute = DetailView != null && DetailView.IsDirty));

            CommandBindings.Add(new CommandBinding(BeWoCommands.Excel, (s, e) => DetailView.ExportToExcel(e.Parameter as string), (s, e) => e.CanExecute = DetailView != null && DetailView.CanExcelExport));

            CommandBindings.Add(new CommandBinding(ApplicationCommands.Delete, (s, e) => DetailView.Delete(), (s, e) => e.CanExecute = DetailView != null && DetailView.CanDelete));

            CommandBindings.Add(new CommandBinding(BeWoCommands.Word, (s, e) => DetailView.ExportToWord(e.Parameter as string), (s, e) => e.CanExecute = DetailView != null && DetailView.CanWordExport));

            CommandBindings.Add(
                new CommandBinding(
                    ApplicationCommands.Close,
                    (s, e) =>
                        {
                            if(ParentView != null)
                                if (ParentView.IsDirty)
                                {
                                    DialogResult = false;
                                    Close();
                                    return;
                                }

                            if (!DoSaveCheck()) 
                                return;

                            DialogResult = savedOrUpdated;

                            Close();

                        }, (s, e) => e.CanExecute = DetailView != null));
        }

        private bool DoSaveCheck()
        {
            return DetailView == null || DetailView.DoSaveCheck();
        }

        private void ModalViewWindow_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
                return;
            }

            if (WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;
            }
        }
    }
}
