using Caliburn.Micro;
using MECF.Framework.UI.Client.CenterViews.Dialogs;
using System;
using System.Dynamic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;

namespace OpenSEMI.Ctrlib.Window
{
    public class CustomWnd : System.Windows.Window
    {
        private HwndSource curHwndSource = null;
        private HwndSourceHook curHwndSourceHook = null;

        static CustomWnd()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomWnd), new FrameworkPropertyMetadata(typeof(CustomWnd)));
        }

        public CustomWnd()
        {
            CommandBindings.Add(new CommandBinding(SystemCommands.ShowSystemMenuCommand, ShowSystemMenu));
            CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, CloseWindow));
            CommandBindings.Add(new CommandBinding(SystemCommands.MaximizeWindowCommand, MaximizeWindow, CanResizeWindow));
            CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand, MinimizeWindow));
            CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand, RestoreWindow, CanResizeWindow));
        }

        #region Window event
        private void CanResizeWindow(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = ResizeMode == ResizeMode.CanResize || ResizeMode == ResizeMode.CanResizeWithGrip;
        }

        private void RestoreWindow(object sender, ExecutedRoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Normal;
        }

        private void MinimizeWindow(object sender, ExecutedRoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

        private void MaximizeWindow(object sender, ExecutedRoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Maximized;
        }

        private void CloseWindow(object sender, ExecutedRoutedEventArgs e)
        {
            this.Close();
        }

        private void ShowSystemMenu(object sender, ExecutedRoutedEventArgs e)
        {
            var vm = new SystemInfoViewModel();
            var wm = new WindowManager();
            
            dynamic settings = new ExpandoObject(); 
            settings.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            settings.WindowStyle = WindowStyle.SingleBorderWindow; 
            settings.ResizeMode = ResizeMode.CanResizeWithGrip;
            settings.ShowInTaskbar = true; 
            settings.Title = "System Info."; 
            wm.ShowDialog(vm, null, settings);
        }

        #endregion

        #region hook
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);

            // 获取窗体句柄
            IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;

            curHwndSource = HwndSource.FromHwnd(hwnd);
            if (curHwndSource == null)
            {
                return;
            }

            curHwndSourceHook = new HwndSourceHook(this.WndProc);
            curHwndSource.AddHook(curHwndSourceHook);
        }

        /// <summary>
        /// 系统消息处理
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <param name="handled"></param>
        /// <returns></returns>
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            IntPtr result = IntPtr.Zero;
            int message = (int)msg;
            switch (message)
            {
                case 0x001A://0x001A,WM_SETTINGCHANGE


                    break;
                default:
                    handled = false;
                    break;
            }

            return result;
        }
        #endregion
    }
}
