// <auto-generated />

#nullable enable

namespace {{rootNamespace}}.{{domain.Name}}
{
    using System;
    using System.Collections.Generic;
    using System.Text.Json;
    using System.Threading;
    using System.Threading.Tasks;

    /// <summary>
    /// Represents an adapter for the {{domain.Name}} domain to simplify the command interface.
    /// </summary>
    public class {{dehumanize domain.Name}}Adapter
    {
        private readonly string m_domainName = "{{dehumanize domain.Name}}";
        private Dictionary<string, DevToolsEventData> m_eventMap = new Dictionary<string, DevToolsEventData>();

        /// <summary>
        /// Initializes a new instance of the {{dehumanize domain.Name}}Adapter class.
        /// </summary>
        /// <param name="session">The IDevToolsSession to be used with this adapter.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="session"/> is <see langword="null"/>.</exception>
        public {{dehumanize domain.Name}}Adapter(IDevToolsSession session)
        {
            Session = session ?? throw new ArgumentNullException(nameof(session));
            Session.DevToolsEventReceived += OnDevToolsEventReceived;
    {{#each domain.Events}}
            m_eventMap["{{Name}}"] = new DevToolsEventData(typeof({{dehumanize Name}}EventArgs), On{{dehumanize Name}});
    {{/each}}
        }

        /// <summary>
        /// Gets the DevToolsSession associated with the adapter.
        /// </summary>
        public IDevToolsSession Session { get; }

    {{#each domain.Events}}
        /// <summary>
        /// {{xml-code-comment Description 2}}
        /// </summary>
        public event EventHandler<{{dehumanize Name}}EventArgs>? {{dehumanize Name}};

    {{/each}}
#nullable disable warnings

    {{#each domain.Commands}}
        /// <summary>
        /// {{xml-code-comment Description 2}}
        /// </summary>
        public Task<{{dehumanize Name}}CommandResponse{{#if NoReturn}}?{{/if}}> {{dehumanize Name}}({{dehumanize Name}}CommandSettings{{#if NoParameters}}?{{/if}} command{{#if NoParameters}} = null{{/if}}, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
        {
            return Session.SendCommand<{{dehumanize Name}}CommandSettings, {{dehumanize Name}}CommandResponse>(command{{#if NoParameters}} ?? new {{dehumanize Name}}CommandSettings(){{/if}}, cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived);
        }

    {{/each}}
#nullable enable warnings

        private void OnDevToolsEventReceived(object? sender, DevToolsEventReceivedEventArgs e)
        {
            if (e.DomainName == m_domainName)
            {
                if (m_eventMap.ContainsKey(e.EventName))
                {
                    var eventData = m_eventMap[e.EventName];
                    var eventArgs = e.EventData.Deserialize(eventData.EventArgsType, global::OpenQA.Selenium.DevTools.Json.DevToolsJsonOptions.Default);
                    eventData.EventInvoker(eventArgs);
                }
            }
        }

    {{#each domain.Events}}
        private void On{{dehumanize Name}}(object? rawEventArgs)
        {
            if (rawEventArgs is {{dehumanize Name}}EventArgs e)
            {
                {{dehumanize Name}}?.Invoke(this, e);
            }
        }

    {{/each}}
    }
}
