package;

import flixel.FlxCamera;
import flixel.FlxObject;
import flixel.FlxSubState;
import flixel.util.FlxColor;
import flixel.addons.effects.chainable.FlxEffectSprite;
import flixel.addons.effects.chainable.FlxGlitchEffect;
import flixel.addons.ui.FlxUI9SliceSprite;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.input.touch.FlxTouch;
import flixel.text.FlxText;
import flixel.tweens.FlxEase;
import flixel.tweens.FlxTween;
import openfl.geom.Rectangle;
import flixel.math.FlxPoint;

/**
 * ...
 * @author Laboon
 */
class Message extends FlxSubState
{
	private var _back:FlxSprite;
	private var _box:FlxUI9SliceSprite;
	private var _face:FlxSprite;
	private var _text:FlxText;
	private var _textContinue:FlxText;
	
	private var _fadingIn:Bool = true;
	private var _alpha:Float = 0;
	private var _fadingOut:Bool = false;


	public function new(CloseCallback:Void->Void, text:String, textContinue:String, ?replicas:Int)
	{
		super(0);
		
		closeCallback = CloseCallback;
		
		_back = new FlxSprite(0, 0);
		_back.makeGraphic(FlxG.width, FlxG.height, 0x99000000);
		
		_box = new FlxUI9SliceSprite(0, 0, AssetPaths.border__png,
			new Rectangle(0, 0, FlxG.width - 50, FlxG.height - 225), [3, 3, 9, 9]);
		_box.x = FlxG.width - _box.width - 20;
		_box.y = FlxG.height - _box.height - 10;
		
		_face = new FlxSprite(_box.x + 8, _box.y + 8);
		_face.loadGraphic(AssetPaths.walrusFace__png, false, 41, 36);
		
		_text = new FlxText(_face.x + _face.width + 8, _face.y, _box.width - 16 - _face.width -8, text, 12);
		_text.alignment = FlxTextAlign.CENTER;
		_text.color = 0x99ff99;
		_text.setBorderStyle(FlxTextBorderStyle.SHADOW, 0xff339933, 2, 1);
		
		_textContinue = new FlxText(_box.x + 8, _box.y + _box.height - 28, _box.width - 16, textContinue, 12);
		_textContinue.alignment = FlxTextAlign.CENTER;
		_textContinue.color = 0x99ff99;
		_textContinue.setBorderStyle(FlxTextBorderStyle.SHADOW, 0xff339933, 2, 1);
		
		_back.scrollFactor.set(0, 0);
		_box.scrollFactor.set(0, 0);
		_face.scrollFactor.set(0, 0);
		_text.scrollFactor.set(0, 0);
		_textContinue.scrollFactor.set(0, 0);
		_back.cameras = [FlxG.camera];
		_box.cameras = [FlxG.camera];
		_face.cameras = [FlxG.camera];
		_text.cameras = [FlxG.camera];
		_textContinue.cameras = [FlxG.camera];
		add(_back);
		add(_box);
		add(_face);
		add(_text);
		add(_textContinue);
		
		forEachOfType(FlxSprite, function(sprite)
		{
			sprite.alpha = 0;
		});
		
		FlxTween.num(0, 1, .66, { type:FlxTween.ONESHOT, ease:FlxEase.sineOut, onComplete:function(_)
		{
			_fadingIn = false;
		}}, updateAlpha);
	}
	
	private function updateAlpha(A:Float):Void
	{
		_back.alpha = _box.alpha = _text.alpha = _textContinue.alpha = _face.alpha = A;
	}

	
	override public function update(elapsed:Float):Void 
	{
		super.update(elapsed);
		if (!_fadingIn && !_fadingOut)
		{
			if (FlxG.keys.anyJustReleased([E, SPACE, ENTER]))
			{
				_fadingOut = true;
				FlxTween.num(1, 0, .66, { type:FlxTween.ONESHOT, ease:FlxEase.circOut, onComplete:function(_)
				{
					close();
				}}, updateAlpha);
			}
		}
	}
	
}