
function selectReflect(event){ 
	drawGraf(document.getElementById('graf-customX'), 'customX');
	drawGraf(document.getElementById('graf-customY'), 'customY');
	drawGraf(document.getElementById('graf-customZ'), 'customZ');
	drawGraf(document.getElementById('graf-R'), 'R');
	drawGraf(document.getElementById('graf-custom'), 'R(λ)*E(λ)'); 
	generateOutputs();
}
function drawGraf(canvas, type){ 
	if(window.myChart !== undefined)
		window.myChart.destroy();
	let labels = [];
	labels = keys.map((item, index) => {  
			if(index / 4 % 1 === 0) 
				return item;
			else return "";
	});
	let data = createDataSet(type, labels);

	Chart.defaults.elements.point = "1px";  
	let index = 0;  
	let text = "CIE Standard illuminant"; 
	let text1 = "Relativní SPD [-]";
	let text2 = "λ [nm]"
	if(window.myCharts !== undefined){
		switch(type){
		case 'cie1931':
		  index = 1
		  text = "CIE (1931) 2° Standard observer";
		  text1 = "Spektrální citlivost [-]";
		  break;
		case 'cie1964':
		  index = 1
		  text = "CIE (1964) 10° Standard observer";
		  text1 = "Spektrální citlivost [-]";
		  break;
		case 'customX':
		  index = 3
		  text = "";
		  break;
		case 'customY':
		  index = 4
		  text = "";
		  break;
		case 'customZ':
		  index = 5
		  text = "";
		  break;
		case 'R' :
		  index = 6;
		  text = "Odrazivost objektu";
		  text1 = "S [-]";
		  break;
		case 'R(λ)*E(λ)':
			index = 7;
			text = 'Spektrum výsledného záření';
			text1 = "S [-]";
		}
		
	}
	let config = {  
		type: 'line', 
	  	data, 
	 	options: {
	 	  	plugins: {
	            title: {
	                display: true,
	                text: text,
					position: 'top',
					//font: {
                       // size: 16
                  //  }
	            }
	        },scales: {
			y: {
					title: {
						display: true,
						text: text1,
						font: {
							size: 11
						},
						
						

					}, 
					 ticks: {
						 font:{
							 size: 10}
					 }
					
					
				},
				x: {
					title: {
						display: true,
						text: text2,
						font: {
							size: 11
						}

					},
					 ticks: {
						 font:{
							 size: 10}
					 }
				}


			}
	 	}
	};
	if(type === 'R(λ)*E(λ)'){
		config.options.scales =  { // osa y
			y:{
				max: window.myCharts[0].scales.y._endValue > 250 ? 250 : window.myCharts[0].scales.y._endValue, 
			
				  title: {
						display: true,
						text: text1,
						font: {
							size: 11
						}
				  },
				  ticks: {
						 font:{
							 size: 10}
					 }
			},
			x: {
					title: {
						display: true,
						text: text2,
						font: {
							size: 11
						},
                     


					},ticks: {
						 font:{
							 size: 10}
					 }
				}

		}
	}
	if(type === 'R(λ)*E(λ)'){

	}
	if(type === 'customX' || type === 'customY' || type === 'customZ'){ 
		let xMax = Math.max.apply(null, getCustomData('x'));
		let yMax = Math.max.apply(null, getCustomData('y'));
		let zMax = Math.max.apply(null, getCustomData('z'));
		let max = 0; 
		switch(type){ 
			case 'customX': 
				if(xMax > yMax && xMax > zMax) 
					max = xMax; 
				else if(zMax <= yMax)
					max = yMax;
				else{
					max = zMax;
				}
				break;
			case 'customY':
				if(yMax > xMax && yMax > zMax)
					max = yMax;
				else if(zMax <= xMax)
					max = xMax;
				else{
					max = zMax;
				}
				break;
			case 'customZ':
				if(zMax > xMax && zMax > yMax)
					max = zMax;
				else if(zMax <= xMax)
					max = xMax;
				else{
					max = yMax;
				}
				break;
			
		}
		max = max * 1.25;
		if(max < 3) max = Math.floor(max * 10) / 10;
		else if (max < 30) max = Math.floor(max);
		else max = Math.floor(max / 10) * 10;
		config.options.scales = {
			y:{
				title: {
						display: true,
						text: '[-]',
						font: {
							size: 13
						}
					},
				max: max,
				min: 0,
				ticks: {
					stepSize: max / 5,
					 font:{
							 size: 10}
				  }
			},
			x: {
					title: {
						display: true,
						text: text2,
						font: {
							size: 11
						}

					},
					 ticks: {
						 font:{
							 size: 10}
					 }
				}
		}
	}
	if(window.myCharts !== undefined){ 
		if(window.myCharts[index] !== undefined) window.myCharts[index].destroy();//
		window.myCharts[index] = new Chart(canvas, config);
		if(canvas.id === 'graf-illum')console.log(window.myCharts[index]);
		window.myCharts[index].canvas.parentNode.style.height = '200px';
		window.myCharts[index].canvas.parentNode.style.width = '400px';
	}
	else{
		window.myCharts = [new Chart(canvas, config)];
		window.myCharts[0].canvas.parentNode.style.height = '200px';
		window.myCharts[0].canvas.parentNode.style.width = '400px';
	}
}
function selectStandIllum(event){ 
	drawGraf(document.getElementById('graf-illum'), document.getElementById('stand-illum').value);
	drawGraf(document.getElementById('graf-customX'), 'customX');
	drawGraf(document.getElementById('graf-customY'), 'customY');
	drawGraf(document.getElementById('graf-customZ'), 'customZ');
	drawGraf(document.getElementById('graf-custom'), 'R(λ)*E(λ)');
	generateOutputs();
}
function selectObserver(event){ 
	drawGraf(document.getElementById('graf-customX'), 'customX');
	drawGraf(document.getElementById('graf-customY'), 'customY');
	drawGraf(document.getElementById('graf-customZ'), 'customZ');
	drawGraf(document.getElementById('graf-custom'), 'R(λ)*E(λ)'); 
	drawGraf(document.getElementById('graf-observ'), document.getElementById('stand-observ').value);
	generateOutputs();
}
function createDataSet(type, labels){ 
	let parentData; 
	let text; 
	let color = "rgb(180, 180, 180)";
	switch(type){ 
		case 'a':
		  parentData = [a];
		  text = 'CIE A';
		  break;
		case 'd50':
		  parentData = [d50];
		  text = 'CIE D50';
		case 'd65':
		  parentData = [d65];
		  text = 'CIE D65';
		  break;
		case 'c':
		  parentData = [c];
		  text = 'CIE C';
		  break;
		case 'f2':
		  parentData = [f2];
		  text = 'CIE F2';
		  break;
		case 'f7':
		  parentData = [f7];
		  text = 'CIE F7';
		  break;
		case 'f11':
		  parentData = [f11];
		  text = 'CIE F11';
		  break;
		case 'cie1931':
		  parentData = [cie31.x, cie31.y, cie31.z];
		  break;
		case 'cie1964':
		  parentData = [cie64.x, cie64.y, cie64.z];
		  break;
		case 'customX':
		  parentData = [getCustomData('x')];
		  text = 'X';
		  color = "rgb(230, 0, 0)"; 
		  break;
		case 'customY':
		  parentData = [getCustomData('y')];
		  text = 'Y';
		  color = "rgb(0, 230, 0)";
		  break;
		case 'customZ':
		  parentData = [getCustomData('z')];
		  text = 'Z';
		  color = "rgb(0, 0, 230)";
		  break;
		case 'R':
		  let data, typeR = document.getElementById('reflect-spectr').value; 
		  switch(typeR){ 
			  case 'greenTable': 
				data = rList.greenTable;
				text = 'Zelený stůl';
				break;
			case 'orangeFabric':
				data = rList.orangeFabric;
				text = 'Oranžová tkanina';
				break;
			case 'bluePaper':
				data = rList.bluePaper;
				text = 'Modrý papír';
				break;
			case 'whiteWall':
				data = rList.whiteWall;
				text = 'Bílá stěna';
				break;
			case 'brownPaper':
				data = rList.brownPaper;
				text = 'Hnědý papír';
				break;
			case 'redSticker':
				data = rList.redSticker;
				text = 'Červená nálepka';
				break;
			case 'perfectDiffuser':
				data = rList.perfectDiffuser;
			    text = 'Ideální difuzér';
				break;
		  }
		  parentData = [];
		  for(let i = 0; i < data.length; i+=1) 
		  	parentData.push(data[i]); 
		 // text = 'R(λ)';
		  parentData = [parentData];
		  break;
		  case 'R(λ)*E(λ)':
			  text = '';
			  parentData = [];
			  switch(document.getElementById('reflect-spectr').value){ 
				case 'greenTable': 
				  refSpec = rList.greenTable;
				  break;
			  case 'orangeFabric':
				  refSpec = rList.orangeFabric;
				  break;
			  case 'bluePaper':
				  refSpec = rList.bluePaper;
				  break;
			  case 'whiteWall':
				  refSpec = rList.whiteWall;
				  break;
			  case 'brownPaper':
				  refSpec = rList.brownPaper;
				  break;
			  case 'redSticker':
				  refSpec = rList.redSticker;
				  break;
			  case 'perfectDiffuser':
				  refSpec = rList.perfectDiffuser;
				  break;
			}
			switch(document.getElementById('stand-illum').value){
				case 'a':
				  standIllum = a;
				  break;
				case 'd50':
				  standIllum = d50;
				  break;
				case 'd65':
				  standIllum = d65;
				  break;
				case 'c':
				  standIllum = c;
				  break;
				case 'f2':
				  standIllum = f2;
				  break;
				case 'f7':
				  standIllum = f7;
				  break;
				case 'f11':
				  standIllum = f11;
				  break;
			}
			  for(let i = 0; i < 37; i++){
				  parentData.push(refSpec[i] * standIllum[i]);
			  }
			  parentData = [parentData];
			  break;
	}
	let data;
	if(parentData.length === 1){
		data = {
		  labels: labels, 
		  datasets: [{ 
		    label: text, 
		    backgroundColor: color, 
		    borderColor: color,
		    data: parentData[0], 
		  	fill: true, 
		  }]
		}
	}
	else{
		data = { 
		  labels: labels, 
		  datasets: [{
		    label: 'x', 
		    backgroundColor: 'rgb(230, 0, 0)',
		    borderColor: 'rgb(230, 0, 0)',
		    data: parentData[0], 
		  },{
		    label: 'y', 
		    backgroundColor: 'rgb(0, 230, 0)',
		    borderColor: 'rgb(0, 230, 0)',
		    data: parentData[1], 
		  },{
		    label: 'z', 
		    backgroundColor: 'rgb(0, 0, 230)',
		    borderColor: 'rgb(0, 0, 230)',
		    data: parentData[2], 
		  }]

		}
	}
	return data;
}
function getCustomData(type){ 
	let data = [37];
	let standIllum, standObs, reflectSpectr; 
	switch(document.getElementById('stand-illum').value){
		case 'a':
		  standIllum = a;
		  break;
		case 'd50':
		  standIllum = d50;
		  break;
		case 'd65':
		  standIllum = d65;
		  break;
		case 'c':
		  standIllum = c;
		  break;
		case 'f2':
		  standIllum = f2;
		  break;
		case 'f7':
		  standIllum = f7;
		  break;
		case 'f11':
		  standIllum = f11;
		  break;
	}
	switch(document.getElementById('stand-observ').value)
	{
		case 'cie1931':
		  standObs = cie31;
		  break;
		case 'cie1964':
		  standObs = cie64;
		  break;
	}
	switch(document.getElementById('reflect-spectr').value){
		case 'greenTable':
			reflectSpectr = rList.greenTable;
			break;
		case 'orangeFabric':
			reflectSpectr = rList.orangeFabric;
			break;
		case 'bluePaper':
			reflectSpectr = rList.bluePaper;
			break;
		case 'whiteWall':
			reflectSpectr = rList.whiteWall;
			break;
		case 'brownPaper':
			reflectSpectr = rList.brownPaper;
			break;
		case 'redSticker':
			reflectSpectr = rList.redSticker;
			break;
		case 'perfectDiffuser':
			reflectSpectr = rList.perfectDiffuser;
			break;
	}
	if(type === 'x'){ 
		for(let i = 0; i < standIllum.length; i++)
				data[i] = standObs.x[i] * standIllum[i] * reflectSpectr[i];
	}else if(type === 'y'){
		for(let i = 0; i < standIllum.length; i++)
				data[i] = standObs.y[i] * standIllum[i] * reflectSpectr[i];
	}else{ 
		for(let i = 0; i < standIllum.length; i++)
				data[i] = standObs.z[i] * standIllum[i] * reflectSpectr[i];
	}
	return data;
}
function generateOutputs(){ 
	let standIllum;
	let standObs;
	let refSpec;
	let standObsName = document.getElementById('stand-observ').value;
	let standIllumName = document.getElementById('stand-illum').value;
	switch(standIllumName){
		case 'a': 
		  standIllum = a;
		  break;
		case 'd50':
		  standIllum = d50;
		  break;
		case 'd65':
		  standIllum = d65;
		  break;
		case 'c':
		  standIllum = c;
		  break;
		case 'f2':
		  standIllum = f2;
		  break;
		case 'f7':
		  standIllum = f7;
		  break;
		case 'f11':
		  standIllum = f11;
		  break;
	}
	switch(standObsName)
	{
		case 'cie1931':
		  standObs = cie31;
		  break;
		case 'cie1964':
		  standObs = cie64;
		  break;
	}
	switch(document.getElementById('reflect-spectr').value){ 
				case 'greenTable': 
				  refSpec = rList.greenTable;
				  break;
			  case 'orangeFabric':
				  refSpec = rList.orangeFabric;
				  break;
			  case 'bluePaper':
				  refSpec = rList.bluePaper;
				  break;
			  case 'whiteWall':
				  refSpec = rList.whiteWall;
				  break;
			  case 'brownPaper':
				  refSpec = rList.brownPaper;
				  break;
			  case 'redSticker':
				  refSpec = rList.redSticker;
				  break;
			  case 'perfectDiffuser':
				  refSpec = rList.perfectDiffuser;
				  break;
			}
	let allData = generateXYZArr(standIllum, standObs, refSpec);
	let dataX = allData[0], dataY = allData[1], dataZ = allData[2]; 
	let sumX = 0, sumY = 0, sumZ = 0; 
	dataX.forEach((item) => sumX += item);// soucet pro X
	dataY.forEach((item) => sumY += item);// soucet pro Y
	dataZ.forEach((item) => sumZ += item);// soucet pro Z
	let temp = generateMultiplyEY(standIllum, standObs.y);
	sumX = (+sumX / temp * 100).toFixed(6);// vysl. X
	sumY = (+sumY / temp * 100).toFixed(6);// vysl. Y
	sumZ = (+sumZ / temp * 100).toFixed(6);// vysl. Z
	let xyYx = (+sumX / (+sumX + +sumY + +sumZ)).toFixed(6); 
	let xyYy = (+sumY / (+sumX + +sumY + +sumZ)).toFixed(6); 
	let xyYY = +sumY; // Y pro xyY
	//debugger;
	let newData = generateXYZArr(standIllum, standObs);//pole XYZ pro idealni difuzer/referencni bila
	let refX, refY, refZ;
	refX = refY = refZ = 0;
	newData[0].forEach(item => refX += item);// soucet pro X
	newData[1].forEach(item => refY += item);// soucet pro Y
	newData[2].forEach(item => refZ += item);// soucet pro Z
	temp = refY;
	refX = (refX / temp * 100).toFixed(6);// vysl. X
	refY = (refY / temp * 100).toFixed(6);// vysl. Y
	refZ = (refZ / temp * 100).toFixed(6);// vysl. Z
	let var_X = sumX / refX;
	let var_Y = sumY / refY;
	let var_Z = sumZ / refZ;

	if ( var_X > 0.008856 ) var_X = Math.pow(var_X, 1/3 ); 
	else                    var_X = ( 7.787 * var_X ) + ( 16 / 116 );
	if ( var_Y > 0.008856 ) var_Y = Math.pow(var_Y, 1/3 );
	else                    var_Y = ( 7.787 * var_Y ) + ( 16 / 116 );
	if ( var_Z > 0.008856 ) var_Z = Math.pow(var_Z, 1/3 );
	else                    var_Z = ( 7.787 * var_Z ) + ( 16 / 116 );
	let LabL = (( 116 * var_Y ) - 16).toFixed(6);// L
	let Laba = (500 * ( var_X - var_Y )).toFixed(6);// a 
	let Labb = (200 * ( var_Y - var_Z )).toFixed(6);// b 

	document.getElementById('x-XYZ').firstChild.innerHTML = "X = " + sumX; 
	document.getElementById('y-XYZ').firstChild.innerHTML = "Y = " + sumY;
	document.getElementById('z-XYZ').firstChild.innerHTML = "Z = " + sumZ;
	document.getElementById('x-xyY').firstChild.innerHTML = "x = " + xyYx;
	document.getElementById('y-xyY').firstChild.innerHTML = "y = " + xyYy;
	document.getElementById('Y-xyY').firstChild.innerHTML = "Y = " + xyYY;
	document.getElementById('L-Lab').firstChild.innerHTML = "L = " + LabL;
	document.getElementById('a-Lab').firstChild.innerHTML = "a = " + Laba;
	document.getElementById('b-Lab').firstChild.innerHTML = "b = " + Labb;
}
function generateXYZArr(standIllum, standObs, reflectSpectr){ 
	if(reflectSpectr === undefined) reflectSpectr = rList.perfectDiffuser;
	let dataX = [], dataY = [], dataZ = [];
	for(let i = 0; i < standIllum.length; i++){
			{dataX.push(standObs.x[i] * standIllum[i] * reflectSpectr[i]);}
	}
	for(let i = 0; i < standIllum.length; i++){
			dataY.push(standObs.y[i] * standIllum[i] * reflectSpectr[i]);
	}
	for(let i = 0; i < standIllum.length; i++){
			dataZ.push(standObs.z[i] * standIllum[i] * reflectSpectr[i]);
	}
	return [dataX, dataY, dataZ]; 
}
function generateMultiplyEY(standIllum, standObsY){
	sum = 0;
	for(let i = 0; i < standIllum.length; i++){
			sum += standIllum[i] * standObsY[i];
	}
	return sum;
}
drawGraf(document.getElementById('graf-illum'), "a"); 
drawGraf(document.getElementById('graf-observ'), 'cie1931');
drawGraf(document.getElementById('graf-R'), 'R');
drawGraf(document.getElementById('graf-customX'), 'customX');
drawGraf(document.getElementById('graf-customY'), 'customY');
drawGraf(document.getElementById('graf-customZ'), 'customZ');
drawGraf(document.getElementById('graf-custom'), 'R(λ)*E(λ)');
generateOutputs();
