using BBWYB.Common.Models;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using System;
using io = System.IO;

namespace WebTest
{
    public class WebView2Manager : IDenpendency
    {
        public WebView2 wb2 { get; private set; }
        //public WebView2Manager()
        //{
        //    Init();
        //}
        public void Init()
        {
            if (wb2 == null)
            {
                wb2 = new WebView2();
                var wb2Setting = CoreWebView2Environment.CreateAsync(userDataFolder: io.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "UPC_WebView2UserData")).Result;
                wb2.EnsureCoreWebView2Async(wb2Setting);
                wb2.CoreWebView2InitializationCompleted += Wb2_CoreWebView2InitializationCompleted;
                wb2.NavigationCompleted += Wb2_NavigationCompleted;
                wb2.WebMessageReceived += Wb2_WebMessageReceived;
            }
        }

        private void CoreWebView2_WebResourceRequested(object? sender, CoreWebView2WebResourceRequestedEventArgs e)
        {
            Console.WriteLine($"{DateTime.Now} WebSroucesRequest {e.Request.Uri}");
        }

        public Action<CoreWebView2WebMessageReceivedEventArgs> OnWebMessageReceived;
        public Action<CoreWebView2NavigationCompletedEventArgs> OnNavigationCompleted;
        public Action<CoreWebView2InitializationCompletedEventArgs> CoreWebView2InitializationCompleted;
        public bool IsInitializationCompleted { get ; private set; }

        private void Wb2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
        {
            OnWebMessageReceived?.Invoke(e);
        }

        private void Wb2_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
        {
            OnNavigationCompleted?.Invoke(e);
        }

        private async void Wb2_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
        {
            wb2.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
            wb2.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;

            //Mozilla/5.0 (Linux; Android 11; M2006J10C Build/RP1A.200720.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/76.0.3809.89 Mobile Safari/537.36 T7/13.8 SP-engine/2.46.0 baiduboxapp/13.8.1.10 (Baidu; P1 11) NABar/1.0 Edg/117.0.0.0
            //Mozilla/5.0 (Linux; Android 12; PEDM00 Build/SKQ1.210216.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/76.0.3809.89 Mobile Safari/537.36 T7/12.29 SP-engine/2.38.0 other/12.29.5.10 (Baidu; P1 12) NABar/1.0
            var platform = "Android";
            var userAgent = "Mozilla/5.0 (Linux; Android 12; PEDM00 Build/SKQ1.210216.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/76.0.3809.89 Mobile Safari/537.36 T7/12.29 SP-engine/2.38.0 other/12.29.5.10 (Baidu; P1 12) NABar/1.0";

            await wb2.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(
            "(() => {" +
            $"const platform = \"{platform}\";" +
            $"const userAgent = \"{userAgent}\";" +
            "if (platform || userAgent) {" +
            "const navigator = window.navigator;" +
            "if (platform) Object.defineProperty(navigator, \"platform\", { value: platform, configurable: false, enumerable: true, writable: false });" +
            "if (userAgent) Object.defineProperty(navigator, \"userAgent\", { value: userAgent, configurable: false, enumerable: true, writable: false });" +
            "}" +
            "})();");


            CoreWebView2InitializationCompleted?.Invoke(e);
            IsInitializationCompleted = true;
        }

        public void Close()
        {
            if (wb2 != null && wb2.CoreWebView2 != null)
            {
                IsInitializationCompleted = false;
                wb2.CoreWebView2InitializationCompleted -= Wb2_CoreWebView2InitializationCompleted;
                wb2.NavigationCompleted -= Wb2_NavigationCompleted;
                wb2.WebMessageReceived -= Wb2_WebMessageReceived;
                var udf = wb2.CoreWebView2.Environment.UserDataFolder;
                wb2.Dispose();
                wb2 = null;
                try
                {
                    io.Directory.Delete(udf, true);
                }
                catch { }
            }
        }
    }
}