<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    width="400" height="400"
    creationComplete="prep()"
    layout="vertical" viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import mx.managers.DragManager;
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import com.metaphile.mov.ITunesData;
            import com.metaphile.mov.MOVReader;

            [Bindable]
            public var files:ArrayCollection = new ArrayCollection();
            private var itunesReader:MOVReader;            

            private var coverArt:Loader;
            private function prep():void
            {
                coverArt = new Loader();
                coverArt.contentLoaderInfo.addEventListener(Event.COMPLETE, bytesLoadedHandler);
                //info.rawChildren.addChild(coverArt);
                itunesReader = new MOVReader();
                itunesReader.onMetaData = itunesDataHandler;

                //register for the drag enter event
                addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragIn);
            
                //register for the drag drop event
                addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragDrop);
            }
            
            //called when the user drags an item into the component area
            private function onDragIn(e:NativeDragEvent):void
            {
                //check and see if files are being drug in
                if(e.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT))
                {
                    //get the array of files
                    var files:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
            
                    //make sure only one file is dragged in (i.e. this app doesn't
                    //support dragging in multiple files)
                    if(files.length == 1)
                    {
                        //accept the drag action
                        DragManager.acceptDragDrop(this);
                    }
                }
            }
            
            //called when the user drops an item over the component
            private function onDragDrop(e:NativeDragEvent):void
            {
                //get the array of files being drug into the app
                var arr:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
            
                try
                {
                    var file:File = File(arr[0]);
                    var bytes:FileStream = new FileStream();
                    bytes.openAsync(file, FileMode.READ);
                    
                    itunesReader.read(bytes);
                    itunesReader.setIdentifier(bytes, file.name);
                    img.source = "";
                    coverArt.unload();
                
                    lblTitle.text = "";
                    lblArtist.text = "";
                    lblYear.text = "";
                }
                catch(error:Error)
                {
                    Alert.show(error.message+"");
                }                
            }            
            private function itunesDataHandler(data:ITunesData):void
            {
                
                lblTitle.text = data.title;
                lblArtist.text = data.artist;
                lblYear.text = data.year;
                if (data.image)
                    coverArt.loadBytes(data.image);

            }
            private function bytesLoadedHandler(event:Event):void
            {
                img.source = coverArt;//event.target;
            }
        ]]>
    </mx:Script>

    <mx:HBox width="100%" height="100%">
        <mx:Canvas id="info" width="100%" height="100%">
            <mx:Image id="img" width="200" height="200" />
            <mx:Label text="Title:" y="220"/><mx:Label id="lblTitle" left="60"  y="220"/>
            <mx:Label text="Artist:" y="240" /><mx:Label id="lblArtist" left="60" y="240" />
            <mx:Label text="Year:" y="260" /><mx:Label id="lblYear" left="60" y="260" />
        </mx:Canvas>            
    </mx:HBox>

</mx:WindowedApplication>