/**
 * @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 TowerCollection = Class.create({
	tabTowers : new Array()
	, shootRadius: 10
	, choosedTowerType: 'default'
	, initialize: function() {
		this.existingTowers = new Array();
		this.existingTowers['default'] = new Tower();
		this.existingTowers['toggleTower1'] = new Tower();
		this.existingTowers['toggleTower2'] = new RapidTower();
		this.existingTowers['toggleTower3'] = new SlowTower();
		this.existingTowers['toggleTower4'] = new FreezeTower();

			
		var self = this;
		$("gameContent").observe('mousedown', function(event) {			
			self.putTower(event);
		});
	}, putTower: function(event) {
		//console.debug(event);
		var clone = Object.clone(this.existingTowers[this.choosedTowerType]);
		if (conf.getGold() >= clone.getGoldCost()) {
			clone.init(this.tabTowers.length, event);		
			conf.addGold(-parseInt(clone.getGoldCost()));
			this.tabTowers.push(clone);
			$('nbTours').update(this.tabTowers.length + ' Towers');
		} else {
			alert('cette tour coute : '+clone.getGoldCost()+', vous n\'avez que : '+conf.getGold()+' golds.');
		}
		
	}, shoot: function(weukCollection) {
		weukCollection.tabWeuk.each(function(weuk, weukIndex) {
			if (weuk.actif == true) {
				this.tabTowers.each(function(tower, towerIndex) {
					if (tower.isCoordInShootRange(weuk.coord)) {
						tower.shoot(weuk);
					}
				})
			}
		}, this);
	}, get: function(cssId) {
		for(var i = 0; i < this.tabTowers.length; i++) {
			if (this.tabTowers[i].toCssId() == cssId) {
				return this.tabTowers[i];
			}
		}
		return  null;
	}, toggleChoosedTowerType: function(towerType) {
		this.choosedTowerType = towerType
	}
});

var Tower = Class.create({

	initialize: function() {
		this.radius = 100;
		this.maxConcurrentShoot = 3;
		this.goldCost = 100;
		this.shootClass = 'normal';
  	},
	
	init: function(index, event) {
		
		this.index	= index;
		this.coord = new Coord(event.layerX, event.layerY)
		this.shootCount = 0;
		this.goldCost = 100;
		
		var elmt = new Element('div', {
			'class': this.toCssClass()
			, 'id': this.toCssId()
		});	
		$("gameContent").appendChild(elmt);
		
		new Effect.Morph(this.toCssId(), {
				style: 'top:'+this.coord.y+'px; left:'+this.coord.x+'px;'
				, duration: 0.1 
		});
	},
	 
	toString: function() {
		return 'Tower';
	},
	
	toCssClass : function() {
		return 'tower '+this.index+' '+this.toString();
	}, 
	
	toCssId : function() {
		return 'tower_'+this.index;
	}, 
	
	GetShootClass: function() {
		return this.shootClass;
	},
	
	getShootArea: function() {
		return this.radius;
	}, 
	
	isCoordInShootRange: function(coord) {
		delta = new Coord(Math.abs(coord.x - this.coord.x), Math.abs(coord.y - this.coord.y));
		if (
				(delta.x < this.radius && delta.y < this.radius) //si on est dans un carré compris dnas les boones coocrdonées : calcul simple, pour eliminier les coordonnées trop distantes sasn besoin de trop de ressource
			&&
				(Math.pow(delta.x, 2) +  Math.pow(delta.y, 2) <= Math.pow(this.radius, 2)) //si l'hypoténuse est <= au rayon, on est dedans
			) {
			
			return true;
		}
		return false;
	}, 
	
	shoot: function(weuk) {
		if (this.shootCount < this.maxConcurrentShoot) {
			var shoot = new Shoot(weuk.toCssId(), this.toCssId(), this.coord, this.GetShootClass());
			this.shootCount++;
		}
		
	}, 
	
	shootSuccessed: function() {
		this.shootCount--;
	},
	
	getGoldCost : function() {
		return this.goldCost;
	}
});

var RapidTower = Class.create(Tower, {
	initialize: function($super) {
		$super();
		this.radius = 40;
		this.maxConcurrentShoot = 4;
		this.goldCost = 150;
		this.shootClass = 'rapide';
  	},
	
	toString: function() {
		return 'RapidTower';
	}
});
var SlowTower = Class.create(Tower, {
	initialize: function($super) {
		$super();
		this.radius = 150;
		this.maxConcurrentShoot = 2;
		this.goldCost = 150;
		this.shootClass = 'slow';
  	},
	
	toString: function() {
		return 'SlowTower';
	}
});
var FreezeTower = Class.create(Tower, {
	initialize: function($super) {
		$super();
		this.radius = 300;
		this.maxConcurrentShoot = 1;
		this.goldCost = 50;
		this.shootClass = 'freeze';
  	},
	
	toString: function() {
		return 'FreezeTower';
	}
});
