package {
	import adobe.utils.CustomActions;
	import com.netgrind.core.Key;
	import com.netgrind.core.Scheduler;
	import com.netgrind.core.Utils;
	import com.netgrind.core.UtilsArray;
	import com.netgrind.core.UtilsArt;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author netgrind
	 */
	public class TextWindow extends Sprite {
		
		[Embed(source = "../lib/ui/ui_messagebox.png")]public var boxTex:Class;
		[Embed(source = "../lib/ui/ui_messageballoon.png")]public var balloonTex:Class;
		
		public var isOpen:Boolean = false;
		public var nextKey:uint = 32;
		public var dialog:Vector.<TextChunk> = new Vector.<TextChunk>();
		public var index:int = 0;
		public var callback:Function;
		public var charTarget:Point = new Point(500,220);
		private var textBox:TextField;
		private var momFace:Character;
		private var charFace:Character;
		private var box:Bitmap;
		private var balloon:Bitmap;
		
		public function TextWindow() {
			super();
			Key.bindFunctionToKeyPress(next, nextKey);
			box = new boxTex();
			addChild(box);
			balloon = new balloonTex();
			addChild(balloon);
			textBox = new TextField();
			textBox.wordWrap = true;
			textBox.defaultTextFormat = new TextFormat(null, 18);
			addChild(textBox);
			textBox.width = 400;
			textBox.height = 150;
			textBox.x = 60;
			textBox.y = 40;
			
			momFace = Character.all[CharacterManager.NICOLE];
			momFace.showFace();
			addChild(momFace);
			momFace.y = 220;
			momFace.scaleX = -1;
			momFace.x = 50;
			
			addEventListener(Event.ADDED_TO_STAGE, init);
			visible  = false;
		}
		
		private function init(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			x = stage.stageWidth * .5 - box.width*.5;
			y = stage.stageHeight * .5 - box.height * .5;
		}
		
		public function next():void {
			index++;
			if (index >= dialog.length) {
				close();
				return;
			}
			textBox.text = dialog[index].text;
			if (dialog[index].momTalking) {
				balloon.scaleX = -1;
				balloon.x = balloon.width;
				textBox.x = 80;
			}else {
				balloon.scaleX = 1;
				balloon.x = 0;
				textBox.x = 60;
			}
			momFace.faceArt.displayFrame(dialog[index].momState);
			charFace.faceArt.displayFrame(dialog[index].charState);
		}
		
		private function update():void {
			/*var r:Number = Math.sin(Scheduler.c) * 20;
			if(index<dialog.length){
				//momFace.rotation = Utils.lerp(momFace.rotation, dialog[index].momTalking?r:0,.2);
				//charFace.rotation = Utils.lerp(charFace.rotation, dialog[index].momTalking?0:r, .2);
			}*/
		}
		
		public function displayDialog(text:Vector.<TextChunk>, character:Character, callback:Function = null):void {
			if (isOpen) return;
			if (charFace != null) {
				Utils.removeChild(charFace, false);
			}
			character.addToDialog(this);
			charFace = character;
			dialog = text;
			index = -1;
			open();
			next();
			if (callback != null) this.callback = callback;
		}
		
		public function open():void {
			if (isOpen) return;
			visible = true;
			isOpen = true;
			Scheduler.invokeEvery(update);
		}		
		
		public function close():void {
			if (!isOpen) return;
			visible = false;
			isOpen = false;
			if (callback != null) callback();
			Scheduler.cancel(update);
		}
		
	}
	
}