/*
 * Desenvolvido por Henrique Moreira e Rafael Duarte
 * Proibida a cópia
 */

//----------------------------------------
// OBJETO PARA CRIAR ANIMAÇÕES DE DESENHO
// Depende de um objeto Desenho instanciado
//----------------------------------------

function Animacao(desenho) {
	var self = this;
	var passo;
	var rodando = false;
	var velocidade1 = 200;
	var velocidade2 = 20;
	var velocidadeDireto = 4;
	var velocidadeAtual = velocidade1;
	var direto = velocidadeDireto - 1;
	
	var tempRodar = "";
	var divisor = 1;
	var seq = "";
	var desenho = desenho;

	//Trata cada passo da animação
	function rodar(){
		if (rodando) {
			desenhoTela();

			passo++;
			if (passo < seq.length) {
				if (direto == velocidadeDireto - 1) {
					setTimeout(function(){
						rodar();
					}, velocidadeAtual);
				} else {
					rodar();
				}
			} else {
				rodando = false;
				desenho.limparTelaPrev();
				//liberando o ato de desenhar
				desenho.liberar(true);
				if(self.onTerminar instanceof Function) self.onTerminar();
			}
		}
	}

	//executa o desenho na tela
	function desenhoTela(){
		var vez = seq[passo].split("@");
		var vals = vez[1].split("#");
		velocidadeAtual = velocidade1;

		switch (parseInt(vez[0])) {
			//retangulo, circulo, linha...
			case 1:
				desenho.desenhar(Math.round(parseInt(vals[1]) / divisor), Math.round(parseInt(vals[2]) / divisor), Math.round(parseInt(vals[3]) / divisor), Math.round(parseInt(vals[4]) / divisor), parseInt(vals[0]), 0, false);
				ponteiro(Math.round(parseInt(vals[3]) / divisor), Math.round(parseInt(vals[4]) / divisor));
				break;
			//lápis
			case 2:
				if(vals.length >= 4) {
					desenho.desenhar(Math.round(parseInt(vals[0]) / divisor), Math.round(parseInt(vals[1]) / divisor), Math.round(parseInt(vals[2]) / divisor), Math.round(parseInt(vals[3]) / divisor), 6, 0, false);
					ponteiro(Math.round(parseInt(vals[2]) / divisor), Math.round(parseInt(vals[3]) / divisor));

					if (vals.length > 4) {
						seq[passo] = "2@" + vals.slice(2).join("#");
						passo--;
						//acelerando
						velocidadeAtual = velocidade2;
						//passar com ou sem tempo alternado (para ficar mais rapido)
						direto = (direto + 1) % velocidadeDireto;
					}
					else {
						direto = velocidadeDireto - 1;
						//verificar se o proximo tambem é lapis, para manter a velocidade
						if (seq[passo + 1] != undefined && seq[passo].charAt(0) == seq[passo + 1].charAt(0)) {
							velocidadeAtual = velocidade2;
						}
					}
				} else
					direto = false;
				break;
			//balde
			case 3:
				var x1, y1, x2, y2;

				for (var cont = 0; cont < vals.length; cont = cont + 4) {
					x1 = Math.round(parseInt(vals[cont]) / divisor);
					y1 = Math.round(parseInt(vals[cont + 1]) / divisor);
					x2 = Math.round(parseInt(vals[cont + 2]) / divisor);
					y2 = Math.round(parseInt(vals[cont + 3]) / divisor);
					desenho.retanguloF(x1, y1, x2, y2, 0);
				}
				break;
			case 21:
				desenho.borracha(Math.round(parseInt(vals[0]) / divisor), Math.round(parseInt(vals[1]) / divisor), 0, false);
				ponteiro(Math.round(parseInt(vals[2]) / divisor), Math.round(parseInt(vals[3]) / divisor));

				if (vals.length >= 4) {
					seq[passo] = "21@" + vals.slice(2).join("#");
					passo--;
					//acelerando
					velocidadeAtual = velocidade2;
				}
				else
					if (seq[passo + 1] != undefined && seq[passo].substr(0, 2) == seq[passo + 1].substr(0, 2)) {
						velocidadeAtual = velocidade2;
					}
				break;
			//mudar cor
			case 5:
				desenho.mudaCor(vals[0], false);
				velocidadeAtual = velocidade2;
				break;
			//mudar borda
			case 6:
				var valor = Math.round(parseInt(vals[0]) / divisor);
				desenho.mudaBorda(valor, false);
				velocidadeAtual = velocidade2;
				break;
			//mudar imagem base
			case 26:
				desenho.transparencia();
				desenho.setArquivoBase(vals[0],false);
			break;
		}
	}
	
	function ponteiro(cx,cy) {
		if(desenho.hasCanvasPrev() && cx > 0 && cy > 0) {
			desenho.setGlobalAlpha(1,0.7);
			desenho.limparTelaPrev();
			//valores originais
			var corDesenho = desenho.getCorValor();
			var bordaDesenho = desenho.getBorda();
			
			desenho.mudaBorda(2,false);
			desenho.mudaCor('xDDDDDD',false);
			desenho.desenhar(cx-5,cy-5,cx+5,cy+5,4,1,false);
			desenho.mudaCor('x999999',false);
			desenho.desenhar(cx-5,cy-5,cx+5,cy+5,5,1,false);
			
			//retornando dados
			desenho.mudaCor(corDesenho,false);
			desenho.mudaBorda(bordaDesenho,false);
			desenho.setGlobalAlpha(1,1);
		}
	}

	//Iniciar a animação
	this.iniciar = function(div){
		if(!rodando) {
			rodando = true;
			divisor = div;

			//limpando tela
			desenho.limparTela(false);
			if(self.onComecar instanceof Function) self.onComecar();
			seq = desenho.getSequencia().split("|");

			//desabilitar novos desenhos
			desenho.liberar(false);

			passo = 0;
			direto = velocidadeDireto - 1;
			setTimeout(function() { rodar(); }, velocidade1);
		}
	}

	//Pausar a animação
	this.pausar = function(){
		if(rodando) {
			rodando = false;
			desenho.limparTelaPrev();
			if(self.onPausar instanceof Function) self.onPausar();
		}
	}

	//cancela animacao e exibe desenho completo
	this.parar = function(){
		rodando = false;
		while(passo < seq.length) {
			desenhoTela();
			passo++;
		}
		//liberando o ato de desenhar
		desenho.liberar(true);
		desenho.limparTelaPrev();
		if(self.onTerminar instanceof Function) self.onTerminar();
	}

	//continuar animação que foi paralizada
	this.continuar = function() {
		if(!rodando) {
			rodando = true;
			if(self.onContinuar instanceof Function) self.onContinuar();
			rodar();
		}
	}
	
	this.setVelocidades = function(vel1, vel2, veld) {
		velocidade1 = vel1;
		velocidade2 = vel2;
		velocidadeDireto = veld;
	}

	//Eventos da classe
	this.onTerminar = null;
	this.onComecar = null;
	this.onPausar = null;
	this.onContinuar = null;
}