var canvas = document.getElementsByTagName("canvas")[0]; 
var ctx = canvas.getContext("2d");
var menu = document.getElementById('menu'); 
var chkText   = document.getElementById('PText'); 
var chkData   = document.getElementById('DefData');
var chkRotate = document.getElementById('Rotate');
var chkloop   = document.getElementById('ChkLoop');
var but1 = document.getElementById('TestPercent'); 
var but2 = document.getElementById('TestProcess'); 
var but3 = document.getElementById('TestBlinkStatus');
var but4 = document.getElementById('TestNumberColumn')
var but5 = document.getElementById('TestDiagramStove');
var but6 = document.getElementById('TestPost');
var text = document.getElementById('TestDiv');

//Функция изменения размера полотна
function Resize() {
	canvas.style.margin = "10px";
	canvas.width = window.innerWidth - 20;
	canvas.height = window.innerHeight - 30 - menu.clientHeight;
	ctx = canvas.getContext("2d");
}
window.addEventListener("load", Resize, false);
window.addEventListener("resize", Resize, false);
//Функция проверки столбцов процесса
but1.onclick = function () {
	ctx.clearRect(0, 0, canvas.width, canvas.height);
	var height = 200;
	var width = 40;
	if (chkRotate.checked){
		var tmp = height;
		height = width;
		width = tmp;
	}
	for (var i = 0; ((width * (i + 1)) + (i * 5)) < (canvas.width - 1); i++) {
		for (var j = 0; ((height * (j + 1)) + (j * 5)) < (canvas.height - 1); j++) {
			var t = new PercentColumn(
				(i * (width + 5)) + 0.5,
				(j * (height + 5)) + 0.5,
				width,
				height);
			if (!chkData.checked)
				t.Color(getRColor());
			t.Percent(Math.random() * 101);
			t.Rotate(chkRotate.checked);
			t.Print(ctx);
			if (chkText.checked)
				t.PrintText(ctx);
		}
	}
}
//Функция проверки столбцов статусов
but2.onclick = function () {
	var y = 200;
	var x = 40;
	var count = Math.floor(Math.random() * 6 + 2);
	var arrPoint = [];
	var arrColor = [];
	for(var i = 0; i < count; i++){
		arrPoint.push(Math.random() * 5 + 1);
		arrColor.push(getRColor());
	}
	if (chkData.checked){
		y = y * 2;
	}
	if (chkRotate.checked){
		var tmp = y;
		y = x;
		x = tmp;
	}
	ctx.clearRect(0, 0, canvas.width, canvas.height);
	for (var i = 0; ((x * (i + 1)) + (i * 5)) < (canvas.width - 1); i++) {
		for (var j = 0; ((y * (j + 1)) + (j * 5)) < (canvas.height - 1); j++) {
			var t = new ProcessColumn(
				(i * (x + 5)) + 0.5,
				(j * (y + 5)) + 0.5,
				x, y);
			t.Rotate(chkRotate.checked);
			if (chkData.checked)
				t.BuildDefault();
			else
				for (var k = 0; k < count; k++)
					t.AddRStat(k, arrPoint[k], arrColor[k]);
			t.Status(Math.floor(Math.random() * (t.StatCount() + 1) - 1));
			t.Percent(Math.random() * 101);
			t.Print(ctx);
			if (chkText.checked)
				t.PrintText(ctx);
		}
	}
}
//Функция проверки мигалки
but3.onclick = function () {
	var y = 40;
	var x = 70;
	var count = Math.floor(Math.random() * 5 + 1);
	var arrColor = [];
	for (var i = 0; i < count; i++)
		arrColor.push(getRColor());
	if (chkRotate.checked) {
		var tmp = y;
		y = x;
		x = tmp;
	}
	ctx.clearRect(0, 0, canvas.width, canvas.height);
	for (var i = 0; ((x * (i + 1)) + (i * 5)) < (canvas.width - 1); i++) {
		for (var j = 0; ((y * (j + 1)) + (j * 5)) < (canvas.height - 1); j++) {
			var t = new BlinkStatus(
				(i * (x + 5)) + 0.5,
				(j * (y + 5)) + 0.5,
				x, y);
			t.Rotate(chkRotate.checked);
			if (chkData.checked)
				t.BuildDefault();
			else
				for (var k = 0; k < count; k++)
					t.AddStatus(k, arrColor[k]);
			t.Status(Math.floor(Math.random() * (t.StatCount() + 1) - 1));
			t.Print(ctx);
			if (chkText.checked)
				t.PrintText(ctx);
		}
	}
}
//Функция проверки номера
but4.onclick = function () {
	var y = 40;
	var x = 70;
	var num = 0;
	if (chkRotate.checked) {
		var tmp = y;
		y = x;
		x = tmp;
	}
	ctx.clearRect(0, 0, canvas.width, canvas.height);
	for (var i = 0; ((y * (i + 1)) + (i * 5)) < (canvas.height - 1); i++) {
		for (var j = 0; ((x * (j + 1)) + (j * 5)) < (canvas.width - 1); j++) {
			var t = new NumberColumn(
				(j * (x + 5)) + 0.5,
				(i * (y + 5)) + 0.5,
				x, y, num++);
			t.Rotate(chkRotate.checked);
			t.Prostoy(Math.floor(Math.random() * 2) == 1);
			t.Print(ctx);
		}
	}
}
//Переменные для диаграммы
var cycle;
var diagTest;
//Функция цикла
function cycle_print() {
	diagTest.RectParam(0.5, 0.5, canvas.width - 1, canvas.height - 1)
	diagTest.Print(ctx);
	if (chkText.checked)
		diagTest.PrintText(ctx);
	if (diagTest.Cycle())
		cycle = setTimeout(cycle_print, 1000);
}
but5.onclick = function () {
	var chkDate = true;

	ctx.clearRect(0, 0, canvas.width, canvas.height);
	diagTest = new Diagram(0.5, 0.5, canvas.width - 1, canvas.height - 1);
	if (chkData.checked) {
		diagTest.BuildDefault();
		for (var i = 0; i < diagTest.ProcCount(); i++) {
			diagTest.ChangeStatProc(i,
				Math.floor(Math.random() * 11),
				Math.floor(Math.random() * 101));
			diagTest.ChangeStatBlink(i, Math.floor(Math.random() * 5 - 1));
			diagTest.ChangeStatNumb(i, Math.floor(Math.random() * 2) == 1);
		}
	}
	else
	{
		var countStove = Math.floor(Math.random() * 20 + 30);
		var countProc = Math.floor(Math.random() * 9 + 1);
		var countBlink = Math.floor(Math.random() * 4 + 1);

		var colorProc = [];
		for (var i = 0; i < countProc; i++)
			colorProc.push(getRColor());
		
		var pointProc = [];
		for (var i = 0; i < countProc; i++)
			pointProc.push(Math.random() * 10);

		var colorBlink = [];
		for (var i = 0; i < countBlink; i++)
			colorBlink.push(getRColor());

		for (var i = 0; i < countStove; i++) {
			var tP = new ProcessColumn(0, 0, 0, 0);
			for (var k = 0; k < countProc; k++)
				tP.AddRStat(k, pointProc[k], colorProc[k]);
			tP.Status(Math.floor(Math.random() * countProc));
			tP.Percent(Math.floor(Math.random() * 101));
			
			var tB = new BlinkStatus(0, 0, 0, 0);
			for (var k = 0; k < countBlink; k++)
				tB.AddStatus(k, colorBlink[k]);
			tB.Status(Math.floor(Math.random() * (countBlink + 1) -1));

			var tN = new NumberColumn(0, 0, 0, 0, i + 1);
			tN.Prostoy(Math.floor(Math.random() * 2) == 1);

			diagTest.AddProc(i, tP, tB, tN);
		}
	}
	diagTest.Rotate(chkRotate.checked);

	if (chkloop.checked) {
		for (var i = 0; i < diagTest.ProcCount(); i++) {
			var dStart = new Date();
			var dEnd = new Date();
			dStart.setSeconds(dStart.getSeconds() + Math.floor(Math.random() * 61 - 50));
			dEnd.setSeconds(dEnd.getSeconds() + Math.floor(Math.random() * 61 - 10));
			diagTest.StartDate(i, dStart);
			diagTest.EndDate(i, dEnd);
		}
		diagTest.Cycle(true);
		cycle_print();
	} else {
		diagTest.Print(ctx);
		if (chkText.checked)
			diagTest.PrintText(ctx);
	}


}


var btn6CycleSend;
but6.onclick = function () {
	var xhr = new XMLHttpRequest();
	//xhr.withCredentials = true;
	xhr.open('POST', 'http://127.0.0.1:65041/currcycles', true);
	xhr.send();

	xhr.onreadystatechange = function () {
		if (xhr.readyState != 4) return;
		if (xhr.status != 200) {
			alert(xhr.status + ': ' + xhr.statusText);
			return;
		}
		PrintDiagram(xhr.responseText);
		if (chkloop.checked) {
			btn6CycleSend = setTimeout(but6.onclick, 60000);
			but6.disabled = true;
		} else {
			but6.disabled = false;
		}
	}
}

function PrintDiagram(pechstatus) {
	var Pech = JSON.parse(pechstatus);
	ctx.clearRect(0, 0, canvas.width, canvas.height);
	var t = new Diagram(0.5, 0.5, canvas.width - 1, canvas.height - 1);
	t.BuildDefault();
	t.Rotate(chkRotate.checked);

	for (var key in Pech.data) {
		var idx = Pech.data[key].vdp - 1;
		var s = -1;
		var b = -1;
		switch (Pech.data[key].cycle) {
			case 0: s = 0; b = -1; break;
			case 1: s = 0; b = 1; break;
			case 2: s = 1; b = 0; break;
			case 5: s = 2; b = 0; break;
			case 6: s = 3; b = 0; break;
			case 7: s = 4; b = 0; break;
			case 8: s = 5; b = 3; break;
			case 9: s = 6; b = 3; break;
			case 10: s = 7; b = 3; break;
			case 11: s = 8; b = 3; break;
			case 12: s = 9; b = 3; break;
			case 14: s = 1; b = 1; break;
			case 15: s = 2; b = 1; break;
			case 16: s = 3; b = 1; break;
			default: s = 0; b = -1;
    }

		t.ChangeStatProc(idx, s, 0);
		t.ChangeStatBlink(idx, b);
		t.StartDate(idx, new Date(Pech.data[key].factStart));
		t.EndDate(idx, new Date(Pech.data[key].thinkEnd));
	}
	t.Print(ctx);
	if (chkText.checked)
		t.PrintText(ctx);
}

function getRColor() {
	var letters = '0123456789ABCDEF';
	var color = '#';
	for (var i = 0; i < 6; i++) {
		color += letters[Math.floor(Math.random() * 16)];
	}
	return color;
}

