using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;

namespace ShootingHero.Components.Backgrounds
{
	public enum Stage
	{
		Stage1 = 1,
		Stage2 = 2,
		Stage3 = 3,
		Stage4 = 4,
		Stage5 = 5,
		Stage6 = 6,
		InfinityStage = 7,
		RandomStage = -1,
	}

	public class Background : DrawableGameComponent
	{
		Stage stage;
		Texture2D stageBackground;
		List<Texture2D> objectTextures = new List<Texture2D> ();

		List<FloatingObject> floatingObjects = new List<FloatingObject> ();

		Timer createTimer;

		public Background ( Game game, Stage stage )
			: base ( game )
		{
			if ( stage == Stage.RandomStage || stage == Stage.InfinityStage )
				stage = ( Stage ) ( Game as ShootingHero ).Random.Next ( 1, 7 );
			this.stage = stage;
			createTimer = new Timer ( 500 );
		}

		protected override void LoadContent ()
		{
			stageBackground = Game.Content.Load<Texture2D> ( "Background/background0" + ( ( int ) stage ).ToString () );

			switch ( stage )
			{
				case Stage.Stage1:
					objectTextures.Add ( Game.Content.Load<Texture2D> ( "Background/cloud1" ) );
					objectTextures.Add ( Game.Content.Load<Texture2D> ( "Background/cloud2" ) );
					break;
				case Stage.Stage2:
					objectTextures.Add ( Game.Content.Load<Texture2D> ( "Background/ginkgo" ) );
					objectTextures.Add ( Game.Content.Load<Texture2D> ( "Background/maple" ) );
					break;
				case Stage.Stage3:
					objectTextures.Add ( Game.Content.Load<Texture2D> ( "Background/bubble1" ) );
					break;
				case Stage.Stage4:
					objectTextures.Add ( Game.Content.Load<Texture2D> ( "Background/leaf" ) );
					break;
				case Stage.Stage5:
					objectTextures.Add ( Game.Content.Load<Texture2D> ( "Background/bubble2" ) );
					break;
				case Stage.Stage6:
					objectTextures.Add ( Game.Content.Load<Texture2D> ( "Background/star1" ) );
					break;
			}

			base.LoadContent ();
		}

		protected override void UnloadContent ()
		{
			objectTextures.Clear ();
			base.UnloadContent ();
		}

		public override void Update ( GameTime gameTime )
		{
			createTimer.Update ( gameTime );
			if ( createTimer.CheckTimer )
			{
				Texture2D texture = null;
				bool rotate = false;
				bool transparent = false;

				switch ( stage )
				{
					case Stage.Stage1:
						texture = ( ( Game as ShootingHero ).Random.Next ( 0, 2 ) == 1 ) ? objectTextures [ 0 ] : objectTextures [ 1 ];
						transparent = ( ( Game as ShootingHero ).Random.Next ( 0, 2 ) == 1 ) ? true : false;
						break;
					case Stage.Stage2:
						texture = ( ( Game as ShootingHero ).Random.Next ( 0, 2 ) == 1 ) ? objectTextures [ 0 ] : objectTextures [ 1 ];
						rotate = true;
						break;
					case Stage.Stage3:
						texture = objectTextures [ 0 ];
						break;
					case Stage.Stage4:
						texture = objectTextures [ 0 ];
						rotate = true;
						break;
					case Stage.Stage5:
						texture = objectTextures [ 0 ];
						break;
					case Stage.Stage6:
						texture = objectTextures [ 0 ];
						transparent = ( ( Game as ShootingHero ).Random.Next ( 0, 1 ) == 0 ) ? true : false;
						rotate = true;
						break;
				}

				FloatingObject fo = new FloatingObject ( Game, texture, rotate, transparent );
				floatingObjects.Add ( fo );
				createTimer.Reset ();
			}

			List<FloatingObject> removeList  = new List<FloatingObject>();
			foreach ( FloatingObject fo in floatingObjects )
			{
				fo.Update ( gameTime );
				if ( fo.Y >= 850 )
					removeList.Add ( fo );
			}
			foreach ( FloatingObject fo in removeList )
				floatingObjects.Remove ( fo );

			base.Update ( gameTime );
		}

		public override void Draw ( GameTime gameTime )
		{
			SpriteBatch spriteBatch = ( Game as ShootingHero ).SpriteBatch;

			spriteBatch.Draw ( stageBackground, new Vector2 ( 0, 0 ), Color.White );

			foreach ( FloatingObject fo in floatingObjects )
				fo.Draw ( gameTime );

			base.Draw ( gameTime );
		}
	}
}
