package classes.matuto.utils.form
{
	import br.com.ninja.debug.NinjaDebug;
	
	import flash.display.Sprite;
	import flash.text.TextField;

	public class HtmlTextField extends Sprite
	{
		
		private const _break	:String = "%0D%0A";
		
		public var textField	:TextField;
		public var tags_vec		:Vector.<Object> = new Vector.<Object>();
		
		private var info		:Object = new Object();
		
		public function HtmlTextField(textField :TextField)
		{
			this.textField = textField;
			
			addChild(textField);
		}
		
		public function getInfo(tag :String) :Array
		{
			return info[tag];
		}
		
		public function set text(value :String) :void
		{
			var text_length :int = value.length;
			var total_tags	:int = tags_vec.length;
			
			//provisorio
			//var tempo_inicial :int = getTimer();
			
			var id			:int 	= 0;
			var final_text	:String = value;
			
			for(var i :int=0; i<text_length; i++){
				
				for(var j:int=0; j<total_tags; j++){
					
					var info :Object = tags_vec[j];
					
					var start_tag	:String = "<" + info.tag;
					var finish_tag	:String = "</" + info.tag + ">";
					var current 	:String = value.substring(i, i + start_tag.length);
					
					if (current==start_tag){
						
						var find			:Boolean = true;
						var finish_pos_tag 	:int = value.indexOf(finish_tag, i);
						
						if(finish_pos_tag<0) {
							find = false;
							finish_pos_tag = value.indexOf("/>", i) - 4;
						}
						
						var first 	:int = i;
						var last	:int = finish_pos_tag+finish_tag.length;
						
						var tag_str :String = value.substring(first, last);
						
						var attributes :Object = getAttributes(tag_str, find);
						
						if(attributes.height){
							
							var space :Number = Number(attributes.vspace)*2;
							
							space += (space>>1);
							
							if(isNaN(space)) space = 12;
							
							var brs	:int = Math.ceil( (Number(attributes.height) + space) / textField.getLineMetrics(0).height) - 2;
							
							var br_str 	:String = "";
							
							for (var n :int = 0; n < brs; n++ ) br_str += "<br/>";
							
							var hspace 	:String = "";
							var vspace	:String = "";
							
							if(info.tag=="img")
							{
								if(!attributes.hspace) hspace = " hspace='0'";
								if(!attributes.vspace) vspace = " vspace='0'";
							}
							
							var new_img_str :String = "<img id='" + id + "'" + hspace + vspace;
							
							/*if(info.replace==null) */final_text = final_text.replace(tag_str, tag_str.replace("<img", new_img_str)+br_str);
							//else final_text = final_text.replace(tag_str, info.replace);
							
							attributes.id = id;
							
							id++;
							
						}else{
							//IMPLEMENTAR
						}
						
						this.info[info.tag].push( attributes );
						
						if(info.replace is String) final_text = final_text.replace(tag_str, info.replace);
						
					}
					
				}
				
			}
			
			//trace(final_text);
			//trace("------------------");
			
			textField.htmlText = brFormat(final_text);
			
			//trace("Tempo: " + ( getTimer()-tempo_inicial ) );
		}
		
		
		private function getAttributes(tag :String, find :Boolean) :Object
		{
			
			var attributes_obj	:Object = new Object();
			var attributes_str 	:String;
			
			if(find)
			{
				var finish_first_tag	:int = tag.indexOf(">") + 1;
				attributes_str	 			 = tag.substring(0, finish_first_tag);
			}else attributes_str 			 = tag;
			
			
			var vec_attributes :Array = attributes_str.split(" ");
			
			var total 	:int = vec_attributes.length;
			var pos_i	:int = 0;
			
			for(var i :int=0; i<total; i++ ){
				
				var part_str :String = vec_attributes[i];
				
				var pos 	:int = part_str.indexOf("='");
				
				//if(pos==-1) pos = part_str.indexOf("='");
				
				if(pos!=-1) {
					
					var start_pos	:int = pos_i + pos + 2;
					
					var last_pos :int = attributes_str.indexOf("'", start_pos);
					
					//if(last_pos==-1) last_pos = attributes_str.indexOf("'", start_pos);
					
					var attribute_name 	:String = part_str.substring(pos, 0);
					var attribute_value	:String = attributes_str.substring(start_pos, last_pos);
					
					attributes_obj[attribute_name] = attribute_value;
				}
				
				pos_i += part_str.length + 1;
				
			}
						
			return attributes_obj;
			
		}
		
		
		private function brFormat(value :String) :String
		{
			
			var _value :String = escape(value);
			
			var lines_arr	:Array = _value.split(_break);			
			
			_value = "";
			
			var total_lines	:int = lines_arr.length;
			
			for(var i:int=0; i<total_lines; i++)
			{
				var current :String = unescape(lines_arr[i]);
				
				current += "<br/>";
				
				_value += current;
			}			
			
			return _value;
			
		}
		
		
		public function addTagFilter(tag :String, replace :String = null) :void
		{
			
			info[tag] = new Array();
			
			tags_vec.push({tag:tag, replace:replace});
		}
		
	}
}