using Microsoft.Web.WebView2.Core;
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace AIProofread.Controls
{

    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            InitWebEnvAsync();
        }

        public async void InitWebEnvAsync()
        {
            // 禁用web安全，允许跨域 否则需要web编译为umd加载模式
            var ops = new CoreWebView2EnvironmentOptions("--disable-web-security");
            var env = await CoreWebView2Environment.CreateAsync(null, Config.WEB_DATA_PATH, ops);
            await webView.EnsureCoreWebView2Async(env);
            //webView.CoreWebView2.Settings.AreDevToolsEnabled = false;
            //webView.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = false;
            //webView.CoreWebView2.Settings.AreHostObjectsAllowed = true;
            // 添加 js与客户端代理

            var eventForwarder = new EventForwarder(this);
            webView.CoreWebView2.AddHostObjectToScript("bridge", Bridge.bridge);
            webView.CoreWebView2.AddHostObjectToScript("form", this);
            webView.CoreWebView2.AddHostObjectToScript("event", eventForwarder);
        }
        private void FormMain_Load(object sender, EventArgs e)
        {
            webView.Source = new Uri("http://127.0.0.1:5173/");
        }

        public void ShowLogin()
        {
            (new FormLogin()).Show();
        }
    }
}
