using CefFlashBrowser.FlashBrowser.Handlers;
using CefFlashBrowser.Data;
using CefFlashBrowser.Utils;
using CefSharp;
using SimpleMvvm.Messaging;
using System.ComponentModel;
using System.Windows;
using BrowserHandlers = CefFlashBrowser.Browser.Handlers;

namespace CefFlashBrowser.Views
{
    /// <summary>
    /// ViewSourceWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ViewSourceWindow : Window
    {
        private class ViewSourceBrowserLifeSpanHandler : LifeSpanHandler
        {
            public override bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
            {
                Application.Current.Dispatcher.Invoke(() => WindowManager.ShowBrowser(targetUrl));
                newBrowser = null;
                return true;
            }
        }


        public string Address
        {
            get { return (string)GetValue(AddressProperty); }
            set { SetValue(AddressProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Address.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AddressProperty =
            DependencyProperty.Register("Address", typeof(string), typeof(ViewSourceWindow), new PropertyMetadata(null, (d, e) =>
            {
                ((ViewSourceWindow)d).browser.Address = $"view-source:{e.NewValue}";
            }));


        public ViewSourceWindow()
        {
            InitializeComponent();

            browser.DragHandler = new BrowserHandlers.DisableDragHandler();
            browser.MenuHandler = new BrowserHandlers.ContextMenuHandler();
            browser.JsDialogHandler = new BrowserHandlers.JsDialogHandler();
            browser.DownloadHandler = new BrowserHandlers.DownloadHandler();
            browser.LifeSpanHandler = new ViewSourceBrowserLifeSpanHandler();

            Messenger.Global.Register(MessageTokens.CLOSE_ALL_BROWSERS, CloseBrowserHandler);
            Closed += delegate { Messenger.Global.Unregister(MessageTokens.CLOSE_ALL_BROWSERS, CloseBrowserHandler); };
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (!browser.IsDisposed)
            {
                browser.CloseBrowser(true);
            }
            base.OnClosing(e);
        }

        private void CloseBrowserHandler(object msg)
        {
            Close();
        }
    }
}
