/*jshint node: true, -W106 */
'use strict';

/*
* Name : barChart.js
* Module : Lib::BusinessTier::BarChart
* Location : /lib/businessTier/barChart
*
* History :
* 
* Version   Date         Programmer         Description
* =========================================================
* 0.0.1     2015-06-01   Samuele Zanella    Initial code
* =========================================================
*/

/*var Socket=require('../../presentationTier/socket.js');
var Page=require('../page/page.js');*/
var Graph=require('./graph.js');
var BarChartFlow=require('../flow/barChartFlow.js');
var BarChartModel=require('../../dataTier/graph/barChartModel.js');
var Helper = require('../../helpers/functionHelper.js');

function BarChart(params, page, graphSocket) {
	if (params===undefined){
		console.log('Error: 361');
		return;
	}
	if (!(graphSocket.hasOwnProperty('_namespace'))){
		console.log('Error: 362');
		return;
	}
	if (!(page.hasOwnProperty('_page'))){
		console.log('Error: 363');
		return;
	}
	var bc=new BarChartModel(params);
	if (!bc.hasOwnProperty('_ID')){
		return;
	}
	this.parent.constructor.call(this, page, graphSocket);
	this._dataBarChart=bc;
	this._flows=[];
}

BarChart.prototype = Object.create(Graph.prototype);
BarChart.prototype.constructor = Graph;
BarChart.prototype.parent = Graph.prototype;

BarChart.prototype.getFlowByID = function(ID){
	if (typeof ID==='string') {
		var flows=this._flows.length;
		var i=0;
		var found=false;
		while (i<flows && (!found)){
			if (this._flows[i]._dataBarChartFlow._ID===ID){
				found=true;
			}
			else{
				i++;
			}
		}//while
		if (found){//in flows[i] there is the ID
			return this._flows[i];
		}
		//no ID in flows
	}
	return 262;
};

BarChart.prototype.createBarChartFlow = function(params, records){
	var bcf=new BarChartFlow(params, this._graphSocket, records);
	if (!bcf.hasOwnProperty('_dataBarChartFlow')){
		return 212;
	}
	this._flows.push(bcf);
	this._dataBarChart.addFlow(bcf._dataBarChartFlow);

	var prop={
		properties: {
			ID: params.ID
		}
	};
	if(params.name!==undefined && typeof params.name === 'string'){
		prop.properties.name=params.name;
	}
	if(params.flowColor!==undefined && Helper.isHEX(params.flowColor)){
		prop.properties.flowColor=params.flowColor;
	}
	//prop.records=records;
	prop.records = bcf.getReplaceDataJSON();
	//"PIPPOVARDAQUA" in teoria qua stai passando tutto indistintamente
	this._graphSocket.sendMessage('insertFlow', prop);
	return bcf;
};

BarChart.prototype.deleteFlow = function(ID){
	if (typeof ID==='string') {
		var flows=this._flows.length;
		var i=0;
		var found=false;
		while (i<flows && (!found)){
			if (this._flows[i]._dataBarChartFlow._ID===ID){
				found=true;
			}
			else{
				i++;
			}
		}//while
		if (found){//in flows[i] there is the ID	
			this._graphSocket.sendMessage('deleteFlow', {ID: ID});
			this._flows.splice(i, 1);
			return true;
		}
		//no ID in flows
	}
	return 263;
};

BarChart.prototype.deleteAllFlows = function(){
	while (this._flows.length>0){
		this._graphSocket.sendMessage('deleteFlow', {ID: this._flows[0]._dataBarChartFlow._ID});
		this._flows.splice(0, 1);
	}
};

BarChart.prototype.updateRecord = function(flowID, index, record){
	return this.getFlowByID(flowID).updateRecord(index, record);
};


BarChart.prototype.updateProperties = function(params){
	if (params!==undefined){
		var prop=this._dataBarChart.updateProperties(params);
		//prop.ID=this._dataBarChart._ID;
		this._graphSocket.sendMessage('updateGraphProp', prop);
	}
};

BarChart.prototype.getProperties = function(){
	return this._dataBarChart.getProperties();
};

BarChart.prototype.getConfigJSON = function(){
	var configJSON={};
	configJSON.properties=this._dataBarChart.getProperties();
	var flowProp=[];
	var flows=this._flows.length;
	configJSON.data=[];
	for (var i=0; i<flows; i++){
		var flowData={};
		flowProp.push(this._flows[i]._dataBarChartFlow.getProperties());
		var records=this._flows[i]._dataBarChartFlow.getData();
		flowData.ID=this._flows[i]._dataBarChartFlow._ID;
		flowData.records=[];
		for (var j=0; j<records.length; j++){
			flowData.records.push({
				norrisRecordID : records[j].norrisRecordID,
				value: [
					records[j][this._flows[i]._dataBarChartFlow._indexKey],
					records[j][this._flows[i]._dataBarChartFlow._valueKey]
				]
			});
		}
		configJSON.data.push(flowData);
	}
	configJSON.properties.flows=flowProp;
	return configJSON;
};

module.exports = BarChart;

@@ -2288,16 +2288,21 @@
 cords = 
+%5B%5D;//
 bcf.getR
@@ -2319,16 +2319,379 @@
 JSON();%0A
+%09var dataRecords=bcf._dataBarChartFlow._records;%0A%09for (var i=0; i%3CdataRecords.length; i++)%7B%0A%09%09if (dataRecords%5Bi%5D.norrisRecordIsValid)%7B%0A%09%09%09prop.records.push(%7B%0A%09%09%09%09norrisRecordID : dataRecords%5Bi%5D.norrisRecordID,%0A%09%09%09%09value: %5B //lat,long%0A%09%09%09%09%09dataRecords%5Bi%5D%5Bthis._dataBarChartFlow._indexKey%5D,%0A%09%09%09%09%09dataRecords%5Bi%5D%5Bthis._dataBarChartFlow._valueKey%5D%0A%09%09%09%09%5D%0A%09%09%09%7D);%0A%09%09%7D%0A%09%7D%0A
 %09//%22PIPP

fix barchart insertflow