using System;

namespace CefSharpWrap
{
    public sealed class CefSharpWrapBrowser : CefSharpWrapBrowserBase
    {
        public event EventHandler<string> ExternalRequested;

        public event EventHandler Resize;

        public CefSharpWrapBrowser(bool allowFileProtocol, string url) : base(url)
        {
            Underlying.RequestHandler = allowFileProtocol
                ? new ExternalRequestBlocker(OnExternalRequested, "file")
                : new ExternalRequestBlocker(OnExternalRequested);

            Underlying.Resize += Underlying_Resize;
        }

        private void Underlying_Resize(object sender, EventArgs e)
        {
            OnResize();
        }

        private void OnExternalRequested(string e) => ExternalRequested?.Invoke(this, e);

        public void LoadUrl(string url) => Underlying.Load(url);

        public void RegisterJsObject(string name, object instance) => Underlying.RegisterJsObject(name, instance);

        private void OnResize()
        {
            Resize?.Invoke(this, EventArgs.Empty);
        }
    }
}