using Protogame;
using Protoinject;
using System.Linq;

namespace SuperBallBrawlers
{
    public class StartGameAction : IEventAction<IGameContext>
    {
        private readonly IHierarchy _hierarchy;

        public StartGameAction(IHierarchy hierarchy)
        {
            _hierarchy = hierarchy;
        }

        public void Handle(IGameContext context, Event @event)
        {
            var world = (SuperBallBrawlersWorld) context.World;

            if (!world.IsChangingStates && world.TargetState == WorldState.TitleScreen)
            {
                world.TargetState = WorldState.GamePlay;
                foreach (var p in world.GetEntitiesForWorld(_hierarchy).OfType<PlayerEntity>())
                {
                    p.ResetState();
                }
            }

            if (!world.IsChangingStates && world.TargetState == WorldState.GamePlay)
            {
                world.TargetState = WorldState.GameInit;
            }

            //if (!world.IsChangingStates && world.TargetState == WorldState.VictoryScreen)
            //{
                //world.TargetState = WorldState.GameInit;
            //}
        }
    }
}
