using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;
#if WINDOWS_PHONE
using Microsoft.Devices;
#endif

namespace ShootingHero.Components
{
	public class Bullet : DrawableGameComponent
	{
		static SoundEffect attackSound;
		static Timer timer;
		static bool vibrated = false;

		Texture2D texture;
		float x, y;
		float angle;
		float speed;

		public float X { get { return x; } set { x = value; } }
		public float Y { get { return y; } set { y = value; } }
		public float Angle { get { return angle; } set { angle = value; } }
		public float Speed { get { return speed; } set { speed = value; } }

		protected Texture2D Texture { get { return texture; } set { texture = value; } }

		public Bullet ( Game game, float x, float y, float angle, float speed )
			: base ( game )
		{
			this.x = x;
			this.y = y;
			this.angle = angle;
			this.speed = speed;

			if ( attackSound == null 
#if !ANDROID
				|| attackSound.IsDisposed
#endif
				)
				attackSound = Game.Content.Load<SoundEffect> ( "Sounds/damage" );

			if ( timer == null ) timer = new Timer ( 1000 );
		}

		public override void Update ( GameTime gameTime )
		{
			x += ( float ) Math.Sin ( angle ) * 2 * speed * gameTime.ElapsedGameTime.Milliseconds / 15.0f;
			y -= ( float ) Math.Cos ( angle ) * 2 * speed * gameTime.ElapsedGameTime.Milliseconds / 15.0f;

			if ( vibrated )
			{
				timer.Update ( gameTime );
				if ( timer.CheckTimer )
				{
					GamePad.SetVibration ( PlayerIndex.One, 0, 0 );
					timer.FullReset ();
				}
			}

			base.Update ( gameTime );
		}

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

			spriteBatch.Draw ( texture, new Vector2 ( x/* - texture.Width / 2*/, y/* - texture.Height / 2*/ ),
				null, Color.White, angle,
				new Vector2 ( texture.Width / 2, texture.Height / 2 ),
				1.0f, SpriteEffects.None, 0 );

			base.Draw ( gameTime );
		}

		protected void PlaySound ()
		{
			if ( ( Game as ShootingHero ).SoundState )
				attackSound.Play ();
			if ( this is MonsterBullet )
				if ( ( Game as ShootingHero ).VibrateState )
				{
					GamePad.SetVibration ( PlayerIndex.One, 0.2f, 0.2f );
#if WINDOWS_PHONE
					VibrateController.Default.Start ( TimeSpan.FromMilliseconds ( 200 ) );
#elif ANDROID
					
#endif
					vibrated = true;
				}
		}
	}
}
