/*
 * Classe statique JS - WEBSOCKET:EVENTS
 */

/*
 * au menu : 
 * - Permet de gérer les relations entre classe via les events
 */

/*
 *  EVENTS TETRIS:GLOBAL
 */











(function(){
VLA_LIBRAIRIE.tetris.global.onMessage = function(eData){
	if(eData.evenement_joueur) { // EVENEMENT_JOUEUR
		if(eData.evenement_joueur.type == "start_game") $(document).trigger("joueur.start",[eData.evenement_joueur]);
		else if(eData.evenement_joueur.type == "stop_game") $(document).trigger("joueur.stop",[eData.evenement_joueur]);
		else if(eData.evenement_joueur.type == "un_client_bouge") $(document).trigger("joueur.bouge",[eData.evenement_joueur]);
	} 
	else if(eData.nouveau_joueur) { // NOUVEAU_JOUEUR
		$(document).trigger("game.nouveauJoueur",[eData.nouveau_joueur]);
	} 
	else if(eData.deconnexion_joueur) { // DECONNEXION_JOUEUR
		$(document).trigger("game.deconnexionJoueur",[eData.deconnexion_joueur]);
		$(document).trigger("view.game.deconnexionJoueur",[{nbFondateurs:alias.jeu.joueurs.length,client:eData.deconnexion_joueur.client}]);
	} 
	else if(eData.serveur_jeu) { // SERVEUR_JEU
		if(eData.serveur_jeu.type == "retour_validation") { // SERVEUR_JEU:RETOUR_VALIDATION
			if(eData.serveur_jeu.resultat) {
    			$(document).trigger("game.brique",[eData.serveur_jeu]);
			} else {
    			$(document).trigger("game.falseBrique",[eData.serveur_jeu]);
			}
		} else if(eData.serveur_jeu.type == "nouvelle_ligne") { // SERVEUR_JEU:NOUVELLE_LIGNE
			if(window.console&&(window.console.debug||window.console.log)) window.console.log("Nouvelle ligne, " + eData.serveur_jeu.lignes);
			$(document).trigger("game.ligne",[eData.serveur_jeu]);
		} else if(eData.serveur_jeu.type == "joueur_start_game") { // SERVEUR_JEU:JOUEUR_START_GAME
			$(document).trigger("joueur.play",[eData.serveur_jeu]);
		}
	} 
	else if(eData.serveur_hello) {
		if(eData.serveur_hello.type == "parametrage") {
			$(document).trigger("game.parametrage",eData.serveur_hello);
			$(document).trigger("view.game.nbClients",alias.jeu.joueurs.length);          	
		} 
	} else if(eData.deconnexion_joueur) {
		
	}
};
})();

/*
 *  EVENTS TETRIS:JEU
 */

(function(){ 
VLA_LIBRAIRIE.tetris.jeu.events = VLA_LIBRAIRIE.tetris.jeu.events || {};

VLA_LIBRAIRIE.tetris.jeu.events = {
		
	// -- Constructeur
	__constructor__:function() {
		self.__events();
	},
		
	// -- GLOBAL
	__events:function() {
		// -- Event type : MOUVEMENT
		// Moi
		$(document).ready(function(){
		$(document).bind("clavier.droite",alias.jeu.onClavier);
		$(document).bind("clavier.gauche",alias.jeu.onClavier);
		$(document).bind("clavier.haut",alias.jeu.onClavier);
		$(document).bind("clavier.bas",alias.jeu.onClavier);
		
		// Joueurs
		$(document).bind("joueur.bouge",self.onBouge);

		// -- Event type : GAME
		$(document).bind("game.parametrage",self.onParametrage);
		$(document).bind("game.nouveauJoueur",self.onConnexion);
		$(document).bind("game.deconnexionJoueur",self.onDeconnexion);
		$(document).bind("game.go",alias.jeu.jouer);
		$(document).bind("game.finish",alias.jeu.arreter);
		$(document).bind("game.ligne",self.onLigne);
		$(document).bind("game.brique",self.onBrique);
		$(document).bind("game.falseLigne",self.onFalseLigne);
		$(document).bind("game.falseBrique",self.onFalseBrique);
		$(document).bind('game.ajax.save',self.onSave);
		
		// -- Event type : ETAT
		// Moi
		$("#tetris_bouton_participer").click(self.onParticiper);
		
		// Joueurs
		$(document).bind("joueur.start",self.onStart);
		$(document).bind("joueur.stop",self.onStop);
		$(document).bind("joueur.play",self.onPlay);
		});
	},
	
	// -- JEU
	// Parametrage
	onParametrage:function(e,obj) {
		if(window.console&&(window.console.debug||window.console.log)) window.console.log("parametrage du jeu en amont du lancement");
		alias.jeu.monId = obj.votreId;
		alias.jeu.joueurs = obj.clientsActuels;
		alias.jeu.nombre_total_ligne = obj.nombreLigne;
		alias.jeu.initGrille(obj.grilleActuelle);
    	$count_joueurQuijouent = 0;
    	for(var i=0;i<alias.jeu.joueurs.length;i++) {
	    	if(alias.jeu.joueurs[i].sessionId != alias.jeu.monId && alias.jeu.joueurs[i].play) {
				$(document).trigger("joueur.play",{type:"start_game",client:alias.jeu.joueurs[i].sessionId,positionInitiale:alias.jeu.joueurs[i].position});
            	$count_joueurQuijouent++;
	    	}
	    }
    	if(window.console&&(window.console.debug||window.console.log)) window.console.log("Il y a actuellement déjà " + (alias.jeu.joueurs.length-1) + " joueurs connectés & " + $count_joueurQuijouent + " en train de jouer");  
	},
	
	// Mouvement
	onBouge:function(e,obj) {
		alias.jeu.bougerBrique(obj.client,obj.position);
	},
	
	// Brique
	onBrique:function(e,obj) {
		alias.jeu.onToucheValidation(obj.client,obj.updateGrille);
    	if(obj.client == alias.jeu.monId) { 
    		alias.view.dialog.load({type:"formulaire",html:alias.formulaire.brique_enregistrement,delay:3000,desactivation:true,mouseSensible:false});
    	}
	},
	
	onFalseBrique:function(e,obj) {
		if(window.console&&(window.console.debug||window.console.log)) window.console.log("Impossible case déjà prise");
		alias.jeu.onToucheNonValidation();
	},
	
	// Ligne
	onLigne:function(e,obj) {
		alias.jeu.nombre_total_ligne = obj.lignes;
		alias.jeu.updateGrille(obj.grilleActuelle);
		alias.jeu.onLigne();
		alias.view.disparitionLigne();
	},
	
	onFalseLigne:function(e,obj) {
		alias.jeu.updateGrille(obj.grilleActuelle);
		alias.jeu.onLigne();
	},
	
	// -- JOUEUR
	onStart:function(e,obj) {
		
	},
	
	onStop:function(e,obj) {
	},
	
	onPlay:function(e,obj) {
		if(window.console&&(window.console.debug||window.console.log)) window.console.log("Le joueur " + obj.client + " commence à jouer en " + obj.positionInitiale.x + " & " + obj.positionInitiale.y);
		alias.jeu.ajouterBrique(obj.client,obj.positionInitiale);
	},
	
	onParticiper:function(e) {
		if(alias.global.connecte) alias.jeu.actionUI({"type":"toogleStartStop"});
		else alias.global.socket.connect();
	},
	
	onConnexion:function(e,obj) {
		if(window.console&&(window.console.debug||window.console.log)) window.console.log("Connexion du joueur  " + obj.client);
		alias.jeu.joueursDejaPresents = obj.clientsActuels;
		$(document).trigger("view.game.nouveauJoueur",[{nbFondateurs:alias.jeu.joueursDejaPresents.length}]);
	},
	
	onDeconnexion:function(e,obj) {
		if(window.console&&(window.console.debug||window.console.log)) window.console.log("Déconnexion du joueur " + obj.client);
		alias.jeu.joueurs = obj.clientsActuels;
		alias.jeu.onDeconnexion({client:obj.client});
	},
	
	onSave:function(e) {
		$(document).ready(function(){alias.view.dialog.load({url:"img/tetris/walloffame.png",delay:300,type:"image",cursor:"pointer",desactivation:true},function(){
			alias.view.dialog.desactiver();
		})});
	}
	
	// -- SERVEUR
	
};

var self = VLA_LIBRAIRIE.tetris.jeu.events;
self.__constructor__();

})(); 



/*
 *  EVENTS TETRIS:VIEW
 */

(function(){ 
VLA_LIBRAIRIE.tetris.view.events = VLA_LIBRAIRIE.tetris.view.events || {};

VLA_LIBRAIRIE.tetris.view.events = {
	__contructor__:function() {
		self.__events();
	},

	__events:function() {
		$(document).ready(function(){
		$(document).bind("game.go",function(){
			alias.view.elementVisibility("tetris_pole_soutient");
		});
		
		// -- Event type : GAME
		$(document).bind("view.game.nouveauJoueur",self.onConnexion);
		//$(document).bind("view.game.deconnexionJoueur",self.onDeconnexion);
		$(document).bind("view.game.nbClients",self.onChangeNbClient);
		$(document).bind("view.game.ligne",self.onLigne);
		$(document).bind("view.game.ready",self.onReady);
		
		});
		
		// -- Event type : FORMULAIRE
		$(document).bind("view.formulaire.ready",self.onFormulaireReady);
	},
	
	onReady:function() {
		// -- Les briques et les éléments sont tous en place
		$(".brique").mouseenter(alias.view.onBriqueEnter);
		$(".brique").mouseleave(alias.view.onBriqueLeave);
		$(".brique").click(alias.view.onBriqueClick);
    	alias.view.header("nombredeligne",alias.jeu.nombre_total_ligne);
	},
	
	onConnexion:function(e,obj) {
    	alias.view.header("nombreutilistateur",obj.nbFondateurs); 
	},
	
	onDeconnexion:function(e,obj) {
    	alias.view.header("nombreutilistateur",obj.nbFondateurs);
    	alias.view.tetrisWarpSuppression(obj.client);
	},
	
	onChangeNbClient:function(e,obj) {
    	alias.view.header("nombreutilistateur",obj);
	},
	
	onLigne:function(e) {
    	alias.view.header("nouvelleligne",null);
    	setTimeout(function() {
    		alias.view.header("nombredeligne",null);
    	}, 3000);
	},
	
	onFormulaireReady:function(e) {
		$(document).ready(function(){alias.view.dialog.load({url:"img/utils/tetris_maintenance.png",type:"image",cursor:"pointer",desactivation:true,position:{x:182,y:50}})});
		//$(document).ready(function(){alias.view.dialog.load({url:"img/utils/msg_devenezFondateur.png",type:"image",cursor:"pointer",desactivation:true,position:{x:182,y:50}},alias.jeu.demarrer)});
	}
};

var self = VLA_LIBRAIRIE.tetris.view.events;
self.__contructor__();
})(); 

