using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using Microsoft.Web.WebView2.Core;
using SJ.Controls;
using System;
using System.Threading;
using System.Windows;

namespace BBWYB.Client.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : BWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Width = SystemParameters.WorkArea.Size.Width * 0.8;
            this.Height = SystemParameters.WorkArea.Size.Height * 0.7;
            this.Loaded += MainWindow_Loaded;

           
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (!CheckWebview2Runtime())
            {
                MessageBox.Show("缺少webview2 runtime,请下载安装之后重新运行bbwyb");
                //下载webview2 runtime
                //Task.Factory.StartNew(DownloadWebview2Runtime);
                var webview2RuntimeUrl = "https://msedge.sf.dl.delivery.mp.microsoft.com/filestreamingservice/files/238fc310-c6c1-4a3e-a806-4a7c3c17b377/MicrosoftEdgeWebView2RuntimeInstallerX64.exe";
                try
                {
                    System.Diagnostics.Process.Start("explorer.exe", webview2RuntimeUrl);
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    Clipboard.SetText(webview2RuntimeUrl);
                    MessageBox.Show($"{ex.Message}\r\n调用浏览器失败,网页链接已复制到剪切板,请手动打开浏览器访问", "提示");
                }
                finally
                {
                    Environment.Exit(Environment.ExitCode);
                }
            }

        }

        private void frame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            if (frame.CanGoBack)
                frame.RemoveBackEntry();
        }

        private bool CheckWebview2Runtime()
        {
            bool isInstall = false;
            try
            {
                isInstall = !string.IsNullOrEmpty(CoreWebView2Environment.GetAvailableBrowserVersionString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return isInstall;
        }
    }

}