package
{
    import com.savagelook.SwatchLoader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.utils.ByteArray;
    
    /**
     * ...
     * @author Tony Lukasavage - SavageLook.com
     */
    [SWF(public class SwatchLoaderTest extends Sprite 
    {
        [Embed( "ase/Moon sliver.ase", mimeType = "application/octet-stream")] private const ASEFile1:Class;
        [Embed( "ase/Alligator eraser.ase", mimeType = "application/octet-stream")] private const ASEFile2:Class;
        [Embed( "ase/Chocolate Mint Teal.ase", mimeType = "application/octet-stream")] private const ASEFile3:Class;
        [Embed( "ase/Graphite daisies.ase", mimeType = "application/octet-stream")] private const ASEFile4:Class;
        [Embed( "ase/Island Pie.ase", mimeType = "application/octet-stream")] private const ASEFile5:Class;
        [Embed( "ase/monazi.ase", mimeType = "application/octet-stream")] private const ASEFile6:Class;
        [Embed( "ase/Sundried Tomato And Olive Bread In Little Italy.ase", mimeType = "application/octet-stream")] private const ASEFile7:Class;
        [Embed( "ase/The Architect.ase", mimeType = "application/octet-stream")] private const ASEFile8:Class;
        [Embed( "ase/cl_gray.ase", mimeType = "application/octet-stream")] private const ASEFile9:Class;
        [Embed( "ase/cl_giant_goldfish.ase", mimeType = "application/octet-stream")] private const ASEFile10:Class;
        
        private var SQUARE_HEIGHT:uint = 150;
        private var SQUARE_WIDTH:uint = 150;
        private var _text:TextField;
        private var _strip:Sprite;
        private var _swatches:Vector.<SwatchLoader> = new Vector.<SwatchLoader>();
        private var _swatch_index:uint;
        
        public function SwatchLoaderTest():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            _swatch_index = 0;
            _swatches.push(new SwatchLoader(new ASEFile1 as ByteArray),
                new SwatchLoader(new ASEFile2 as ByteArray),
                new SwatchLoader(new ASEFile3 as ByteArray),
                new SwatchLoader(new ASEFile4 as ByteArray),
                new SwatchLoader(new ASEFile5 as ByteArray),
                new SwatchLoader(new ASEFile6 as ByteArray),
                new SwatchLoader(new ASEFile7 as ByteArray),
                new SwatchLoader(new ASEFile8 as ByteArray),
                new SwatchLoader(new ASEFile9 as ByteArray),
                new SwatchLoader(new ASEFile10 as ByteArray));
            
            _text = new TextField();
            _text.textColor = 0xffffff;
            _text.autoSize = "left";
            addChild(_text);
            
            _strip = new Sprite();
            _strip.x = (this.stage.stageWidth / 2) - (SQUARE_WIDTH * 5 / 2);
            _strip.y = (this.stage.stageHeight / 2) - (SQUARE_HEIGHT / 2);
            _strip.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void { loadNextSwatch(); } );
            addChild(_strip);
            
            loadNextSwatch();
        }
        
        private function loadNextSwatch():void {
            while (_strip.numChildren > 0) _strip.removeChildAt(0);
            if (_swatch_index >= _swatches.length-1) {
                _swatch_index = 0;
            } else {
                _swatch_index++;
            }
            
            var swatch:SwatchLoader = _swatches[_swatch_index];
            _text.text = "Swatch name: " + (swatch.name == "" ? "unknown" : swatch.name);
            for (var i:uint = 0; i < 5; i++) {
                var square:Sprite = new Sprite();
                _strip.addChild(square);
                square.graphics.beginFill(swatch.colorValues[i]);
                square.graphics.drawRect(i * SQUARE_WIDTH, 0, SQUARE_WIDTH, SQUARE_HEIGHT); 
                square.graphics.endFill();
                
                var nameText:TextField = new TextField();
                _strip.addChild(nameText);
                nameText.textColor = 0xffffff;
                nameText.autoSize = "center";
                nameText.text = swatch.colorNames[i];
                nameText.x = (i * SQUARE_WIDTH) + (SQUARE_WIDTH / 2) - (nameText.textWidth/2);
                nameText.y = SQUARE_HEIGHT + 5;
            }
        }
    }
}