package classes.gustavo.midia
{
	
	import classes.fl.motion.easing.Back;
	import classes.fl.motion.easing.Exponential;
	import classes.fl.motion.easing.Quintic;
	import classes.greensock.TweenMax;
	import classes.gustavo.form.Anexo;
	import classes.gustavo.ui.alert.Alert;
	import classes.matuto.core.Button;
	import classes.matuto.core.Matuto;
	import classes.matuto.interfaces.IDisplay;
	import classes.matuto.loaders.ImagemLoad;
	import classes.matuto.mov.ObjectStartDrag;
	import classes.matuto.ui.Mt.AddAlert;
	import classes.matuto.utils.display.DisplayObjectUtils;
	import classes.matuto.utils.form.TimeName;
	
	import flash.display.Bitmap;
	import flash.display.BlendMode;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.ui.Mouse;
	import flash.utils.setTimeout;
	
	import genericos.User;
	
	import modules.foto.Foto;

	public class UploadPhotoCrop extends MovieClip
	{
		
		private var alvo			:MovieClip;
		private var anexo			:Anexo;
		private var imagem			:ImagemLoad;
		private var urlPhp			:String;
		private var nomeFoto		:String;
		private var dir				:String;
		private var porcento		:TextField;
		private var resposta		:TextField;
		private var bg				:MovieClip;
		private var tirar			:Button;
		private var resetB			:Button;
		private var mao				:MovieClip;
		private var mov				:ObjectStartDrag;
		public  var foto			:Bitmap;
		public  var estaComFoto		:Boolean = false;
		private var container		:Sprite;
		public  var containerT		:Foto;
		

		public function UploadPhotoCrop()
		{
			super();
						
			alvo  		= _alvo;
			anexo 		= _anexo;	
			porcento 	= _porcento;
			resposta	= _resposta;
			bg			= _bg;
			tirar		= _tirar;
			resetB      = _reset;
			mao			= _mao;
			mao.alpha	= 0;	
			
			tirar.alpha = 0;
			_proximo.alpha = 0;
			mao.visible = false;	
			resetB.visible = false;
			
			_resetItens.addEventListener(MouseEvent.CLICK,onClickResetItens);
			_proximo.addEventListener(MouseEvent.CLICK,onClickProximo);
			
		}
		
		
		public function set setContainer(value:Foto)
		{
		   containerT = value;
		}
		
		
		private function onClickProximo(e:MouseEvent):void
		{
			containerT.grid.fts.visible = false;
			containerT.grid.removeControl();
			User.BIT_MAP 	= DisplayObjectUtils.PhotoDisplayCopy(this,new Rectangle(4,2,_bg.width+7,_bg.height+7));;
			Matuto.SET_URL("compartilhar");
		}
		
		private function onClickResetItens(e:MouseEvent):void
		{
			containerT.grid.removeNovos();
		}
		
		public function init(_urlPhp:String,_dir:String="cards/"):void
		{
			urlPhp 		= _urlPhp;
			dir			= _dir;
			initAnexo();
			initControle();
		}
		
		//#####################################################################//
		//ANEXO
		//#####################################################################//
		
		private function initAnexo():void
		{
			nomeFoto 	= TimeName.getName;	
			anexo.inicialize(urlPhp+"?nome="+nomeFoto+".jpg","*.jpg","Arquivos de texto (*.jpg)");
			anexo.addEventListener(Anexo.ON_COMPLETE,onCompleteAnexo);
			anexo.texto.text  =  "Selecione uma foto";
			anexo.addEventListener(Event.OPEN,function(e:Event):void
			{
				TweenMax.to(tirar,1.2,{autoAlpha:0,ease:Back.easeInOut});
			});
		}
		
		private function onCompleteAnexo(e:Event):void
		{
			AddAlert.add("Carregando foto...");
			AddAlert.alert.texto.text = "Carregando foto...";
			setTimeout(loadImagem,2000);
		}
		
		//#####################################################################//
		//#####################################################################//

		
		//#####################################################################//
		//IMAGEM
		//#####################################################################//
		
		private function loadImagem():void
		{	
		   estaComFoto    = false; 	

			   
		   if(container)
		   {
			  alvo.removeChild(container);
		      container = null;
		   }
		   
		   container = new Sprite();
		   alvo.addChild(container);
		   
		   if(imagem)
		   closeImg();
		   else
		   {
			  imagem = new ImagemLoad();
			  imagem.addEventListener(ImagemLoad.COMPLETE,onCompleteImg);
			  imagem.addEventListener(ImagemLoad.PROGRESS,onProgressImg);
			  imagem.load(User.serve+dir+nomeFoto+".jpg");
			  
			  anexo.change(urlPhp+"?nome="+nomeFoto+".jpg");
		   }		
		}
		
		private function onCompleteImg(e:Event):void
		{
			AddAlert.close();
			initImg();
			anexo.texto.text  =  "Selecione outra foto para alterar!";
			User.nomeFoto     = nomeFoto;
			nomeFoto 		  = TimeName.getName;
		}
		
		private function initImg():void
		{
			container.addChild(imagem);
			DisplayObjectUtils.resizeProporcao(imagem.getBitmap(),638,396);
			imagem.getBitmap().alpha = 0;
			TweenMax.to(imagem.getBitmap(),1,{autoAlpha:1,ease:Exponential.easeInOut,onComplete:function():void{	
			}});

			TweenMax.to(tirar,2,{autoAlpha:1,ease:Back.easeInOut,delay:.4,onComplete:function():void{	
			}});
			
			porcento.text = "";
		}
		
		private function closeImg():void
		{
			TweenMax.to(imagem.getBitmap(),1,{autoAlpha:0,ease:Quintic.easeInOut});
		
			setTimeout(function():void
			{  
				container.removeChild(imagem);
				imagem = null;
				loadImagem();
				
			},900);
		}
	
		private function onProgressImg(e:Event):void
		{
			porcento.text = imagem.getPorcento.toString()+"%";
		}
		
		//#####################################################################//
		//#####################################################################//
		
		//#####################################################################//
		//PHOTO
		//#####################################################################//

		public function tiraFoto():void
		{
			estaComFoto    = true;
			foto 		   = DisplayObjectUtils.PhotoDisplayCopy(this,new Rectangle(4,2,_bg.width,_bg.height));
			foto.x 		   = 4.3;
			foto.y		   = 2.15;
			foto.blendMode = BlendMode.MULTIPLY;
			TweenMax.to(foto,0, {ease:Exponential.easeInOut,"colorMatrixFilter":{"colorize":0, "amount":.2, "brightness":1, "threshold":60}});
			container.removeChild(imagem);
			container.addChild(foto);
			imagem = null;
		}
		
		public function reset():void
		{
			imagem = null;
			container.removeChild(foto);
			foto   = null;
			estaComFoto = false;
		}
		
		public function get getPhoto():Bitmap
		{
			return foto;
		}
	
		//#####################################################################//
		//#####################################################################//
		
		//#####################################################################//
		//CONTROLE
		//#####################################################################//
		
		private function initControle():void
		{
			tirar.addEventListener(MouseEvent.CLICK,clickTirar);
			resetB.addEventListener(MouseEvent.CLICK,clickReset);
		}
		
		private function clickTirar(e:MouseEvent):void
		{
			tirar.visible  = false;
			resetB.visible = true;
			tiraFoto();
			TweenMax.to(_proximo,2,{autoAlpha:1,ease:Back.easeInOut,delay:.4,onComplete:function():void{	
			}});
			
		}
		
		private function clickReset(e:MouseEvent):void
		{
			tirar.visible  = true;
			resetB.visible = false;
			initAnexo();
			anexo.select();
			TweenMax.to(_proximo,1.2,{autoAlpha:0,ease:Back.easeInOut,onComplete:function():void{	
			}});
		}
			
		//#####################################################################//
		//#####################################################################//
		
		public function close():void
		{
		}
		
		public function resize():void
		{
		}
	
		public function dispose():void
		{	
		}
		
		
	}
}