using System;

using Svelto.ECS;

namespace GamecraftModdingAPI.Events
{
    public class EmitterBuilder
    {
		private string name;

		private int? type;

        /// <summary>
        /// Create a new event emitter builder.
        /// </summary>
        public EmitterBuilder()
        {
        }

        /// <summary>
        /// Create a new event emitter builder.
		/// This is equivalent to new <code>EmitterBuilder().Name(name)</code>
        /// </summary>
        /// <param name="name">The emitter name.</param>
		public EmitterBuilder(string name)
		{
			this.name = name;
		}

        /// <summary>
		/// Create and return an event emitter builder.
        /// </summary>
        /// <returns>The builder.</returns>
		public static EmitterBuilder Builder()
		{
			return new EmitterBuilder();
		}

        /// <summary>
        /// Create and return an event emitter builder.
		/// This is equivalent to <code>Builder().Name(name)</code>
        /// </summary>
        /// <returns>The builder.</returns>
        /// <param name="name">The emitter name.</param>
		public static EmitterBuilder Builder(string name)
		{
			return new EmitterBuilder(name);
		}

        /// <summary>
        /// Name the event emitter.
        /// </summary>
        /// <returns>The builder.</returns>
        /// <param name="name">The event emitter name.</param>
		public EmitterBuilder Name(string name)
		{
			this.name = name;
			return this;
		}

        /// <summary>
        /// Set the type of event to handle.
        /// </summary>
        /// <returns>The builder.</returns>
        /// <param name="eventType">The event type.</param>
		public EmitterBuilder Handle(EventType eventType)
		{
			return Handle((int)eventType);
		}

		/// <summary>
        /// Set the type of event to handle.
        /// </summary>
        /// <returns>The builder.</returns>
        /// <param name="eventType">The event type.</param>
		public EmitterBuilder Handle(int eventType)
		{
			this.type = eventType;
			return this;
		}

        /// <summary>
        /// Build the event emitter.
        /// </summary>
        /// <returns>The event emitter.</returns>
        /// <param name="register">Automatically register the event emitter with EventManager.AddEventemitter().</param>
		public IEventEmitterEngine Build(bool register = true)
		{
			if (string.IsNullOrWhiteSpace(name))
            {
                throw new EventParameterMissingException("Event emitter name must be defined before Build() is called");
            }
			if (!type.HasValue)
			{
				throw new EventParameterMissingException("Event emitter event type must be defined before Build() is called");
			}
			SimpleEventEmitterEngine result = new SimpleEventEmitterEngine(type.Value, name);
			if (register)
			{
				EventManager.AddEventEmitter(result);
			}
			return result;
		}
    }
}