Ext.namespace('fcbm.model');

/*
 * Extends Ext.util.Observable
 */
fcbm.model.Document = function (type, name, circumstances, baseUrl, codeCountry) {
	
	this.type = type;
	this.name = name;
	this.circumstances = circumstances;
	this.clinicPresentation = '';
	this.questions = '';
	this.solComment1 = '';
	this.solComment2 = '';
	this.commentWall2 = '';
	this.numeration = new fcbm.model.Numeration();
	this.formuleLeuco = new fcbm.model.FormuleLeuco();
	this.cytoSigns = null;
	this.diagnostics = null;
	this.investigations = null;
	this.imageWall = new fcbm.model.ImageWall();
	this.reponse = {};
	this.reponse.imageWall = [];
	this.reponse.hypDiag = [];
	this.reponse.examens = [];
	this.reponse.questions = '';
	this.reponse.annoCyto = [];
	
	var defaultImageWallState = (this.type == 'training' ? false : true); 
	
	this.loadState = {
		DocumentLoaded: false,
		ImgWallLoaded: false,
		ClinicPresentationLoaded:false,
		QuestionsLoaded:false,
		SolComment1Loaded:false,
		SolComment2Loaded:false,
		CommentWall2: false,
		Ready: false
	};
	
	var varArray = new Array (
		"document.currentFile.document.loadState.DocumentLoaded",
		"document.currentFile.document.loadState.ImgWallLoaded",
		"document.currentFile.document.loadState.ClinicPresentationLoaded",
		"document.currentFile.document.loadState.QuestionsLoaded",
		"document.currentFile.document.loadState.SolComment1Loaded",
		"document.currentFile.document.loadState.SolComment2Loaded",
		"document.currentFile.document.loadState.CommentWall2"
	);
	document.loader = new fcbm.component.Loader("Chargement du document", varArray);
		
	/*
	 * updateLoadState
	 * 
	 * Permet de savoir lorsque le document est chargé complètement
	 */
	this.updateLoadState = function (event) {
		switch (event.eventName) {
			case "DocumentLoaded":
				this.loadState.DocumentLoaded = true;
				break;
			case "ImgWallLoaded":
				this.loadState.ImgWallLoaded = true;
				break;
			case "ClinicPresentationLoaded":
				this.loadState.ClinicPresentationLoaded = true;
				break;
			case "QuestionsLoaded":
				this.loadState.QuestionsLoaded = true;
				break;
			case "SolComment1Loaded":
				this.loadState.SolComment1Loaded = true;
				break;
			case "SolComment2Loaded":
				this.loadState.SolComment2Loaded = true;
				break;
			case "CommentWall2":
				this.loadState.CommentWall2 = true;
				break;
		}
		if (this.loadState.DocumentLoaded && this.loadState.ImgWallLoaded && this.loadState.ClinicPresentationLoaded && this.loadState.QuestionsLoaded && this.loadState.SolComment1Loaded && this.loadState.SolComment2Loaded && this.loadState.CommentWall2) {
			this.loadState.Ready = true;
			this.fireEvent('Ready', {});
		}
	}
	
	
	/*
	 * load
	 * 
	 * Charge le Document.xml à partir de l'url
	 */
	this.load = function (baseUrl) {
		
		this.imageWall.setBaseUrl (baseUrl);
		this.imageWall.setCodeCountry (codeCountry);
		
		var urlDocumentXML = baseUrl + "/Document.xml";
		var imgWallXML = baseUrl + "/IMG/ImgWalls.xml";
		var urlClinicPresentation = baseUrl + "/I18N/" + codeCountry + "/Initial/ClinicPresentation.htm";
		var urlQuestions = baseUrl + "/I18N/" + codeCountry + "/Initial/Questions.htm";
		var urlCommentWall2 = baseUrl + "/I18N/" + codeCountry + "/Initial/Init2.htm";
		var urlSolComment1 = baseUrl + "/I18N/" + codeCountry + "/Solution/SolComment1.htm";
		var urlSolComment2 = baseUrl + "/I18N/" + codeCountry + "/Solution/SolComment2.htm";
		
		// urlDocumentXML
		Ext.Ajax.request({
			url: urlDocumentXML,
			success: function (conn, response, options){
				this.parseDocumentXML (conn.responseXML);
			},
			scope: this,
			failure: function (conn, response, options){
				Ext.MessageBox.show({
					title: 'Erreur',
					msg: "fcbm.model.Document: Erreur au chargement du fichier " + urlDocumentXML,
					buttons: Ext.Msg.OK,
					icon: Ext.MessageBox.ERROR
				});
				if (document.currentFile != null) {
					document.currentFile.destroy();
					delete document.currentFile;
				}
			}
		});
		
		// imgWallXML
		Ext.Ajax.request({
			url: imgWallXML,
			success: function(conn, response, options){
				this.parseImageWallXML(conn.responseXML);
			},
			scope: this,
			failure: function(conn, response, options){
				Ext.MessageBox.show({
					title: 'Erreur',
					msg: "fcbm.model.Document: Erreur au chargement du fichier " + imgWallXML,
					buttons: Ext.Msg.OK,
					icon: Ext.MessageBox.ERROR
				});
				if (document.currentFile != null) {
					document.currentFile.destroy();
					delete document.currentFile;
				}
			}
		});
		
		
		// ClinicPresentation
		Ext.Ajax.request({
			url: urlClinicPresentation,
			success: function (conn, response, options){
				this.parseClinicPresentation (conn.responseText);
			},
			scope: this,
			failure: function (conn, response, options){
				Ext.MessageBox.show({
					title: 'Erreur',
					msg: "fcbm.model.Document: Erreur au chargement du fichier " + urlClinicPresentation,
					buttons: Ext.Msg.OK,
					icon: Ext.MessageBox.ERROR
				});
				if (document.currentFile != null) {
					document.currentFile.destroy();
					delete document.currentFile;
				}
			}
		});
		
		// Questions
		Ext.Ajax.request({
			url: urlQuestions,
			success: function (conn, response, options){
				this.parseQuestions (conn.responseText);
			},
			scope: this,
			failure: function (conn, response, options){
				Ext.MessageBox.show({
					title: 'Erreur',
					msg: "fcbm.model.Document: Erreur au chargement du fichier " + urlQuestions,
					buttons: Ext.Msg.OK,
					icon: Ext.MessageBox.ERROR
				});
				if (document.currentFile != null) {
					document.currentFile.destroy();
					delete document.currentFile;
				}
			}
		});
		
		// SolComment1
		Ext.Ajax.request({
			url: urlSolComment1,
			success: function (conn, response, options){
				this.parseSolComment1 (conn.responseText);
			},
			scope: this,
			failure: function (conn, response, options){
				Ext.MessageBox.show({
					title: 'Erreur',
					msg: "fcbm.model.Document: Erreur au chargement du fichier " + urlSolComment1,
					buttons: Ext.Msg.OK,
					icon: Ext.MessageBox.ERROR
				});
				if (document.currentFile != null) {
					document.currentFile.destroy();
					delete document.currentFile;
				}
			}
		});
		
		// SolComment2
		Ext.Ajax.request({
			url: urlSolComment2,
			success: function (conn, response, options){
				this.parseSolComment2 (conn.responseText);
			},
			scope: this,
			failure: function (conn, response, options){
				Ext.MessageBox.show({
					title: 'Erreur',
					msg: "fcbm.model.Document: Erreur au chargement du fichier " + urlSolComment2,
					buttons: Ext.Msg.OK,
					icon: Ext.MessageBox.ERROR
				});
				if (document.currentFile != null) {
					document.currentFile.destroy();
					delete document.currentFile;
				}
			}
		});
		
		
		// CommentWall2
		Ext.Ajax.request({
			url: urlCommentWall2,
			success: function (conn, response, options){
				this.parseCommentWall2 (conn.responseText);
			},
			scope: this,
			failure: function (conn, response, options){
				Ext.MessageBox.show({
					title: 'Erreur',
					msg: "fcbm.model.Document: Erreur au chargement du fichier " + urlSolComment2,
					buttons: Ext.Msg.OK,
					icon: Ext.MessageBox.ERROR
				});
				if (document.currentFile != null) {
					document.currentFile.destroy();
					delete document.currentFile;
				}
			}
		});
	}
	
	/*
	 * parseDocumentXML
	 * 
	 * Initialise une partie de l'objet Document à partir d'un objet 
	 * JSON représentant le fichier Document.xml
	 */
	this.parseDocumentXML = function (xml) {
		var json = eval("(" + xml2json (xml, '') + ")");
		
		// numeration
		this.numeration.parseJSON (json.Document.Atlas.Initial.DataElement.FormuleData.Item);
		// formule leucocytaire
		var jsonNodeFormuleLeuco = this.findJsonObjectById(json.Document.Atlas.Response.DataElement, 'FormuleLeuco');
		this.formuleLeuco.parseJSON (jsonNodeFormuleLeuco.FormuleData.Item);
		// diagnostics
		var jsonNodeDiagnostics = this.findJsonObjectById(json.Document.Atlas.Response.DataElement, 'Diagnostics');
		this.diagnostics = this.getListInJsonNode(jsonNodeDiagnostics);
		// cytoSigns
		var jsonNodeCytoSigns = this.findJsonObjectById(json.Document.Atlas.Response.DataElement, 'CytoSigns');
		this.cytoSigns = this.getListInJsonNode(jsonNodeCytoSigns);
		// investigations
		var jsonNodeInvestigations = this.findJsonObjectById(json.Document.Atlas.Response.DataElement, 'Investigations');
		this.investigations = this.getListInJsonNode(jsonNodeInvestigations);
		// mur d'images
		var jsonImageWallNode = jsonNodeFormuleLeuco;
		this.imageWall.parseDocumentJSON (jsonImageWallNode.GraphicalFormuleData.ImgAnnotation);

		this.fireEvent('DocumentLoaded', {eventName:'DocumentLoaded'}); 
	};
	
	/*
	 * parseImageWallXML
	 * 
	 * Initialise une partie de l'objet Document à partir d'un objet 
	 * JSON représentant le fichier ImgWall.xml
	 */
	this.parseImageWallXML = function (xml) {
		var json = eval("(" + xml2json (xml, '') + ")");
		this.imageWall.parseImagesJSON (json);
		
		this.fireEvent('ImgWallLoaded', {eventName:'ImgWallLoaded'});
	}
	
		
	/*
	 * 
	 */
	this.parseClinicPresentation = function (HTMLText) {
		this.clinicPresentation = HTMLText;
		
		this.fireEvent('ClinicPresentationLoaded', {eventName:'ClinicPresentationLoaded'});
	}
	
	/*
	 * 
	 */
	this.parseQuestions = function (HTMLText) {
		this.questions = HTMLText;
		
		this.fireEvent('QuestionsLoaded', {eventName:'QuestionsLoaded'});
	}
	
	/*
	 * 
	 */
	this.parseSolComment1 = function (HTMLText) {
		this.solComment1 = HTMLText;
		
		this.fireEvent('SolComment1Loaded', {eventName:'SolComment1Loaded'});
	}
	
	/*
	 * 
	 */
	this.parseSolComment2 = function (HTMLText) {
		this.solComment2 = HTMLText;
		
		this.fireEvent('SolComment2Loaded', {eventName:'SolComment2Loaded'});
	}
	
	/*
	 * 
	 */
	this.parseCommentWall2 = function (HTMLText) {
		this.CommentWall2 = HTMLText;
		
		this.fireEvent('CommentWall2', {eventName:'CommentWall2'});
	}
	
	
	/*
	 * Cherche dans un noeud json de type Array quels est le premier dont l'id correspond 
	 * à l'@id passé en paramètre.
	 * Renvoi le premier noeud correspondant 
	 */
	this.findJsonObjectById = function (jsonNode, id) {
		for (var i=0; i<jsonNode.length; i++) {
			var cur = jsonNode[i];
			if (cur.id == id)
				return cur;			
		}
	};
	
	/*
	 * Renvoi les noeud _text (id) des éléments de liste (List) dans un noeud json  
	 */
	this.getListInJsonNode = function(jsonNode){
		var list = new Array();
	
		var hasMultiple = true;
		try {jsonNode.List.ListRef[0].idCat;} 
		catch (error) {hasMultiple = false;}
		
		if (hasMultiple)
			for (var i = 0; i < jsonNode.List.ListRef.length; i++)
				list.push(parseInt(jsonNode.List.ListRef[i]._text));
		else
			list.push(parseInt(jsonNode.List.ListRef._text));
		return list;
	}


	// Ajoute des évenement
	this.addEvents({
		DocumentLoaded: false,
		ImgWallLoaded: false,
		ClinicPresentationLoaded: false,
		QuestionsLoaded: false,
		SolComment1Loaded: false,
		SolComment2Loaded: false,
		CommentWall2: false,
		Ready: false
	});
	this.addListener('DocumentLoaded', this.updateLoadState);
	this.addListener('ImgWallLoaded', this.updateLoadState);
	this.addListener('ClinicPresentationLoaded', this.updateLoadState);
	this.addListener('QuestionsLoaded', this.updateLoadState);
	this.addListener('SolComment1Loaded', this.updateLoadState);
	this.addListener('SolComment2Loaded', this.updateLoadState);
	this.addListener('CommentWall2', this.updateLoadState);
	
	// Lancement du chargement	
	this.load(baseUrl);


	
};

Ext.extend(fcbm.model.Document, Ext.util.Observable);



