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

namespace Ramitta
{
    public partial class winTitleBar : UserControl
    {
        public winTitleBar()
        {
            InitializeComponent();
        }

        #region 命令执行方法

        private void MinimizeCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var window = Window.GetWindow(this);
            if (window != null)
            {
                window.WindowState = WindowState.Minimized;
            }
        }

        private void MaximizeCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var window = Window.GetWindow(this);
            if (window != null)
            {
                window.WindowState = window.WindowState == WindowState.Maximized
                    ? WindowState.Normal
                    : WindowState.Maximized;
            }
        }

        private void CloseCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var window = Window.GetWindow(this);
            if (window != null)
            {
                window.Close();
            }
        }

        private void DragMoveCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var window = Window.GetWindow(this);
            if (window != null)
            {
                window.DragMove();
            }
        }

        #endregion

        #region 鼠标事件处理

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
            {
                // 双击标题栏切换最大化/还原
                var window = Window.GetWindow(this);
                if (window != null)
                {
                    //MaximizeCommand_Executed(null, null);
                }
            }
            else if (e.ChangedButton == MouseButton.Left)
            {
                // 单机拖动窗口
                var window = Window.GetWindow(this);
                window?.DragMove();
            }
        }

        #endregion

        #region 依赖属性

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(winTitleBar),
                new PropertyMetadata("Application"));

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(ImageSource), typeof(winTitleBar),
                new PropertyMetadata(null));

        public ImageSource Icon
        {
            get { return (ImageSource)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

        #endregion
    }
}