/*
 * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

var modalPopup = ".modal";
var modalPopupContainer = modalPopup + " .modal-content";
var modalPopupContent = modalPopup + " .modal-content";
var body = "body";

var backendEndBasePath = "/api/device-mgt/v1.0";

/*
 * set popup maximum height function.
 */
function setPopupMaxHeight() {
    $(modalPopupContent).css('max-height', ($(body).height() - ($(body).height() / 100 * 30)));
    $(modalPopupContainer).css('margin-top', (-($(modalPopupContainer).height() / 2)));
}

/*
 * show popup function.
 */
function showPopup() {
    $(modalPopup).modal('show');
    setPopupMaxHeight();
}

/*
 * hide popup function.
 */
function hidePopup() {
    $('label[for=deviceName]').remove();
    $('.control-group').removeClass('success').removeClass('error');
    $(modalPopupContent).html('');
    $(modalPopup).modal('hide');
}

/*
 * DOM ready functions.
 */
$(document).ready(function () {
    attachEvents();
});

function attachEvents() {
    /**
     * Following click function would execute
     * when a user clicks on "Download" link
     * on Device Management page in WSO2 DC Console.
     */
    $(".download-link").click(function(){
        toggleEnrollment();
    });

    function toggleEnrollment(){
        $(modalPopupContent).html($("#qr-code-modal").html());
        generateQRCode(modalPopupContent + " .qr-code");
        modalDialog.show();
    }
}

function artifactUpload() {
	var contentType = "application/json";

	var urix = backendEndBasePath + "/admin/devicetype/deploy/android_sense";
	var defaultStatusClasses = "fw fw-stack-1x";
	var content = $("#androidsense-statistic-response-template").find(".content");
	var title = content.find("#title");
	var statusIcon = content.find("#status-icon");
	var data = {}
	invokerUtil.post(urix, data, function (data) {
		title.html("Deploying statistic artifacts. Please wait...");
		statusIcon.attr("class", defaultStatusClasses + " fw-check");
		$(modalPopupContent).html(content.html());
		showPopup();
		setTimeout(function () {
			hidePopup();
			location.reload(true);
		}, 5000);

	}, function (jqXHR) {
		title.html("Failed to deploy artifacts, Please contact administrator.");
		statusIcon.attr("class", defaultStatusClasses + " fw-error");
		$(modalPopupContent).html(content.html());
		showPopup();
	}, contentType);
}

