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

namespace BeWo.View
{
    /// <summary>
    /// Interaktionslogik für BeWoWindow.xaml
    /// </summary>
    public partial class BeWoWindow : Window
    {
        private object _GroupBoxContent;
        public object GroupBoxContent
        {
            get { return _GroupBoxContent; }
            set 
            { 
                _GroupBoxContent = value;
                rootGroupBox.Content = value;
            }
        }

        public bool IsMaximizable = true;

        public BeWoWindow()
        {
            InitializeComponent();

            CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, (s, e) => Close()));
        }

        public BeWoWindow(FrameworkElement fe)
        {
            InitializeComponent();

            rootGroupBox.Content = fe;
            CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, (s, e) => Close()));
        }

        private void RootGroupBox_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                DragMove();
            }
            catch (Exception){}
        }

        private void RootGroupBox_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (!IsMaximizable)
            {
                return;
            }

            var pos = PointToScreen(Mouse.GetPosition(this));

            if (!(e.OriginalSource is Border) && !(e.OriginalSource is TextBlock) || pos.Y > 200d || e.OriginalSource is TextBlock && Mouse.DirectlyOver is Button)
            {
                return;
            }
            
            if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
                return;
            }

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