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

namespace ShootingHero.Components.Bullets.PlayerBullet
{
	public class SentrikBullet : HeroBullet
	{
		static Texture2D [] texture = new Texture2D [ 3 ];

		bool isGuided;

		public SentrikBullet ( Game game, float x, float y, float angle, int power, bool isDrone, bool isGuided )
			: base ( game, x, y, angle, 3 )
		{
			int index = isDrone ? 2 : ( power - 1 );
			if ( texture [ index ] == null || texture [ index ].IsDisposed )
				texture [ index ] = Game.Content.Load<Texture2D> ( "Bullet/sentrik_bullet" + ( index + 1 ).ToString () );
			Texture = texture [ index ];

			Power = power;
			if ( isDrone )
			{
				IsNondestroyBullet = true;
				Speed = 2;
			}
			else
			{
				if ( this.isGuided = isGuided )
				{
					Monster nearbyMonster = null;
					float distance = 99999999999999, tempDistance;

					foreach ( Monster monster in ( ( Game.Components [ 0 ] as Transitor ).CurrentScene as GameScene ).MonsterManager.Container )
					{
						if ( monster.CheckTimeUnit ) continue;
						tempDistance = MathHelper.Distance ( new Vector2 ( X, Y ), new Vector2 ( monster.X, monster.Y ) );
						if ( tempDistance <= distance )
						{
							distance = tempDistance;
							nearbyMonster = monster;
						}
					}

					if ( nearbyMonster != null )
						Angle = ( float ) Math.Atan2 ( nearbyMonster.Y - Y, nearbyMonster.X - X ) + 3.141592f / 2;
				}
			}
		}
	}
}
