/**
 * @author bruno DA SILVA
 * @license GPL V3
 * 
 * 
 
 	This file is part of weukerDefense.js

    weukerDefense.js is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    weukerDefense.js is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with weukerDefense.js.  If not, see <http://www.gnu.org/licenses/>.

 */
var VagueDeWeuks = Class.create({
	vagueCount : 0
	, weukCount : 0
	, initialize: function(tabPath){
		this.tabPath = tabPath;
		this.actif = false;
		this.weukActif = false;
		this.tabWeuk = new Array();
	}, startVague: function(){
		if (this.actif) {
			return;
		}
		this.actif = true;
		$('nbVagues').update(this.tabWeuk.length + ' Vagues');
		var self = this;
		this.periodicalE = new PeriodicalExecuter(function(pe) {
			if (self.weukCount > conf.getWeukPerVague(self.vagueCount) ) {
				self.actif = false;
				self.StoppedDate = new Date();
				self.waitForNextVague()
			} else if(self.actif) {
				var weuckIndex = parseInt(self.weukCount)
				self.tabWeuk.push(new Weuck(self.tabPath, weuckIndex));
				self.weukCount++
			}
			//console.debug('called');
		}, conf.getDelayBetweenWeuk(this.vagueCount));
	}, waitForNextVague: function() {
		if (this.actif == false && this.weukActif == false) {
			//console.debug('weuk et vague inactifs');
			
		}
		this.vagueCount++;
	}, avance: function() {
		if (this.tabWeuk.length == 0) {
				return true;
			}
		this.weukActif = false
		this.tabWeuk.each(function(weuk, weukIndex) {
			weuk.avance();
			if (weuk.actif == true) {
				this.weukActif = true;
			}
		}, this);
		if (this.weukActif == false && this.actif == false) {
			this.waitForNextVague();
			return false;
		}
		return true;	
	}, get: function(cssId) {
		for(var i = 0; i < this.tabWeuk.length; i++) {
			if (this.tabWeuk[i].toCssId() == cssId) {
				return this.tabWeuk[i];
			}
		}
		return  null;
	}, reset: function() {
		this.tabWeuk.each(function(weuk, weukIndex) {
			weuk.remove();
		}, this);
		this.tabWeuk = new Array();
		this.weukCount = 0;
	}
});

var Weuck = Class.create({
	initialize: function(tabPath, index){
		this.index = index;
		this.coord = new Coord();
		var elmt = new Element('div', { 
			'class': this.toCssClass()
			, 'id': this.toCssId()
		});	
		$("gameContent").appendChild(elmt);
		
		this.tabPath = tabPath;
		this.currPathIndex = 0;
		this.actif = true;
		this.lifePoint = 5;
		this.setCoord(this.tabPath.first().start.x - 10, this.tabPath.first().start.y); 
		this.nexPath();
		this.goldOnExplode = 6;
		
	}, getSpeed: function(index, pathData) {
		return conf.getWeukSpeed(index, Math.abs(pathData.start.x-pathData.end.x) + Math.abs(pathData.start.y-pathData.end.y));
	}, setCoord: function(x, y) {
		this.coord.x = x;
		this.coord.y = y;
		$(this.toCssId()).setStyle({
		  top: this.coord.y+'px',
		  left: this.coord.x +'px'
		});
	}, toCssClass : function() {
		return 'weuk '+this.index
	}, toCssId : function() {
		return 'weuk_'+this.index
	}, arrived: function() {
		alert('un weuk est arrivé');
	}, nexPath: function() {
		this.currPathCoord = this.tabPath.clone().slice(this.currPathIndex).first();
		this.currPathIndex++;
	}, avance: function() {
		var deplacement = conf.getWeukMoveVelocity(this.currPathCoord, this.coord)
		if (deplacement.y == 0 && deplacement.x == 0) {
			this.nexPath();
			if (this.currPathCoord == null || this.currPathCoord == undefined) {
				this.actif = false;
				return false;
			}
		}
		this.setCoord(deplacement.x + this.coord.x, deplacement.y + this.coord.y);
		return true;
	}, shooted : function(specialClass) {		
		if (specialClass == 'freeze') {
			this.lifePoint = this.lifePoint - 3;
		} else {
			this.lifePoint--;
		}
		
		if (this.lifePoint < 0) {
			this.explode();
		}
	}, explode: function() {
		if (this.actif != false) {
			this.actif = false;
			conf.addGold(parseInt(this.goldOnExplode));
		}
		//$(this.toCssId()).update('badaboum!');
		Effect.Puff($(this.toCssId()), { duration: .3 , limit: 1});
	}, remove: function() {
		$(this.toCssId()).remove();
	}
});