
// *************************************************************************************************************************************
//http://borjan5/ArchLS/api/SoaScape3D/Render25D/62b702b6-7903-49f4-b609-5ccda2bdba4f
function renderApiTest() {
    var url = "http://borjan5/ArchLS/api/SoaScape3D/Render25D/62b702b6-7903-49f4-b609-5ccda2bdba4f";
    xhr = new XMLHttpRequest();
    xhr.open("GET", url, false);
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.addEventListener("load", apiComplete, false);
    /*
    xhr.addEventListener("error", apiFailed, false);
    xhr.addEventListener("abort", apiCanceled, false); */
    xhr.send();
}
function renderFile25D() {
    var url = "http://borjan5/ArchLS/api/SoaScape3D/Render25D/62b702b6-7903-49f4-b609-5ccda2bdba4f";
    xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.addEventListener("load", apiComplete, false);
    xhr.send();
}

function renderFile() {
    alert('Not there yet');
}

// *************************************************************************************************************************************
function renderLargeLocal(file) {
    var url = file;
	//alert("CORONA: " + url);
    xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.addEventListener("load", apiComplete, false);

    xhr.addEventListener("error", apiFailed, false);
    xhr.addEventListener("abort", apiCanceled, false);
    xhr.send();
}

// *************************************************************************************************************************************
function renderDemo() {
    var url = "http://borjan5/ArchLS/api/SoaScape3D/RenderDemo";
    xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.addEventListener("load", apiComplete, false);

    xhr.addEventListener("error", apiFailed, false);
    xhr.addEventListener("abort", apiCanceled, false);
    xhr.send();
}

// *************************************************************************************************************************************
function renderBuiltIn() {
    model = {
        "Title": "Built-in Data", "Mode": 1,
        "Applications": [
            { "Name": "CRM", "Form": 0, "Id": 0, "ToolTip": "", "V3": 0, "X": 46.5826874, "Y": 0.0, "Z": 30.3723526 },
            { "Name": "Case Management Social", "Form": 0, "Id": 1, "ToolTip": "", "V3": 0, "X": 91.46761, "Y": 36.0399628, "Z": 50.6884537 },
            { "Name": "Case Tracing Social Domain", "Form": 0, "Id": 2, "ToolTip": "", "V3": 0, "X": 51.7862473, "Y": 36.0399628, "Z": 81.5939941 }
        ],
        "Services": [
            { "Name": "Open Cases", "ProviderId": 0, "Form": 1, "Id": 0, "ToolTip": "", "V3": 0, "X": 50.97662, "Y": 6.359515, "Z": 36.7166367 }
        ], "ConsumingRelations": [{ "ConsumerId": 1, "ServiceId": 0, "ProviderId": 0 }, { "ConsumerId": 2, "ServiceId": 0, "ProviderId": 0 }
        ],
        "Helpers": [{ "Form": 0, "X": 69.0251465, "Y": 36.0399628, "Z": 55.9831734, "W": 22.4424629, "H": 25.6108208 }],
        "LastError": ""
    };
    showmodel(-1);
}
// *************************************************************************************************************************************
// *************************************************************************************************************************************
function checkedServices() {
    var element = document.getElementById('CONF-svc');
    CONFIG.showServices = element.checked;
    if (model) {
        showmodel(-1);
        ZoomAll();
    }
}

function checkedStyle() {
    var elements = document.getElementsByName("STYLE");
    for (var i = 0, l = elements.length; i < l; i++) {
        if (elements[i].checked) {
            CONFIG.viewStyle = elements[i].value;
            break;
        }
    }
    //alert(CONFIG.viewStyle);
    if (model) {
        showmodel(-1);
        ZoomAll();
    }       
}

function checkedNodeLabels() {
    var element = document.getElementById('CONF-nl');
    CONFIG.enforceNodeLabels = element.checked;
    if (model) {
        showHideLabels();
    }
}

function checkedServiceLabels() {
    var element = document.getElementById('CONF-sl');
    CONFIG.enforceServiceLabels = element.checked;
    if (model) {
        showHideLabels();
    }
}

function checkedBoundaries() {
    var element = document.getElementById('CONF-bd');
    CONFIG.planesVisible = element.checked;
    var newAlpha;
    if (CONFIG.planesVisible) newAlpha = 0.2; else newAlpha = 0.0;
    if (helper1) helper1.material.alpha = newAlpha;
    if (helper2) helper2.material.alpha = newAlpha;
}

function colorChange(newColor) {
    if (scene) {
        if (newColor == "rand")
            scene.clearColor = BABYLON.Color3.Random();
        else
            scene.clearColor = BABYLON.Color3.FromHexString(newColor);;
    }
}

function myUpload() {
    var file = document.getElementById('9999').files[0];
    var ajax = new XMLHttpRequest;
    var formData = new FormData;
    formData.append('archimateFile', file);
    //alert('aa1');
    ajax.upload.addEventListener("progress", myProgressHandler, false);
    //ajax.addEventListener('');
    ajax.addEventListener('load', myOnLoadHandler, false);
    fName = generateUUID();
    ajax.open('PUT', 'http://BORJAN5/BAB_DATA/' + fName + '.xml', true);
    ajax.send(formData);
}
function generateUUID() {
    var d = new Date().getTime();
    if (window.performance && typeof window.performance.now === "function") {
        d += performance.now();
    }
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
}
function myProgressHandler(event) {
    //your code to track upload progress
    var p = Math.floor(event.loaded / event.total * 100);
    document.getElementById("uuid").textContent = fName + '  ' + p + '%';
}
function myOnLoadHandler(event) {
    // your code on finished upload
    // document.title = event.target.responseText;
    document.getElementById("25D").disabled = false;
    document.getElementById("3D").disabled = false;
    document.getElementById("2D").disabled = false;
    // alert('DONE ' + event.target.responseText);
    document.getElementById("uuid").textContent = '  File uploaded: ' + fName + ".xml";
}

// var CONFIG = { showServices: true, viewStyle: true, toolTipsEnabled: false }
function showConfig() {
    document.getElementById("CONF-svc").checked = CONFIG.showServices;
    document.getElementById("CONF-bill").checked = CONFIG.viewStyle;
    document.getElementById("CONF-nl").checked = CONFIG.enforceNodeLabels;
    document.getElementById("CONF-sl").checked = CONFIG.enforceServiceLabels;
}

function apiComplete(evt) {
    var data = xhr.responseText;
			if(xhr.status == 200) {
				modelList = JSON.parse(data);
				showmodel(0);
			} else {
				var popup = window.open("", "Error", "width=600,height=400, left=20, top=20");
				popup.document.write(data);
			}

}
function apiFailed(evt) {
    alert("An error occurred while transferring the file.");
}
function apiCanceled(evt) {
    alert("The transfer has been canceled by the user.");
}
