/*******************************************************************************
 * IBM Confidential
 * 
 * OCO Source Materials
 * 
 * A and PM: PD
 * 
 * (c) Copyright IBM Corp. 2014
 * 
 * The source code for this program is not published or otherwise divested of
 * its trade secrets, irrespective of what has been deposited with the U.S.
 * Copyright Office.
 ******************************************************************************/

define([
	"dojo/_base/declare", 
	"dojox/grid/EnhancedGrid",
	"dojo/_base/array",
	"dojo/query",
	"dojo/dom-style",
	"dojo/dom-attr",
	"dojo/_base/html",
	"dijit/Tooltip",
	"dojox/grid/enhanced/plugins/Selector"
],function(declare, EnhancedGrid, array, query, domStyle, domAttr, html){
	var PreviewGrid = declare("pd/widgets/PreviewGrid", [EnhancedGrid], {
		
		rowsPerPage: 60,
		keepRows: 120,
		autoHeight: false,
		
		plugins: {
			selector: {
				col: "single",
				row: "single",
				cell: "disabled"
			}
		},
		
		constructor: function() {
			this.selector = null;
		},
		
		postMixInProperties: function(){
			this.structure = this.store.objectStore.pdGetOriginalStructure();
			this.inherited(arguments);
		},
		
		pdToggleColumns: function(selection){
			for (var i=0; i<selection.length; i++) {
				this.pdToggleColumn(selection[i], i==(selection.length-1));
			}
		},
		
		pdToggleColumn: function(sel, needResize) {
			if (sel.hidden){
				this.selector.deselect("col", sel.index);
			}

			this.layout.cells[sel.index].hidden = sel.hidden;
			query(".pd_idx"+sel.index, pd_previewContainer.domNode).
			style("display", sel.hidden?"none":"table-cell");
			
			if (needResize) {
				this.resize();
				if (!sel.hidden){
					this.pdSelectColumn(sel.index);
				}
			}
		},
		
		pdSelectColumn: function(idx) {
			//scroll to the column according to the selection from ColumnGrid.
			var prevColFocusIdx = this.focus._colHeadFocusIdx;
			var colHeaderNode = this.focus._findHeaderCells()[idx];
			
			this.selector.clear();
			this.selector.select("col", idx);
			this.scrollToRow(0);
			
			if (prevColFocusIdx != null && prevColFocusIdx >= 0 && prevColFocusIdx != idx){
				html.toggleClass(this.focus._findHeaderCells()[prevColFocusIdx], 
						this.focus.focusClass, false);
			}
			html.toggleClass(colHeaderNode, this.focus.focusClass, true);
			this.focus._colHeadNode = colHeaderNode;
			this.focus._colHeadFocusIdx = idx;
			this.focus._scrollHeader(idx);
		},
		
		canSort: function(colIndex, field){
			// summary:
			//		Overwritten
			return false;
		},
		
		textSizeChanged: function(){
			//override
		},
		
		startup: function() {
			this.inherited(arguments);
			this.connect(this, "onCellMouseOver", this._pdShowTooltip);
			this.connect(this, "onHeaderCellMouseOver", this._pdShowHeaderTooltip);
			
			this.selector = this.pluginMgr.getPlugin("selector");
		},
		
		_pdShowHeaderTooltip: function(e) {
			var structure = this.structure[0];
			var msg = structure[e.cellIndex].name;
			
			if (msg) {
				domAttr.set(e.cellNode, "title", msg);
			}
		},
		
		_pdShowTooltip: function(e) {
			var item = e.grid.getItem(e.rowIndex); 
			var msg = e.grid.store.getValue(item, e.cell.field); 
			
			if (msg) {
				domAttr.set(e.cellNode, "title", msg);
			}
		},
		
		defaultUpdate: function(){
			//override the default one, so the table doesn't get rerendered.
			if(!this.domNode){return;}
			if(this.updating){
				this.invalidated.all = true;
				return;
			}
			this.prerender();
		}
		
	});
	
	PreviewGrid.markupFactory = EnhancedGrid.markupFactory;
	PreviewGrid.registerPlugin = EnhancedGrid.registerPlugin;
	
	return PreviewGrid;
});