package Units

{

	/**
	 * ...
	 * @author ...
	 */
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.net.URLRequest;
	import Math;
	public class Units extends Sprite
	{
		private var nam:String;
		private var move:int;
		private var maxR:int;
		private var minR:int;
		private var unitN:String;
		private var weakA:String;
		private var weakB:String;
		private var strongA:String;
		private var strongB:String;
		private var number:int;
		private var atk:int;
		private var team:String;
		private var image:Loader;
		private var imageName:String;
		private var link:Boolean = false;
		private var linked:String = "";
		private var nodeX:Array = new Array();
		private var nodeY:Array = new Array();
		private var nodeArr:Array = new Array();
		private var head:MovieClip = new MovieClip();
		private var hitBox:MovieClip = new MovieClip();
		private var empty:Boolean = new Boolean;
		private var attacked:Boolean = new Boolean;
		private var distance:int = 0;
		private var xdmg:Boolean = new Boolean;
		private var hpBar:MovieClip = new MovieClip();

		public function Units(defa:Boolean = false, uN:String = "DEFAULT", tea:String = "RED", n:String = "DEFAULT", img:String = "DEFAULT.jpg", m:int = 0, ma:int = 0, mi:int = 0, at:int = 0,
		wA:String = "DEFAULT", wB:String = "DEFAULT", sA:String = "DEFAULT", sB:String = "DEFAULT", nu:int = 0) {
			nam = n;
			xdmg = false;
			move = m;
			maxR = ma;
			minR = mi;
			unitN = uN;
			weakA = wA;
			weakB = wB;
			strongA = sA;
			strongB = sB;
			number = nu;
			atk = at;
			imageName = img;
			team = tea;
			empty = true;
			if (defa) {
				loadDefault();
			}
			loadImage();

		}

		public function setLocale(mX:int, mY:int):void {
			this.x = mX;
			this.y = mY;
			image.x = mX;
			image.y = mY;
			hitBox.x = mX;
			hitBox.y = mY;
			hpBar.x = mX;
			hpBar.y = mY;
		}
		
		public function getHPbar():MovieClip {
			return hpBar;
		}
		
		public function updateHP():void {
			trace(this.x);
			trace (this.y + 40);
			if (number < 0) {
				number = 0;
			}
			hpBar.scaleX = (number / 100);		
		}
		//loads image, untested due to resources
		public function setLink(partner:String):void {
			link = true;
			linked = partner;
		}

		public function isLinked():Boolean {
			return link;
		}

		public function getLinked():String {
			return linked;
		}
		
		public function removeHpBar():void {
			hpBar.graphics.clear();
		}
		public function loadImage():void {

			try
			{
				image = new Loader();
				image.load(new URLRequest(imageName));
			}
			catch (err:Error)
			{
				image.load(new URLRequest("test.png"));
			}
		}

		//should return image which allows for display, use addChild( unitVarName.displayImg())
		public function displayImg():Loader {
			return image;
		}
		public function loadDefault():void {
			if (unitN == "DEFAULT") {

			} else if ( unitN == "LANCER" ) {
				move = 200;
				minR = 0;
				maxR = 50;
				atk = 25;
				number = 100;
				if (team == "RED") {
					imageName = "RedLancer.png";
				} else {
					imageName = "BluLancer.png";
				}
				weakA = "ARCHER";
				weakB = "CATAPULT";
				strongA = "KNIGHT";
				strongB = "THEIF";
			} else if ( unitN == "KNIGHT" ) {
				move = 50;
				minR = 0;
				maxR = 50;
				atk = 25;
				number = 100;
				if (team == "RED") {
					imageName = "RedKnight.png";
				} else {
					imageName = "BluKnight.png";
				}
				weakA = "ARCHER";
				weakB = "LANCER";
				strongA = "THEIF";
				strongB = "FLAGBEAR";
			} else if ( unitN == "ARCHER" ) {
				move = 50;
				minR = 50;
				maxR = 250;
				atk = 10;
				number = 100;
				if (team == "RED") {
					imageName = "RedArcher.png";
				} else {
					imageName = "BluArcher.png";
				}
				weakA = "KNIGHT";
				weakB = "FLAGBEAR";
				strongA = "LANCER";
				strongB = "CATAPULT";

			} else if ( unitN == "FLAGBEAR" ) {
				move = 50;
				minR = 0;
				maxR = 50;
				atk = 0;
				number = 100;
				if (team == "RED") {
					imageName = "RedFlagb.png";
				} else {
					imageName = "BluFlagb.png";
				}
				weakA = "NULL";
				weakB = "NULL";
				strongA = "NULL";
				strongB = "NULL";
			} else if ( unitN == "THEIF") {
				move = 100;
				minR = 0;
				atk = 25;
				number = 100;
				maxR = 12;
				if (team == "RED") {
					imageName = "RedTheif.png";
				} else {
					imageName = "BluTheif.png";
				}
				weakA = "CATAPULT";
				weakB = "LANCER";
				strongA = "FLAGBEAR";
				strongB = "ARCHER";
			} else if ( unitN == "CATAPULT") {
				move = 25;
				minR = 25;
				maxR = 200;
				atk = 100;
				number = 100;
				if (team == "RED") {
					imageName = "RedCata.png";
				} else {
					imageName = "BluCata.png";
				}
				weakA = "NULL";
				weakB = "NULL";
				strongA = "NULL";
				strongB = "NULL";
			}
						if(team == "RED"){

				hitBox.graphics.beginFill(0xFF0000, .25);;
			} else {
				hitBox.graphics.beginFill(0x0000FF, .25);
			}
			hitBox.graphics.lineStyle(1, 0x000000);
			hitBox.graphics.drawEllipse(this.x + 25  - maxR, this.y + 25 - maxR, maxR * 2, maxR * 2);
			hitBox.graphics.lineStyle(0);
			hitBox.graphics.drawEllipse(this.x + 25 - minR, this.y + 25 - minR, minR * 2, minR * 2);
			hitBox.graphics.endFill();
			hpBar.graphics.beginFill(0x00FF00);
			hpBar.graphics.drawRect(this.x, this.y + 40, 50 * Number(number / 100), 10);
			hpBar.graphics.endFill();
		}

		public function loadHitBox():MovieClip {
			return hitBox;
		}

		public function clearHitBox():void {
			hitBox.graphics.clear();
		}
		public function getMove():int {
			return move;
		}

		public function xtraDmg():Boolean {
			return xdmg;
		}

		public function enableDmg():void {
			xdmg = true;
		}

		public function getHP():int {
			return number;
		}

		public function getMar():int {
			return maxR;
		}

		public function getMir():int {
			return minR;
		}

		public function getAtk():int {
			return atk;
		}

		public function isStrong(untN:String):Boolean {
			if (strongA == untN || strongB == untN) {
				return true;
			}
			return false;
		}

		public function isWeak(untN:String):Boolean {
			if (weakA == untN || weakB == untN ) {
				return true;
			}
			return false;
		}

		public function getName():String {
			return unitN;
		}

		public function takeDamage(dmg:int):void {
			number = number - dmg;	
			attacked = true;
			updateHP();
		}

		public function isDead():Boolean {
			if (number < 1) {
				return true;
			}
			return false;
		}

		public function nodSz():int {
			return nodeArr.length;
		}

		public function getNode(index:int):MovieClip {
			return nodeArr[index];
		}

		public function isEmpty():Boolean {
			return empty;
		}

		public function removeImg():void {
			image.unload();
		}

		//this is for visual purposes ONLY

		public function createNode(mX:int, mY:int):MovieClip {
			nodeX.push(mX);
			nodeY.push(mY);
			var tempImg:MovieClip = new MovieClip();
			empty = false;
			tempImg.graphics.clear();
			tempImg.graphics.beginFill(0x000000);
			tempImg.graphics.drawRect(mX, mY, 50, 50);
			tempImg.graphics.endFill();
			nodeArr.push(tempImg);
			return(nodeArr[nodeArr.length - 1]);
		}

		public function removeNode():void {
			nodeX.pop();
			nodeY.pop();
		}

		public function distMoved():int {
			return distance;
		}

		public function moveDist(dist:int):void {
			distance = distance + dist;
		}

		public function removeFirst():void {
			nodeX.splice(0, 1);
			nodeY.splice(0, 1);
		}

		public function getNX(index:int):int {
			return (nodeX[index]);
		}

		public function getNY(index:int):int {
			return (nodeY[index]);
		}

		public function nodeSize():int {
			return nodeX.length;
		}

		public function beenAtkd():Boolean {
			if (attacked) {
				return true;
			} else {
				return false;
			}
		}
		public function clearNode():void {
			attacked = false;
			distance = 0;
			xdmg = false;
			for (var i:int = 0; i <= nodeX.length; i++) {
				nodeX.pop();
				nodeY.pop();
			}
			for (var j:int = 0; j <= nodeArr.length; j++) {
				nodeArr.pop();
			}
			empty = true;
		}

		public function clearSq():void {
			for (var j:int = 0; j < nodeArr.length; j++) {
				nodeArr[j].graphics.clear();
			}
		}

		//This is for hit testing
		//I wonder if the user will notice a difference in 5 px
		//I also wonder why no programming language has a built in function to calc the distance between two lines
		//I mean, seriously.
		public function hitTest(xCoord:int, yCoord:int):Boolean {
			var xT:int = xCoord;
			var yT:int = yCoord;
			var xO:int = image.x + 25;
			var yO:int = image.y + 25;
			var dist:Number = Math.sqrt(Math.pow(xT - xO, 2) + Math.pow(yT - yO, 2));
			trace(dist);
			if (dist <= maxR + 25 && dist >= minR - 25) {
				return true;
			}
			return false;
		}

	}

}

