/**
* @desc         Flash to Phaser JSFL
* @version      1.0 - May 28th 2014
* @author       Richard Davey <rich@photonstorm.com>
* @copyright    2014 Photon Storm Ltd.
* @license      {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/

var getTotal = function (name) {

    var t = 0;
    
    for (var i = 0; i < totals.length; i++)
    {
        if (totals[i] === name)
        {
            t++;
        }
    }
    
    totals.push(name);
    
    if (t == 0)
    {
        return '';
    }
    else
    {
        return t.toString();
    }
    
}

var addBitmap = function (element, group) {

    var png = element.libraryItem.name;
    var name = png.substr(0, png.length - 4);
    var local = name + getTotal(name);
    var x = element.x;
    var y = element.y;
    var r = element.rotation;
    var sx = element.scaleX;
    var sy = element.scaleY;
    
    if (round)
    {
        x = Math.round(x);
        y = Math.round(y);
    }
    
    //fl.trace('Bitmap: ' + name + ' x: ' + x + ' y: ' + y + ' r: ' + r + ' sx: ' + sx + ' sy: ' + sy);
    
    //  Save to preload array
    if (preload.indexOf(png) == -1)
    {
        preload.push(png);
    }
    
    //  Save to the create array
    if (group == '')
    {
        create = create.concat("    var " + local + " = game.add.sprite(" + x + ", " + y + ", '" + name + "');\n");

        if (r !== 0)
        {
            create = create.concat("    " + local + ".angle = " + r + ";\n");
        }

        if (sx !== 1 || sy !== 1)
        {
            create = create.concat("    " + local + ".scale.set(" + sx + ", " + sy + ");\n");
        }
        
        create = create.concat("\n");
    }
    else
    {
        create = create.concat("    var " + local + " = " + group + ".create(" + x + ", " + y + ", '" + name + "');\n");

        if (r !== 0)
        {
            create = create.concat("    " + local + ".angle = " + r + ";\n");
        }

        if (sx !== 1 || sy !== 1)
        {
            create = create.concat("    " + local + ".scale.set(" + sx + ", " + sy + ");\n");
        }
        
        create = create.concat("\n");
    }
    
}

var addGroup = function (element) {

    //  lower-case first character and remove spaces
    var name = element.libraryItem.name.replace(" ", "");
    var first = name.substr(0, 1).toLowerCase();
    name = first + name.substr(1, name.length - 1);
    var local = name + getTotal(name);
    var children = element.libraryItem.timeline.layers[0].frames[0].elements;
    
    var x = element.x;
    var y = element.y;
    var r = element.rotation;
    var sx = element.scaleX;
    var sy = element.scaleY;
    
    if (round)
    {
        x = Math.round(x);
        y = Math.round(y);
    }

    create = create.concat("    var " + local + " = game.add.group();\n");
    create = create.concat("    " + local + ".x = " + x + ";\n");
    create = create.concat("    " + local + ".y = " + y + ";\n");

    if (r !== 0)
    {
        create = create.concat("    " + local + ".angle = " + r + ";\n");
    }

    if (sx !== 1 || sy !== 1)
    {
        create = create.concat("    " + local + ".scale.set(" + sx + ", " + sy + ");\n");
    }
    
    create = create.concat("\n");
    
    //fl.trace('Group: ' + name + ' x: ' + x + ' y: ' + y + ' r: ' + element.rotation);

    for (var i = 0; i < children.length; i++)
    {
        if (children[i].instanceType == "bitmap")
        {
            addBitmap(children[i], name);
        }
    }
    
}

fl.outputPanel.clear();

var totals = new Array();
var round = true;
var preload = new Array();
var create = "function create() {\n\n";
var stage = fl.getDocumentDOM().getTimeline().layers[0].frames[0].elements;

for (var i = 0; i < stage.length; i++)
{
    if (stage[i].instanceType == "bitmap")
    {
        addBitmap(stage[i], '');
    }
    else if (stage[i].instanceType == "symbol")
    {
        addGroup(stage[i]);
    }
}

create = create.concat("}");

var output = "// Generated by Flash to Phaser v1.0 at " + new Date().toUTCString() + "\n\n";

output = output.concat("\n");
output = output.concat('function preload() {');
output = output.concat("\n\n");

//  Preload block
//  game.load.image('defaultFont', '../../assets/fonts/arcadefonts/waku-Waku Waku 7 (Sunsoft).png');

for (var i = 0; i < preload.length; i++)
{
    var name = preload[i].substr(0, preload[i].length - 4);
    output = output.concat("    game.load.image('" + name + "', '" + preload[i] + "');\n");
}

output = output.concat("\n");
output = output.concat('}');
output = output.concat("\n\n");
output = output.concat(create);

fl.trace("Generating ...");
var f =  fl.browseForFileURL("save", "Select a JS", "Phaser JS Document (*.js)", "js");
fl.outputPanel.clear();
fl.trace(output);
fl.outputPanel.save(f);
fl.trace("Saved to " + f);
