// ActionScript file

import flash.display.BitmapData;

import mx.graphics.codec.JPEGEncoder;
import mx.rpc.http.HTTPService;
import mx.utils.Base64Encoder;

private var bm:BitmapData;


private function getCam():void
{
    if (Camera.getCamera())     
    { 
        // assign the user's default camera to a variable
        var camera:Camera = Camera.getCamera();
        
        // set the camera quality to be the highest as possible
        camera.setQuality(0, 100);    
        
        // set the width, height, frames per second
        camera.setMode(theCam.width, theCam.height, 30);    
        
        // attach the camera to our "theCam" VideoDisplay
        theCam.attachCamera(camera);    
    }
        
    else
    {
        //tell the user to try another method
    }
    
}


public function takePicture():void {           
    
    //if we are not previewing any picture, we'll take one :)
    if (!previewBox.visible) {    
        
        //create a BitmapData variable called picture that has theCam's size
        var picture:BitmapData = new BitmapData(theCam.width, theCam.height);
        
        //the BitmapData draws our theCam
        picture.draw(theCam);        
        
        //Our preview's source is a new Bitmap made of picture's BitmapData
        preview.source = new Bitmap(picture);
        
        //stores this BitmapData into another BitmapData (outside this function)        
        bm = picture;    
        
        //makes the previewBox visible, so we can see our picture
        previewBox.visible = true;
        
        //displays the flashLight
        flashLight.visible = true;
        
        //makes the flashLight go way
        flashLight.visible = false;
        
        //change our trigger label, so the user will be able to try again
        trigger.label = "Take another picture...";        
        
        //enables the savePic button
        savePic.enabled = true;
        
        //changes the color of the button
        trigger.setStyle('chromeColor', '#ff0000');
    }
        
        //if we are previewing a picture...
    else { 
        
        //makes the previewBox invisible
        previewBox.visible = false;        
        
        //changes the label
        trigger.label = 'Take a picture!';    
        
        //disables the savePic button
        savePic.enabled = false;
        
        //changes the color of the button
        trigger.setStyle('chromeColor', '#33abe9');
        
    }            
    
}



public function savePicture():void {               
    
    //change the savePic button label so the user knows as soon as possible
    //that we are saving his picture
    savePic.label = "Saving..."
    
    //disables the button so the user don't click it twice
    savePic.enabled = false;
    
    //the trigger button displays a nice message    
    trigger.label = "That's a nice picture :)"
    
    //disables the "trigger" button, now is too late to take another picture!
    trigger.enabled = false;    
    
    
    //creates a new JPEGEncoder called "je"
    //sets the quality to 100 (maximum)
    var je:JPEGEncoder = new JPEGEncoder(100);     
    
    //creates a new ByteArray called "ba"
    //JPEGEnconder encodes our "bm" Bitmap data: our "picture"    
    var ba:ByteArray = je.encode(bm);
    //this ByteArray is now an encoded JPEG
    
    //creates a new Base64Encoder called "be"
    var be:Base64Encoder = new Base64Encoder();
    
    //encodes our "ba" ByteArray (wich is our JPEG encoded picture) with base64Encoder
    be.encodeBytes(ba);
    
    //Now we have our "encodedData" string to send to the server
    var encodedData:String = be.flush();
    
    
    
    //this is the HTTPService that we will use to send our data to the server    
    var handleService:HTTPService = new HTTPService();    
    
    //now we set what URL we want... Set the URL of your server-side page/action
    //this is a typical Ruby on Rails URL. Controller: users_controller, Action: handlepicture
    handleService.url = "http://localhost:3000/users/camerasnap";    
    
    //another example of URL:
    //appUrl.url = "http://www.example.com/handlePicture.php";
    
    //or, a relative path:
    //appUrl.url = "/handlePicture.php"
    
    
    //we choose POST as our method    
    handleService.method = "POST";
    
    //here we show the busy cursor for better visual feedback
    handleService.showBusyCursor = true;
    
    //Finally, we send our "encodedData" as a "content" variable
    handleService.send({content: encodedData});    
    
    
    // 1 - in your server-side code you can handle the POSTED variable called "content"
    // 2 - use a base64 decoder
    // 3 - write it to disc (now you have a real image saved in your server)
    // 4 - make this image the user's profile picture or anything you want
    
}