package {
    import com.bit101.components.*;
    import com.widged.medias.BitmapLoadEvent;
    import com.widged.medias.BitmapLoader;
    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BlendMode;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

   [SWF(width="710", height="460", backgroundColor="#FFFFFF", frameRate="20")]
    public class ImageCutout extends Sprite
    {
        private var spriteData:BitmapData;
        private var imageData:BitmapData;
        private var maskData:BitmapData;
        private var isImageLoaded:Boolean;
        private var isMaskLoaded:Boolean;
        
        private var imageUrl:String = "cutout-assets/breakfast.png";
        private var maskUrl:String = Cutout.PIXELATE;
        private var imgW:int = 575;
        private var imgH:int = 431;
        private var blend:String = BlendMode.SCREEN;
        
        
        // controls
        private var wheel:WheelMenu;

        public function ImageCutout()
        {

            isImageLoaded = false;
            isMaskLoaded = false;
            spriteData = new BitmapData(imgW, imgH, false, 0xFFFFFF);
            initializeSprite(spriteData);

            imageData = new BitmapData(imgW, imgH, false, 0xFFFFFF);
            loadBitmapDataFromUrl(imageUrl, onImageLoaded);

            loadMask();

            drawControlPanel();

        }
        
        private function initializeSprite(bmpData:BitmapData):void
        {
            var imageSprite:Sprite = new Sprite();
            var imageBitmap:Bitmap = new Bitmap(bmpData);
            this.addChild(imageSprite);
            imageSprite.addChild(imageBitmap);
            imageSprite.name = "image";
            imageSprite.blendMode = BlendMode.LAYER;
        }
        
        private function loadMask():void
        {
            maskData = new BitmapData(imgW, imgH, false, 0xFFFFFF);
            loadBitmapDataFromUrl(maskUrl, onMaskLoaded);
        }
        
          // avoiding the security sandbox violation that occurs when using the url as source
          private function loadBitmapDataFromUrl(imageUrl:String, callBack:Function):void
          {
             var bmpLoader:BitmapLoader = new BitmapLoader();
             bmpLoader.addEventListener(BitmapLoadEvent.COMPLETE, callBack); 
             bmpLoader.load(imageUrl);
          }

          private function onImageLoaded(event:BitmapLoadEvent):void
          {
               var bd:BitmapData = event.bitmap.bitmapData; 
               imageData.draw(bd, null);
               isImageLoaded = true;
               addMaskIfLoaded();
               
          }

          private function onMaskLoaded(event:BitmapLoadEvent):void
          {
               var bd:BitmapData = event.bitmap.bitmapData; 
               isMaskLoaded = true;
               maskData.draw(bd);
               addMaskIfLoaded();
          }
          
          private function addMaskIfLoaded():void
          {
              if(!(isImageLoaded && isMaskLoaded)) { return; }
              composeSprite();
          }
          
          private function composeSprite():void
          {
              // Draws the source display object onto the bitmap image
             // draw(bmpData, matrix, colorTransform, blendMode, clipRect, smoothing)
              spriteData.draw(maskData); 
              spriteData.draw(imageData,null, null, blend); // SCREEN,LIGHTEN
          }

       // ##################
       //  Control Panel
       // ##################
        private function drawControlPanel():void
        {

            var window1:Window = new Window(this, 580, 0, "Blends");
            window1.width = 120;
            window1.height = 240;
            var i:int = 0;
            new RadioButton(window1.content, 10, 10 + i++ * 15, "add",false,function ():void { changeBlend(BlendMode.ADD); });
            new RadioButton(window1.content, 10, 10 + i++ * 15, "alpha",false,function ():void { changeBlend(BlendMode.ALPHA); });
            new RadioButton(window1.content, 10, 10 + i++ * 15, "darken",false,function ():void { changeBlend(BlendMode.DARKEN); });
            new RadioButton(window1.content, 10, 10 + i++ * 15, "difference",false,function ():void { changeBlend(BlendMode.DIFFERENCE); });
            new RadioButton(window1.content, 10, 10 + i++ * 15, "erase",false,function ():void { changeBlend(BlendMode.ERASE); });
            new RadioButton(window1.content, 10, 10 + i++ * 15, "hardlight",false,function ():void { changeBlend(BlendMode.HARDLIGHT); });
            new RadioButton(window1.content, 10, 10 + i++ * 15, "invert",false,function ():void { changeBlend(BlendMode.INVERT); });
            new RadioButton(window1.content, 10, 10 + i++ * 15, "layer",false,function ():void { changeBlend(BlendMode.LAYER); });
            new RadioButton(window1.content, 10, 10 + i++ * 15, "lighten",false,function ():void { changeBlend(BlendMode.LIGHTEN); });
            new RadioButton(window1.content, 10, 10 + i++ * 15, "multiply",false,function ():void { changeBlend(BlendMode.MULTIPLY); });
            new RadioButton(window1.content, 10, 10 + i++ * 15, "normal",false,function ():void { changeBlend(BlendMode.NORMAL); });
            new RadioButton(window1.content, 10, 10 + i++ * 15, "overlay",false,function ():void { changeBlend(BlendMode.OVERLAY); });
            new RadioButton(window1.content, 10, 10 + i++ * 15, "screen",false,function ():void { changeBlend(BlendMode.SCREEN); });
            new RadioButton(window1.content, 10, 10 + i++ * 15, "substract",false,function ():void { changeBlend(BlendMode.SUBTRACT); });

            var window2:Window = new Window(this, 580, 245, "Cutouts");
            window2.width = 120;
            window2.height = 195;
            i=0;
            new RadioButton(window2.content, 10, 10 + i++ * 15, "crystalize",  false,function ():void { changeCutout(Cutout.CRYSTALIZE); });
            new RadioButton(window2.content, 10, 10 + i++ * 15, "door",        false,function ():void { changeCutout(Cutout.DOOR); });
            new RadioButton(window2.content, 10, 10 + i++ * 15, "duotone",     false,function ():void { changeCutout(Cutout.DUOTONE); });
            new RadioButton(window2.content, 10, 10 + i++ * 15, "faded",       false,function ():void { changeCutout(Cutout.FADED); });
            new RadioButton(window2.content, 10, 10 + i++ * 15, "oval",        false,function ():void { changeCutout(Cutout.OVAL); });
            new RadioButton(window2.content, 10, 10 + i++ * 15, "pixelate",    false,function ():void { changeCutout(Cutout.PIXELATE); });
            new RadioButton(window2.content, 10, 10 + i++ * 15, "pixelate2",   false,function ():void { changeCutout(Cutout.PIXELATE2); });
            new RadioButton(window2.content, 10, 10 + i++ * 15, "plastic wrap",false,function ():void { changeCutout(Cutout.PLASTIC_WRAP); });
            new RadioButton(window2.content, 10, 10 + i++ * 15, "spirals",     false,function ():void { changeCutout(Cutout.SPIRALS); });
            new RadioButton(window2.content, 10, 10 + i++ * 15, "texture",     false,function ():void { changeCutout(Cutout.TEXTURE); });
            new RadioButton(window2.content, 10, 10 + i++ * 15, "torn",        false,function ():void { changeCutout(Cutout.TORN); });

        }
        
        private function changeBlend(blendName:String):void
        {
            blend = String(blendName);
            composeSprite(); 
        }

        private function changeCutout(url:String):void
        {
            maskUrl = url;
            loadMask();
        }

    }
}