﻿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 Column level references of all the cells. This will perform all column level functions
	* like, insert column, delete column and sorting the cells.
	* @author Devender Gupta
	*/
	public class ColumnReference extends EventDispatcher{
		
		private var mcRef:MovieClip;
		private var objConfigXML:XML;
		private var xmlCaptivateParams:XML;
		
		private var objWidgetController:WidgetController;
		private var arrRowReferences:Array;
		private var arrColumnReferences:Array;
		
		public var arrCellsReference:Array;
		public var currentSelectedCell:Number = -1;
		public var columnWidth:Number = 0;
		public var xPos:int = 0;
		public var index:int;
		public var columnSelected:Boolean = false;
		private var objColumnHeaderStack:ColumnHeaderStack;
		public const UPDATEWIDTH = "updateWidth"
		public function ColumnReference(){
			
		}
				
		
		/**
		* This function set the reference of the main movieclip in which the cells will be created.
		*/
		public function setReferences(mc:MovieClip, arrCols:Array):void{
			objWidgetController = WidgetController.getInstance();
			objConfigXML = objWidgetController.objConfigXML;
			xmlCaptivateParams = objWidgetController.xmlCaptivateParams;
			mcRef = mc;
			arrColumnReferences = arrCols;
		}
		
		/**
		* This function is used to initiates the class.
		*/
		public function init(index:int):void{
			arrCellsReference = new Array();
			columnWidth = objConfigXML.column.@width;
			xPos = int(objConfigXML.rowColumnGrid.@xPos) + int(objConfigXML.rowColumnGrid.@rowWidth);
			objColumnHeaderStack = ColumnHeaderStack.getInstance();
			this.index = index;
		}
		
		/**
		* This function is used to store the references of all the rows in the class.
		*/
		public function setRowReferences(arrRows:Array):void{
			arrRowReferences = arrRows;
		}
		
		/**
		* This function is used to create column at any index value when called by ColumnClass.
		*/
		public function createColumnAt(index:int):void{
			for(var i=0; i<arrRowReferences.length; i++){
				var objCellClass = new CellClass();
				objCellClass.setColumnReference(this);
				objCellClass.setRowReference(arrRowReferences[i]);
				objCellClass.txtContent = xmlCaptivateParams.table.column[index].row[i].cell
				objCellClass.init(columnWidth);
				
				if(i>0){
					objCellClass.y = arrCellsReference[i-1].y + arrCellsReference[i-1].Height;
					objCellClass.setHeaderStyle(false);
					
				}else{
					objCellClass.y = int(objConfigXML.rowColumnGrid.@yPos) + int(objConfigXML.rowColumnGrid.@columnHeight);
					if(xmlCaptivateParams.table.column[index].row[i].cell == ""){
						objCellClass.txtContent = "<font color='#ffffff' face='Arial' size='12'><p align='center'>Column</p></font>"//objColumnHeaderStack.drawNext();
					}
					objCellClass.setHeaderStyle(true);
					objCellClass.addEventListener(objCellClass.UPDATEWIDTH, columnResized);
				}
				if(index>0){
					xPos = arrColumnReferences[index-1].xPos + arrColumnReferences[index-1].columnWidth
				}
				objCellClass.x = xPos
				objCellClass.Height = arrRowReferences[i].rowHeight
				objCellClass.addEventListener(objCellClass.SELECTED, mcRef.cellSelected);
				mcRef.addChild(objCellClass)
				if(i==0){
					objCellClass.makeDraggable();
				}
				arrCellsReference.push(objCellClass);
			}
			
		}
		
		public function deleteCellsAt(ind:int){
			trace(arrRowReferences.length);
			for(var i=0; i<arrRowReferences.length; i++){
				var objCellClass = arrRowReferences[i].arrCellsReference.splice(ind, 1)[0];
				if(i==0){
					objColumnHeaderStack.putBack(objCellClass.txtContent)
				}
				mcRef.removeChild(objCellClass)
				objCellClass = null;
			}
			arrCellsReference = new Array();
		}
		
		
		public function columnChanged(){
			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 columnResized(evt:Event):void{
			var newWidth = evt.currentTarget.Width;
			var prevWidth = 0;
			for(var i=0; i<arrCellsReference.length; i++){
				if(arrCellsReference[i] != evt.currentTarget){
					prevWidth = arrCellsReference[i].Width
					arrCellsReference[i].Width = newWidth;
				}
			}			
			for(i=index+1; i<arrColumnReferences.length; i++){				
				arrColumnReferences[i].shift(newWidth - prevWidth);
			}
			columnWidth = newWidth
			dispatchEvent(new Event(UPDATEWIDTH));
		}
		
		public function shift(pixels:Number):void{
			xPos = xPos + pixels;
			for(var i=0; i<arrCellsReference.length; i++){
				arrCellsReference[i].x = xPos;
			}
		}
		
		private function columnHeaderWidthUpdated(evt:Event):void{
			
		}
		
	}
}