package {
    import flash.display.DisplayObject;
    import flash.display.StageScaleMode;
    import flash.display.Sprite;
    import flash.display.BlendMode;
    
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.geom.Rectangle;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import net.sakri.event.SimpleButtonMenuClickEvent;
    
    import flash.geom.Matrix;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BitmapDataChannel;
    import flash.geom.Point;
    
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.media.SoundMixer;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
    
    import net.sakri.text_effects.SplitTextField;
    import net.sakri.components.Reflection;
    import net.sakri.components.SimpleButtonMenu;
    
    [SWF(width="1000", height="500", backgroundColor="#000000")]

    public class SoundTextEffect extends Sprite{
        [Embed(source="assets/pirate.gif")]
        public var PirateSmiley:Class;

        [Embed(source="assets/ArialBlackFont.swf", fontName="Arial Black")]
        public static var ArialBlack:Class;

        private var _holder:Sprite;
        
        private var _split_text:SplitTextField;
        private var _follower1:SplitTextField;
        private var _follower2:SplitTextField;
        private var _static_text:SplitTextField;
                
        private var _sound:Sound;
        private var _channel:SoundChannel;
        private var _spectrum_graph:BitmapData;
        
        private var _app_text_format:TextFormat;
        
        private var _sound_names:Array=new Array("super sharp shooter","g thang","window licker");
        private var _sounds:Array=new Array("assets/choppedsss.mp3","assets/chopped_thang.mp3","assets/chopped_licker.mp3");
        private var _sound_menu:SimpleButtonMenu;

        private var _text_value:String="SAKRI.NET/Blog!!";
        
        public function SoundTextEffect(){
            stage.scaleMode=StageScaleMode.NO_SCALE;
            init();
        }
        
        private var _num_chars:uint;
        private var _loop_space:uint;//=Math.floor(256/_num_chars);
        private var _og_height:uint;
        
        private var _font_colors:Array=new Array(0x414e18,0x9eb800,0x8a9b65,0xd4e3b8);
        
        private var _reflection:Reflection;    
        
        private function init():void{
            initTextFormat();
            
            _num_chars=_text_value.length;
            _loop_space=Math.floor(256/_num_chars);

            //Create "split text fields"
            _app_text_format.color=_font_colors[3];
            _split_text=new SplitTextField(_text_value,_app_text_format);
            _split_text.alpha=50;
            _app_text_format.color=_font_colors[2];
            _follower1=new SplitTextField(_text_value,_app_text_format);
            _follower1.alpha=65;
            _app_text_format.color=_font_colors[1];
            _follower2=new SplitTextField(_text_value,_app_text_format);
            _follower2.alpha=80;
            _app_text_format.color=_font_colors[0];    
            _static_text=new SplitTextField(_text_value,_app_text_format);
            _holder=new Sprite();
            _holder.addChild(_static_text);            
            _holder.addChild(_follower2);
            _holder.addChild(_follower1);
            _holder.addChild(_split_text);
            _holder.x=500-_split_text.width/2;
            _holder.y=250-_split_text.height/2;
            addChild(_holder);
            
            //add sweet pirate graphixxx
            var pirate1:*=new PirateSmiley();
            var pirate2:*=new PirateSmiley();    
            pirate1.scaleX=pirate2.scaleX=3;
            pirate1.scaleY=pirate2.scaleY=3;
            addChild(pirate1);
            addChild(pirate2);
            
            pirate1.x=_holder.x-pirate1.width;
            pirate2.x=_holder.x+_holder.width;
            pirate1.y=pirate2.y=_holder.y+30;
            
            _spectrum_graph=new BitmapData(256,60,true,0x00000000);
            var bitmap:Bitmap=new Bitmap(_spectrum_graph);
            addChild(bitmap);
            bitmap.x=100;
            bitmap.y=100;
            
            createReflection();
            playSound();
            createSoundMenu();
            addEventListener(Event.ENTER_FRAME,reactToSound);
        }
        
        private function createSoundMenu():void{
            _sound_menu=new SimpleButtonMenu();
            _sound_menu.init(_sound_names,1000);
            _sound_menu.y=500-40;
            _sound_menu.addEventListener(SimpleButtonMenuClickEvent.SIMPLE_BUTTON_MENU_CLICK,handleSoundMenuClick);
            addChild(_sound_menu);
        }
        
        private function playSound(index:uint=0):void{
            if(_channel!=null)_channel.stop();
            _sound=new Sound(new URLRequest(_sounds[index]));
            _channel=_sound.play(400,1000);//ANYONE WHO LISTENS TO THIS CRAP OVER 1000 LOOPS HAS SERIOUS ISSUES...
        }
        
        private function createReflection():void{
            _reflection=new Reflection();
            _reflection.init(_holder);
            addChild(_reflection);
            _reflection.x=_holder.x;
            _reflection.y=_holder.y+_holder.height-50;
            addEventListener(Event.ENTER_FRAME,_reflection.render);
        }
        
        public function handleSoundMenuClick(e:SimpleButtonMenuClickEvent):void{
            playSound(e.index);
        }

        public function reactToSound(e:Event):void{
            var spectrum:ByteArray=new ByteArray();
            SoundMixer.computeSpectrum(spectrum);    
            _spectrum_graph.fillRect(_spectrum_graph.rect,0x00000000);
            var char_index:uint=0;
            var counter:uint=0;
            var cur:Number=0;
            for(var i:int=0;i<256;i++){
                if(counter==_loop_space){
                    _split_text.getChildAt(char_index).y=cur;//instead of getChild, optimize somehow...
                    _follower1.getChildAt(char_index).y=cur/1.5;
                    _follower2.getChildAt(char_index).y=cur/3;
                    char_index++;
                    counter=0;
                }
                cur=spectrum.readFloat()*40;
                counter++;
            }
        }
            
        private function createTextField():TextField{
            var tf:TextField=new TextField();
            tf.autoSize="left";
            tf.text=_text_value;
            tf.selectable=false;
            tf.embedFonts=true;
            tf.setTextFormat(_app_text_format);
            return tf;
        }
        
        private function initTextFormat():void{
            _app_text_format=new TextFormat();
            _app_text_format.font="Arial Black";
            _app_text_format.size="86";
            _app_text_format.bold=true;
            _app_text_format.color="0x000000";
        }
        
    }
}