using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PickupTheCrew
{
	class PlayStates
	{
		/// <summary>
		/// Function to update a game in the Attract state.
		/// </summary>
		/// <param name="game"></param>
		/// <param name="gameTime"></param>
		/// <param name="keyboard"></param>
		public static void UpdateStateAttract(PickupTheCrew game, GameTime gameTime, KeyboardState keyboard)
		{
			if (keyboard.IsKeyDown(Keys.Up))
			{
				game.HardReset();
				game.State = GameState.Playing;
			}

			// Update all the crew members so that they follow the AI
			//foreach(Sprite visibleSprite in game.CrewMemberObjects)
			//{
			//	visibleSprite.Update(gameTime, game);
			//}
		}

		/// <summary>
		/// Function to update a game in the Playing state.
		/// </summary>
		/// <param name="game"></param>
		/// <param name="gameTime"></param>
		/// <param name="keyboard"></param>
		public static void UpdateStatePlaying(PickupTheCrew game, GameTime gameTime, KeyboardState keyboard)
		{
			// Update all the Sharks
			foreach (Sprite visibleSprite in game.SharkObjects)
			{
				visibleSprite.Update(gameTime, game);
			}
			// Update all the CrewMembers
			foreach (Sprite visibleSprite in game.CrewMemberObjects)
			{
				visibleSprite.Update(gameTime, game);
			}
			// Update the player
			game.PlayerObject.Update(gameTime, game);

			// Level up if there are no more CrewMembers waiting to be rescued
			bool isReadyForLevelUp = true;
			foreach(CrewMember nextCrewMember in game.CrewMemberObjects)
			{
				if(nextCrewMember.State == CrewMemberState.AwaitingRescue)
				{
					isReadyForLevelUp = false;
					break;
				}
			}
			if(isReadyForLevelUp)
			{
				if(game.State == GameState.Playing)
				{
					// Play a sound
					MediaPlayer.Play(game.Sounds["LevelUp"]);
					// Update the HUD
					game.DisplayMessage = String.Format("Level {0} complete!", game.Level);
					// Increment the level counter
					game.Level++;
					// Add to the player's score relative to the level they are on
					game.Score += 50 * game.Level;

					// Add another Shark
					game.SharkCount++;
					game.SharkObjects.Add(new Shark(Vector2.Zero));
					// Increase the Shark's MovementSpeed slightly
					Shark.MovementSpeed += Shark.MovementSpeedIncrement;

					// Do a soft reset to get everything in position for the next level
					game.SoftReset();

					game.State = GameState.LevelUp; // switch to the 'LevelUp' state
				}
				else if(game.State == GameState.Attract)
				{
					// Do a simple soft reset for the AI
					game.SoftReset();
				}
				
			}
		}

		/// <summary>
		/// Function to update a game in the LevelUp state
		/// </summary>
		/// <param name="game"></param>
		/// <param name="gameTime"></param>
		/// <param name="keyboard"></param>
		public static void UpdateStateLevelUp(PickupTheCrew game, GameTime gameTime, KeyboardState keyboard)
		{
			if (keyboard.IsKeyDown(Keys.Space))
			{
				game.State = GameState.Playing;
			}
		}

		/// <summary>
		/// Function to update a game in the GameOver state.
		/// </summary>
		/// <param name="game"></param>
		/// <param name="gameTime"></param>
		/// <param name="keyboard"></param>
		public static void UpdateStateGameOver(PickupTheCrew game, GameTime gameTime, KeyboardState keyboard)
		{
			if (keyboard.IsKeyDown(Keys.Space))
			{
				game.HardReset();
				game.State = GameState.Attract;
			}
		}
	}
}
