
function testAlert(text)
{
    alert(text);
}

function showArchiApp(mesh, scene, color, textColor)
{
    var textureX = new BABYLON.DynamicTexture("dynamic texture", 256, scene, true);
    var materialX = new BABYLON.StandardMaterial("mat", scene);
    materialX.emissiveTexture = textureX;
    materialX.diffuseColor = new BABYLON.Color3(0, 0, 0);
    materialX.specularColor = new BABYLON.Color3(0, 0, 0);
    mesh.material = materialX;
    writeTextApp(textureX, mesh.name, color, textColor);
}

function showArchiSvc(mesh, scene, color, textColor)
{
    var textureX = new BABYLON.DynamicTexture("dynamic texture", 256, scene, true);
    var materialX = new BABYLON.StandardMaterial("mat", scene);
    materialX.emissiveTexture = textureX;
    materialX.diffuseColor = new BABYLON.Color3(0, 0, 0);
    materialX.specularColor = new BABYLON.Color3(0, 0, 0);
    mesh.material = materialX;
    writeTextService(textureX, mesh.name, color, textColor);
}

function writeTextApp(aTexture, text, color, textColor) {
    var ctx = drawArchiRect(aTexture, color, textColor); // "#B5FFFF"
    drawAppSymbol(ctx);
    ctx.fillStyle = textColor;
    wrapText(ctx, text, 12, 120, 240, 40);
    ctx.restore();
    aTexture.update();
}

function writeTextService(aTexture, text, color, textColor) {
    var ctx = drawArchiRect(aTexture, color, textColor);
    drawSvcSymbol(ctx, color, textColor); // "#FFA79E" "black"
    ctx.fillStyle = textColor;
    wrapText(ctx, text, 12, 120, 240, 40);
    ctx.restore();
    aTexture.update();
}

function wrapText(context, text, x, y, maxWidth, lineHeight) {
    var words = text.split(' ');
    var line = '';
	//context.font = "bold 32px Arial";
    //context.textAlign="center";	
    for (var n = 0; n < words.length; n++) {
        var testLine = line + words[n] + ' ';
        var metrics = context.measureText(testLine);
        var testWidth = metrics.width;
        if (testWidth > maxWidth && n > 0) {
            context.fillText(line, x, y);
            line = words[n] + ' ';
            y += lineHeight;
        } else {
            line = testLine;
        }
    }
    context.fillText(line, x, y);
}

function drawArchiRect(aTexture, backColor, foreColor) {
    var ctx = aTexture.getContext();
    var cn = ctx.canvas;
    //alert("H: " + cn.height + " L: " + cn.left + " T: " + cn.top + " W: " + cn.width);
    ctx.font = "bold 32px Arial"; //"40px Tw Cen MT Condensed";
    ctx.save();
    ctx.fillStyle = backColor;
    ctx.fillRect(0, 0, 256, 256);
    ctx.strokeStyle = foreColor;
    ctx.lineWidth = "2";
    ctx.lineJoin = "round";
    ctx.strokeRect(0, 0, 256, 256);
    return ctx;
}

function drawAppSymbol(ctx) {
    ctx.lineWidth = "2";
    ctx.strokeRect(190, 20, 50, 56);
    ctx.fillRect(184, 28, 14, 14);
    ctx.strokeRect(184, 28, 14, 14);
    ctx.fillRect(184, 46, 14, 14);
    ctx.strokeRect(184, 46, 14, 14);
}

function drawSvcSymbol(ctx, backColor, foreColor) {
    ctx.lineJoin = "round";
    ctx.lineWidth = "30";
    ctx.strokeStyle = foreColor;
    ctx.strokeRect(189, 27, 32, 32);
    ctx.strokeStyle = backColor;
    ctx.strokeRect(191, 29, 28, 28);
}

function setProportionalTextonLabel(label, text, color, maxWidth, maxLines) {
    var textBlock = new BABYLON.GUI.TextBlock();
    label.addControl(textBlock);
    var textWidth = text.length * 8;
    var textToShow;
    textToShow = text;
    if (textWidth < maxWidth) {
        label.width = "" + (textWidth + 4) + "px";
    } else {
        label.width = "" + maxWidth + "px";
        var words = text.split(' ');
        var line = '';
        var cnt = 1;
        var charCount;
        //context.textAlign="center";	
        for (var n = 0; n < words.length; n++) {
            var testLine = line + words[n] + ' ';
            var testWidth = testLine.length * 8;
            if (testWidth > maxWidth && n > 0) {
                charCount += line.length;
                line = words[n] + ' ';
                cnt++;
            } else {
                line = testLine;
            }
            if (cnt > maxLines) {
                textToShow = text.substring(1, charCount - 3) + ' ...';
                break;
            }
        }
        label.height = "" + cnt * 24 + "px";
    }
    textBlock.textWrapping = true;
    textBlock.text = textToShow;
    textBlock.color = color;
}

function setProportionalLabelText(label, text, color, maxWidth, maxLines) {
    var textBlock = new BABYLON.GUI.TextBlock();
    var charWidth = 8;
    label.addControl(textBlock);
    var textWidth = text.length * charWidth;
    var textToShow;
    textToShow = text;
    if (textWidth < maxWidth) {
        label.width = "" + (textWidth + 4) + "px";
    } else {
        label.width = "" + maxWidth + "px";
        var cnt = 1;
        if (maxLines > 1) {
            var words = text.split(' ');
            var line = '';

            var charCount;
            for (var n = 0; n < words.length; n++) {
                var testLine = line + words[n] + ' ';
                var testWidth = testLine.length * charWidth;
                if (testWidth > maxWidth && n > 0) {
                    charCount += line.length;
                    line = words[n] + ' ';
                    cnt++;
                } else {
                    line = testLine;
                }
                if (cnt > maxLines) {
                    textToShow = text.substring(1, charCount - 5) + ' ...';
                    break;
                }
            }
        } else {
            textToShow = text.substring(0, maxWidth / charWidth - 5) + ' ...';
        }
        label.height = "" + cnt * 24 + "px";
    }
    textBlock.textWrapping = true;
    textBlock.text = textToShow;
    textBlock.color = color;
}


// , 0.6, "yellow", "black"
function createLabel(mesh, advTexture, visible, alpha, backColor, color) {
    var maxWidth = "60px";
    var singleHeight = "18px";
    var label = new BABYLON.GUI.Rectangle("label for " + mesh.name);
    label.background = backColor;
    label.height = singleHeight;
    label.alpha = alpha;
    label.cornerRadius = 4; //20;
    label.thickness = 1;
    label.linkOffsetY = 1;
    label.linkOffsetX = 1;
    label.linkOffsetZ = 1;
    label.fontFamily = "Consolas";
    label.fontSize = "14";
    label.width = maxWidth;
    advTexture.addControl(label);
    label.linkWithMesh(mesh);
    setProportionalLabelText(label, mesh.name, color, 300, 1)

    label.isVisible = visible;
    mesh._label = label;
}