package {
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.MouseEvent;
    import flash.filters.DropShadowFilter;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
    
    import net.eidiot.etracer.eerror;
    import net.eidiot.etracer.einfo;
    import net.eidiot.etracer.elog;
    import net.eidiot.etracer.etrace;
    import net.eidiot.etracer.ewarn;
    import net.eidiot.etracer.initETracer;
    
    [SWF(width = 580, height = 300, backgroundColor = 0xDBDBDB)]
    public class ETracerDemo extends Sprite
    {
        //============================================================
        //                        Variable
        //============================================================
        private var _logBtn:TextButton;
        private var _warnBtn:TextButton;
        private var _errorBtn:TextButton;
        private var _traceBtn:TextButton;
        //---------- einfo() ----------
        private var _arrayBtn:TextButton;
        private var _requestBtn:TextButton;
        private var _displayBtn:TextButton;
        
        private var _code:CodeView;
        private var _nextY:Number = 40;
        //============================================================
        //                        Constructor
        //============================================================
        public function ETracerDemo()
        {
            super();
            init();
        }
        //============================================================
        //                        Private Method
        //============================================================
        private function init():void
        {
            //---------- init stage ----------
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            //---------- init ETracer ----------
            initETracer(stage, 450, 260, 120, 10, false);
            //---------- init title ----------
            var title:Label = new Label("Test ETracer", 16, 0);
            displayChild(title, 5, 10);
            title.filters = [new DropShadowFilter(2, 45, 0, 0.5)];
            //---------- elog() ----------
            _logBtn = new TextButton("elog()");
            displayChild(_logBtn, 10, _nextY);
            _nextY += _logBtn.height + 5;
            _logBtn.addEventListener(MouseEvent.ROLL_OVER, onOverLogBtn);
            _logBtn.addEventListener(MouseEvent.ROLL_OUT, onOutLogBtn);
            _logBtn.addEventListener(MouseEvent.CLICK, onClickLogBtn);
            //---------- ewarn() ----------
            _warnBtn = new TextButton("ewarn()");
            displayChild(_warnBtn, 10, _nextY);
            _nextY += _warnBtn.height + 5;
            _warnBtn.addEventListener(MouseEvent.CLICK, onClickWarnBtn);
            //---------- eerror() ----------
            _errorBtn = new TextButton("eerror()");
            displayChild(_errorBtn, 10, _nextY);
            _nextY += _errorBtn.height + 5;
            _errorBtn.addEventListener(MouseEvent.CLICK, onClickErrorBtn);
            //---------- etrace() ----------
            _traceBtn = new TextButton("etrace()");
            displayChild(_traceBtn, 10, _nextY);
            _nextY += _traceBtn.height + 5;
            _traceBtn.addEventListener(MouseEvent.CLICK, onClickTraceBtn);
            //---------- einfo() ----------
            // label
            var infoLabel:Label = new Label("einfo()", 12, 0);
            displayChild(infoLabel, 10, _nextY);
            _nextY += infoLabel.height + 5;
            // arrayBtn
            _arrayBtn = new TextButton("Array");
            displayChild(_arrayBtn, 10, _nextY);
            _nextY += _arrayBtn.height + 5;
            _arrayBtn.addEventListener(MouseEvent.CLICK, onClickArrayBtn);
            // requestBtn
            _requestBtn = new TextButton("URLRequest");
            displayChild(_requestBtn, 10, _nextY);
            _nextY += _requestBtn.height + 5;
            _requestBtn.addEventListener(MouseEvent.CLICK, onClickRequestBtn);
            // displayBtn
            _displayBtn = new TextButton("Display Object");
            displayChild(_displayBtn, 10, _nextY);
            _nextY += _displayBtn.height + 5;
            _displayBtn.addEventListener(MouseEvent.CLICK, onClickDisplayBtn);
            //---------- hot key ----------
            displayChild(new Label("Hot Key:", 12, 0), 10, _nextY + 10);
            displayChild(new Label("Ctrl + Alt + X", 12, 0xFF0000), 10, _nextY + 30);
            //---------- code ----------
            _code = new CodeView(570);
            displayChild(_code, 5, 278);
            _code.update("initETracer(stage, 450, 260, 120, 10, false);");
        }
        private function displayChild(child:DisplayObject, px:Number, py:Number):void
        {
            addChild(child);
            child.x = px;
            child.y = py;
        }
        //============================================================
        //                        Event Handle
        //============================================================
        private function onClickDisplayBtn(e:MouseEvent):void
        {
            einfo(e.currentTarget, "Test output DisplayObject", 0, "x", "y");
            _code.update('einfo(e.currentTarget, "Test output DisplayObject", 0, "x", "y");');
        }
        private function onClickArrayBtn(e:MouseEvent):void
        {
            einfo(["a", 2, false, ["b", 3], {c:"c", d:{e:"e", f:"f"}}], "Test output Array", 2);
            _code.update('einfo(["a", 2, false, ["b", 3], {c:"c", d:{e:"e", f:"f"}}], "Test output Array", 2);');
        }
        private function onClickRequestBtn(e:MouseEvent):void
        {
            var requset:URLRequest = new URLRequest("http://eidiot.net/getImage");
            var vars:URLVariables = new URLVariables();
            vars.method = "GetImage";
            vars.ID = 123;
            requset.data = vars;
            einfo(requset, "test output URLRequest", 1, "url", "data");
            _code.update('einfo(requset, "test output URLRequest", 1, "url", "data");');
        }
        private function onOverLogBtn(e:MouseEvent):void
        {
            elog("Over the log button. Please lick it.");
            _code.update('elog("Over the log button. Please lick it.");');
        }
        private function onOutLogBtn(e:MouseEvent):void
        {
            elog("Out the log button. Why not click it.");
            _code.update('elog("Out the log button. Why not click it.");');
        }
        private function onClickLogBtn(e:MouseEvent):void
        {
            elog("Click the log button", "x:" + _logBtn.x, "y:" + _logBtn.y);
            _code.update('elog("Click the log button", "x:" + _logBtn.x, "y:" + _logBtn.y);');
        }
        private function onClickTraceBtn(e:MouseEvent):void
        {
            etrace("mouseX:" + mouseX, "mouseY:" + mouseY);
            _code.update('etrace("mouseX:" + mouseX, "mouseY:" + mouseY);');
        }
        private function onClickWarnBtn(e:MouseEvent):void
        {
            ewarn(new Error("It's a warn!"));
            _code.update('ewarn(new Error("It\'s a warn!"));');
        }
        private function onClickErrorBtn(e:MouseEvent):void
        {
            try { _errorBtn.tryRemove(100); } catch (e:Error) { eerror(e); } 
            _code.update('try { _errorBtn.tryRemove(100); } catch (e:Error) { eerror(e); } ');
        }
    }
}
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;
    
    internal class TextButton extends Sprite
    {
        public function TextButton(label:String):void
        {
            var tf:Label = new Label(label, 12, 0x0000FF);
            addChild(tf);
            with (graphics)
            {
                lineStyle(0, 0, 0.2);
                beginFill(0, 0.1);
                drawRoundRect(0, 0, width, height, 5);
                endFill();
            }
            buttonMode = true;
            mouseChildren = false;
        }
        internal function tryRemove(i:int):void
        {
            removeChildAt(i);
        }
    }
    internal class Label extends TextField
    {
        public function Label(str:String, size:int, color:uint)
        {
            defaultTextFormat = new TextFormat("Verdana", size, color);
            text = str;
            width = textWidth + 4;
            height = textHeight + 4;
            mouseEnabled = false;
        }
    }
    internal class CodeView extends Sprite
    {
        public function CodeView(w:Number)
        {
            var tf:TextField = new TextField();
            tf.defaultTextFormat = new TextFormat("Verdana", 12, 0);
            tf.text = "Code:";
            tf.width = w;
            tf.height = tf.textHeight + 4;
            addChild(tf);
            with (graphics)
            {
                lineStyle(0, 0xFFFFFF);
                beginFill(0xEEEFF0);
                drawRoundRect(0, 0, width, height, 5);
                endFill();
            }
        }
        internal function update(code:String):void
        {
            TextField(getChildAt(0)).text = "Code : " + code;
        }
    }