// Copyright 2008 ProjectWizards GmbH. All rights reserved.

//console.log("UtilizationViewController");

var	MECumulationParameterUtilisation = 0;
var	MECumulationParameterWorkingUnits = 1;
var	MECumulationParameterPossibleWorkingUnits = 2;
var	MECumulationParameterWorkingUnitsOverPossibleWorkingUnits = 3;
var	MECumulationParameterBracket = 4;
var	MECumulationParameterActualWorkingUnits = 5;
var	MECumulationParameterRemainingWorkingUnits = 6;

var UtilizationViewController = ActivitiesViewController.extend({
	constructor: function(container, moc, controls, environment, styleList, viewOptions, columns, initialSortDescriptors, actionMethods, ruler, printPageURL, showPrintView, splitViewState, isBeingExportedForQuicklook){
		this.replaceKeyForTitleColumnInColumns(columns);
		this.base(container, moc, controls, environment, styleList, viewOptions, columns, initialSortDescriptors, actionMethods, ruler, printPageURL, showPrintView, splitViewState);
		this.className = "UtilizationViewController";
		this.displayBrackets = false;
		this.isBeingExportedForQuicklook = isBeingExportedForQuicklook;
		if(this.isBeingExportedForQuicklook)
			PWUtils.notificationCenter.removeObserverForMessage(this, "rulerChanged");	
	},
		
	createSubviews: function(){
		if(!this.outlineController){
			this.outlineController = new OutlineController(this.environment);
			this.outlineController.setChildrenKey("assignments");
			this.outlineController.setSortDescriptors(this.initialSortDescriptors);
			this.outlineController.setDelegate(this.activitiesView);
			this.activitiesView = new UtilizationView(this.outlineController, this.container, this.columns, this.styleList, this.ruler, this.viewOptions, this.environment, this.controls, this.moc, this.showPrintView, this);
			this.setSplitViewState(this.splitViewState);
			if(this.splitterPosition)
				this.activitiesView.splitView.setSplitterPosition(this.splitterPosition);
			NotificationCenter.defaultNotificationCenter().addObserverForMessage(this, WRLAjaxContextChangedByServerNotification, "managedObjectContextChangedByServer");	
		}
	},
	
	replaceKeyForTitleColumnInColumns: function(columns){
		for(var i=0; i<columns.length; i++){
			var column = columns[i];
			if(column.key == "title"){
				column.key="utilizationTitle";
				column.prototypeSortDescriptors = [ {key: "utilizationTitle", ascending: true}];
				column.editable = false;
				column.disableReversedSortDescriptors = true;
				break;
			}
		}
	},

	visibleGanttChanged: function(startDate, endDate) {
		if(this.activitiesView){
			this.activitiesView.ganttScrollView.ganttView.requestGraphValues();
		}
	},

	// Methods for graphs:

	displayGraphs: function(){
		return true;
	},

	showGraphForObject: function(object){
		var result = false;
		if(object.className() == "MEMasterResource"){
			return !(this.viewOptions.cumulationParameter == MECumulationParameterBracket && object.valueForKey("resources").length);
		}
		return result;
	},

	graphValuesReceived: function(result){
		this.graphValuesReceivedMethod.call(this.graphValuesReceivedObject, eval("("+result.responseText+")"));
	},
	
	requestGraphValues: function(request, method, sender){
		this.graphValuesReceivedMethod = method;		
		this.graphValuesReceivedObject = sender;
		this.actionMethods.graphValuesForRequest({request:Object.toJSON(request)});
	},
	
	isInLayoutChangingAction: function() {
		return (this.activitiesView && this.activitiesView.outlineView && this.activitiesView.outlineView.inCollapseAction) || this.isInZoomAction;
	},
	
	graphTextForBlock: function(graphObject, block, width) {
		var result = block.string;
		if(this.viewOptions.cumulationParameter == MECumulationParameterUtilisation){			
			if(width < 19 && result.length > 2)
				result = result.substr(0, 2);
			else if (width < 28 && result.length > 3)
				result = result.substr(0, 3);
			return result;		
		}
		return result;
	},
	
	willDrawGraphBlock: function(graphObject, block) {
		var color = "#ffd14f"; // Gelb
		if(block.value > graphObject.overThreshold)
			color = "#f16767"; // Rot
		else if(block.value >= graphObject.underThreshold)
			color = "#80be67"; // Grün
		block.div.style.backgroundColor = color;
	},
	
	requestGraphValuesForObject: function(graphObject) {
		var resources = graphObject.object.valueForKey("resources");
		return resources && resources.length;
	},
	
	barColorForObject: function(object, isGroup){
		if(object.className() == "MEMasterResource")
			return {color:"#1457CF" , backgroundColor:"#98B2F3"};
		return null;
	},

	columnsFromWorkspaceData: function(workspaceData) {
		var columns = eval('('+workspaceData.columns+')');
		this.replaceKeyForTitleColumnInColumns(columns);
		return columns;
	},
	
	outlineDoubleClickInEmptyArea: function(outline, event) {

	},

	workspaceViewName: function() {
		return "utilisationViewController";
	}
	
	
	
});
