using PlanetExpress.items.enemys.masters;
using System;
using System.Drawing;
using Math2D;
using Math2D.Shapes;
using System.Reflection;

namespace PlanetExpress.items.enemys {
	public enum EnemyPosition { Left, Right };
	
	public class EnemyClass : PersonClass {
		
		public EnemyClass() : base() {
			FMaster = new BaseEnemyMaster(this);			
			Setposition("right");
		}
		
		public override void Init () {
			HP = (int)(HP * HPScale);
		}
		
		public override bool Action (double time) {
			Master.Action(time);
			base.Action(time);
			if (!Fixed) {
				switch (Position) {
					case EnemyPosition.Right:
						X -= 100f * (float)time;
						return X > - BaseSize * 2f * SizeScale;
					case EnemyPosition.Left:
						X += 100f * (float)time;
						return X < GameApp.ScreenWidth + BaseSize * 2f * SizeScale;
					default:
						return false;
				}
			} else {
				return true;
			}
		}
		
		public int HP;
		public override void Demage (int v) {
			HP -= v;
		}
		
		
		protected float ColorR = 1f;
		protected float ColorG = 1f;
		protected float ColorB = 1f;
		public virtual void Setcolor(string val) {
			try {
				int n = Convert.ToInt32(val, 16);
				Color c = Color.FromArgb(n);
				ColorR = c.R/256f;
				ColorG = c.G/256f;
				ColorB = c.B/256f;
			} catch { }
		}
		
		protected float Str2Float(string v, float def) {
			try {
				float n;
				v = v.Replace(",", System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator);
				v = v.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator);
				if (float.TryParse(v, out n)) {
					return n;
				}	
			} catch {				
			}
			return def;	
		}	
		
		public float BaseSize = 10f;
		public float SizeScale = 1f;
		public virtual void Setsize(string val) {			
			SizeScale = Str2Float(val, 1f);
		}
		
		public float HPScale = 1f;
		public virtual void Sethp(string val) {
			HPScale = Str2Float(val, 1f);
		}
		
		public float SpeedScale = 1f;
		public virtual void Setspeed(string val) {
			SpeedScale = Str2Float(val, 1f);
		}
		
		BaseEnemyMaster FMaster;
		public BaseEnemyMaster Master {
			get { return FMaster; }	
		}
		
		public virtual void Setmaster(string val) {
			Type mc = Assembly.GetExecutingAssembly().GetType(val);
			if (mc==null) {
				Console.Error.WriteLine("Master {0} not found", val);
				return;
			}
			ConstructorInfo ctor = mc.GetConstructor(new Type[]{typeof(EnemyClass)});
			FMaster = (BaseEnemyMaster)ctor.Invoke(new object[]{this});
		}
		
		public virtual void Setweapon(string val) {
			Type mc = Assembly.GetExecutingAssembly().GetType(val);
			if (mc==null) {
				Console.Error.WriteLine("weapon {0} not found", val);
				return;
			}
			ConstructorInfo ctor = mc.GetConstructor(new Type[]{typeof(CustomGameObject)});
			Weapon = (weapon.CustomWeapon)ctor.Invoke(new object[]{this});
		}
		
		public virtual void Setweapon2(string val) {
			Type mc = Assembly.GetExecutingAssembly().GetType(val);
			if (mc==null) {
				Console.Error.WriteLine("weapon2 {0} not found", val);
				return;
			}
			ConstructorInfo ctor = mc.GetConstructor(new Type[]{typeof(CustomGameObject)});
			Weapon2 = (weapon.CustomWeapon)ctor.Invoke(new object[]{this});
		}
		
		protected EnemyPosition Position = EnemyPosition.Right;
		public virtual void Setposition(string val) {
			switch (val.ToLower()) {
				case "left": Position = EnemyPosition.Left;
					X = -BaseSize * SizeScale * 2;
					break;
				case "right": Position = EnemyPosition.Right;
					X = GameApp.ScreenWidth + BaseSize * SizeScale * 2;
					break;
			}
		}
		
		bool FFixed = false;
		public bool Fixed {
			get { return FFixed; }	
		}
		public virtual void Setfixed(string val) {
			bool b = false;
			if (bool.TryParse(val, out b)) {
				FFixed = b;	
			} else {
				FFixed = false;	
			}			
		}
		
		public override void Die () {
			FMaster.Die();
		}
		
		public override bool Shoot () {
			switch (Position) {
			case EnemyPosition.Left:
				return Weapon.Shoot(X, Y, 0f);				
			case EnemyPosition.Right:
				return Weapon.Shoot(X, Y, (float)Math.PI);				
			}
			return false;
		}
	}
}
