﻿package TableWidget{
	

	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import fl.containers.ScrollPane;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	/**
	* This class is used to create row level references of all the cells. This will perform all row level functions
	* like, insert row, delete row.
	* @author Devender Gupta
	*/
	public class RowReference extends EventDispatcher{
		
		private var mcRef:MovieClip;
		private var objConfigXML:XML;
		private var xmlCaptivateParams:XML;
		
		private var objWidgetController:WidgetController;
		private var arrColumnReferences:Array;
		public var arrRowReferences:Array;
		public var arrCellsReference:Array;
		
		public var rowHeight:Number = 0;
		public var yPos:Number = 0;
		public var index:int;
		public var rowSelected:Boolean = false;
		public var currentSelectedCell:Number = -1;
		public var currentSelectedColumn:ColumnReference;
		
		public const UPDATEHEIGHT = "updateHeight";
		
		public function RowReference(){
			
		}
				
		
		/**
		* This function set the reference of the main movieclip in which the cells will be created.
		*/
		public function setReferences(mc:MovieClip, arrRows:Array):void{
			objWidgetController = WidgetController.getInstance();
			objConfigXML = objWidgetController.objConfigXML;
			xmlCaptivateParams = objWidgetController.xmlCaptivateParams;
			mcRef = mc;
			arrRowReferences = arrRows;
		}
		
		/**
		* This function is used to initiates the class.
		*/
		public function init(index:int):void{
			arrCellsReference = new Array();
			rowHeight = objConfigXML.row.@height;
			yPos = int(objConfigXML.rowColumnGrid.@yPos) + int(objConfigXML.rowColumnGrid.@columnHeight)
			this.index = index;
		}
		
		/**
		* This function is used to store the references of all the columns in the class.
		*/
		public function setColumnReferences(arrColumns:Array):void{
			arrColumnReferences = arrColumns;
		}
		
		
		/**
		* This function is used to create row at any index value when called by RowClass.
		*/
		public function createRowAt(index:int):void{
			
			/*for(var i=index+1; i<arrRowReferences.length; i++){
				arrRowReferences[i].index ++;
				trace(arrRowReferences[i].index);
				
			}*/
			for(var i=0; i<arrColumnReferences.length; i++){
				var objCellClass:CellClass = new CellClass();
				objCellClass.setColumnReference(arrColumnReferences[i]);
				objCellClass.setRowReference(this);
				//
				trace("create cell");
				objCellClass.BgColor=xmlCaptivateParams.table.column[i].row[index].cell[0].@bgColor
				//
				objCellClass.txtContent = xmlCaptivateParams.table.column[i].row[index].cell
				objCellClass.init(arrColumnReferences[i].columnWidth);
				if(i>0){
					objCellClass.x = arrCellsReference[i-1].x + arrCellsReference[i-1].Width;
				}else{
					objCellClass.x = int(objConfigXML.rowColumnGrid.@xPos) + int(objConfigXML.rowColumnGrid.@rowWidth);
				}
				objCellClass.setHeaderStyle(false);
				if(index>0){
					yPos = arrRowReferences[index-1].yPos + arrRowReferences[index-1].rowHeight
				}
				objCellClass.y = yPos
				objCellClass.Width = arrColumnReferences[i].columnWidth
				if(!objCellClass.hasEventListener(objCellClass.UPDATEHEIGHT)){
					objCellClass.addEventListener(objCellClass.UPDATEHEIGHT, rowResized);
				}
				objCellClass.addEventListener(objCellClass.SELECTED, mcRef.cellSelected);
				mcRef.addChild(objCellClass)
				arrCellsReference.push(objCellClass);
				
			}
		}
		
		public function deleteCellsAt(ind:int){
			
			for(var i=0; i<arrColumnReferences.length; i++){
				var objCellClass = arrColumnReferences[i].arrCellsReference.splice(ind, 1)[0];
				mcRef.removeChild(objCellClass)
			}
			arrCellsReference = new Array();
		}
		public function rowChanged():void{
			if(currentSelectedCell != -1){
				arrCellsReference[currentSelectedCell].setSelection(true);
			}
		}
		
		public function addEvents():void{
			for(var i=0; i<arrCellsReference.length; i++){
				if(!arrCellsReference[i].hasEventListener(arrCellsReference[i].UPDATEHEIGHT)){
					arrCellsReference[i].addEventListener(arrCellsReference[i].UPDATEHEIGHT, rowResized);
				}
				if(!arrCellsReference[i].willTrigger(arrCellsReference[i].SELECTED)){
					arrCellsReference[i].addEventListener(arrCellsReference[i].SELECTED, mcRef.cellSelected);
				}
			}
		}
		
		private function rowResized(evt:Event):void{
			
			var newHeight = evt.currentTarget.Height;
			var prevHeight = 0;
			
			var maxHeight = 0;
			for(var i=0; i<arrCellsReference.length; i++){
				arrCellsReference[i].wrapText();
				maxHeight = Math.max(arrCellsReference[i].Height, maxHeight);
			}
			for(i=0; i<arrCellsReference.length; i++){
				arrCellsReference[i].Height = maxHeight;
			}
			var newPosition = arrRowReferences[index].yPos + maxHeight;
			var prevPosition;
			if(arrRowReferences[index+1] != null){
				prevPosition = arrRowReferences[index+1].yPos;
			}else{
				prevPosition = arrRowReferences[index].yPos;
			}

			for(i=0; i<arrRowReferences.length; i++){				
				if(i>index){
					arrRowReferences[i].shift(newPosition - prevPosition);
				}
			}
			rowHeight = maxHeight;
			arrRowReferences[index].currentSelectedColumn = evt.currentTarget.columnRef
			dispatchEvent(new Event(UPDATEHEIGHT));
		}
		
		public function shift(pixels:Number):void{
			yPos = yPos + pixels;
			for(var i=0; i<arrCellsReference.length; i++){
				arrCellsReference[i].y = yPos;
			}
		}
		
		
	}
}