package com.guru.component.fileDownloader
{
	import com.guru.call.FunctionCall;
	import com.guru.call.ICall;
	import com.guru.component.message.Message;
	import com.guru.component.panel.Panel;
	import com.guru.component.panel.StatusBar;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author Eddy Lee
	 */
	public class FileDownloader
	{
		private var mPath_str:String;
		private var mSuccessCall:ICall;
		private var mFailCall:ICall;
		private var mURLRequest:URLRequest;
		private var mFileReference:FileReference;
		
		public function FileDownloader(pPath_str:String, pSuccessCall:ICall, pFailCall:ICall)
		{
			mPath_str = pPath_str;
			mSuccessCall = pSuccessCall;
			mFailCall = pFailCall;
			
			mFileReference = new FileReference();
			mFileReference.addEventListener(IOErrorEvent.IO_ERROR, onDownloadFileErrorHandler);
			mFileReference.addEventListener(ProgressEvent.PROGRESS, onDownloadFileProgressHandler);
			mFileReference.addEventListener(Event.COMPLETE, onDownloadFileCompleteHandler);
			mFileReference.addEventListener(Event.CANCEL, onDownloadFileCancelHandler);
		}
		
		public function dispose():void
		{
			try
			{
				mFileReference.cancel();
			}
			catch (e) { }
			
			mFileReference.removeEventListener(IOErrorEvent.IO_ERROR, onDownloadFileErrorHandler);
			mFileReference.removeEventListener(ProgressEvent.PROGRESS, onDownloadFileProgressHandler);
			mFileReference.removeEventListener(Event.COMPLETE, onDownloadFileCompleteHandler);
			mFileReference.removeEventListener(Event.CANCEL, onDownloadFileCancelHandler);
			
			mFileReference = null;
			mURLRequest = null;
		}
		
		public function setPath(pPath_str:String):void
		{
			mPath_str = pPath_str;
		}
		
		public function setDoneCall(pCall:ICall):void
		{
			mSuccessCall = pCall;
		}
		
		public function setFailCall(pCall:ICall):void
		{
			mFailCall = pCall;
		}
		
		public function start():void
		{	
			mURLRequest = new URLRequest(mPath_str);
			
			Panel.getProgressBar().showPanel(new FunctionCall(this, doStart));
		}
		
		private function doStart():void
		{
			Panel.getProgressBar().setCancelCall(new FunctionCall(this, onDownloadFileCancelHandler, null));
			
			mFileReference.download( mURLRequest);
		}
		
		private function onDownloadFileCancelHandler(e:Event):void
		{
			mFileReference.cancel();
			//mFailCall.execute();
			Panel.getProgressBar().hidePanel(mFailCall);
		}
		
		private function onDownloadFileErrorHandler(e:IOErrorEvent):void
		{
			//Logger.eddy("onDownloadFileErrorHandler: " + e);
			
			Panel.getProgressBar().hidePanel(new FunctionCall(Panel, Panel.showStatusBar, StatusBar.STATUS, Message.DOWNLOAD_ERROR, mFailCall));
		}
		
		private function onDownloadFileCompleteHandler(e:Event):void
		{
			//Logger.eddy("onDownloadFileCompleteHandler: " + e);
			
			Panel.getProgressBar().hidePanel(new FunctionCall(Panel, Panel.showStatusBar, StatusBar.STATUS, Message.DOWNLOAD_SUCCESS, mSuccessCall));
		}
		
		private function onDownloadFileProgressHandler(e:ProgressEvent):void
		{
			Logger.eddy("onDownloadFileProgressHandler: " + e);
			
			Panel.getProgressBar().updateProgress(e.bytesLoaded, e.bytesTotal);
		}
	}

}