/*
 * 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.
 */

/*
 * On operation click function.
 * @param selection: Selected operation
 */
function operationSelect(selection) {
    modalDialog.header($(" .operation[data-operation-code=" + selection + "] .header-content").html());
    modalDialog.content($(" .operation[data-operation-code=" + selection + "] .body-content").html());
    modalDialog.show();
}

function submitForm(formId) {
    $("#basic-modal-view #btnSend").addClass("hidden");
    $("#basic-modal-view #lbl-execution").removeClass("hidden");
    var form = $("#basic-modal-view #" + formId);
    var uri = form.attr("action");
    var deviceId = form.data("device-id");
    var contentType = form.data("content-type");
    var operationCode = form.data("operation-code");
    var uriencodedQueryStr = "";
    var uriencodedFormStr = "";
    var payload = {};
    form.find("input").each(function () {
        var input = $(this);
        if (input.data("param-type") == "path") {
            uri = uri.replace("{" + input.attr("id") + "}", input.val());
        } else if (input.data("param-type") == "query") {
            var prefix = (uriencodedQueryStr == "") ? "?" : "&";
            uriencodedQueryStr += prefix + input.attr("id") + "=" + input.val();
        } else if (input.data("param-type") == "form") {
            var prefix = (uriencodedFormStr == "") ? "" : "&";
            uriencodedFormStr += prefix + input.attr("id") + "=" + input.val();
            if(input.attr("type") == "text"){
                payload[input.attr("id")] = input.val();
            } else if(input.attr("type") == "checkbox"){
                payload[input.attr("id")] = input.is(":checked");
            }
        }
    });
    uri += uriencodedQueryStr;
    var httpMethod = form.attr("method").toUpperCase();
    //var contentType = form.attr("enctype");

    if (contentType == undefined || contentType == "") {
        contentType = "application/x-www-form-urlencoded";
        payload = uriencodedFormStr;
    }

    //setting responses callbacks
    var defaultStatusClasses = "fw fw-stack-1x";
    var content = $("#operation-response-template").find(".content");
    var title = content.find("#title");
    var statusIcon = content.find("#status-icon");
    var description = content.find("#description");
    description.html("");
    var resetLoader = function () {
        $("#basic-modal-view #btnSend").addClass("hidden");
        $("#basic-modal-view #lbl-execution").removeClass("hidden");
    };


    var successCallBack = function (response) {
        var res = response;
        try {
            res = JSON.parse(response).messageFromServer;
        } catch (err) {
            //do nothing
        }
        console.log(content);
        modalDialog.header("Operation Triggered!");
        modalDialog.content("");
        modalDialog.content(res);
        modalDialog.show();

    };
    var errorCallBack = function (response) {
        var reason = (response.responseText == "null")?response.statusText:response.responseText;
        try {
            reason = JSON.parse(reason).message;
        } catch (err) {
            //do nothing
        }
        modalDialog.header(" An Error Occurred!");
        modalDialog.content("");
        modalDialog.content(reason);
        modalDialog.showAsError();

    };
    //executing http request
    if (httpMethod == "GET") {
        invokerUtil.get(uri, successCallBack, errorCallBack, contentType);
    } else if (httpMethod == "POST") {
        var deviceList = [deviceId];
        payload = generatePayload(operationCode, payload, deviceList);
        invokerUtil.post(uri, payload, successCallBack, errorCallBack, contentType);
    } else if (httpMethod == "PUT") {
        invokerUtil.put(uri, payload, successCallBack, errorCallBack, contentType);
    } else if (httpMethod == "DELETE") {
        invokerUtil.delete(uri, successCallBack, errorCallBack, contentType);
    } else {
        title.html("An Error Occurred!");
        statusIcon.attr("class", defaultStatusClasses + " fw-error");
        description.html("This operation requires http method: " + httpMethod + " which is not supported yet!");
        resetLoader();
        $(modalPopupContent).html(content.html());
    }
}

$(document).on('submit', 'form', function (e) {
    e.preventDefault();
    var postOperationRequest = $.ajax({
        url: $(this).attr("action") + '&' + $(this).serialize(),
        method: "post"
    });

    var btnSubmit = $('#btnSend', this);
    btnSubmit.addClass('hidden');

    var lblSending = $('#lblSending', this);
    lblSending.removeClass('hidden');

    var lblSent = $('#lblSent', this);
    postOperationRequest.done(function (data) {
        lblSending.addClass('hidden');
        lblSent.removeClass('hidden');
        setTimeout(function () {
            hidePopup();
        }, 3000);
    });

    postOperationRequest.fail(function (jqXHR, textStatus) {
        lblSending.addClass('hidden');
        lblSent.addClass('hidden');
    });
});

// Constants to define operation types available
var operationTypeConstants = {
    "PROFILE": "profile",
    "CONFIG": "config",
    "COMMAND": "command"
};


var generatePayload = function (operationCode, operationData, deviceList) {
    var payload = {};
    var operationType;
    switch (operationCode) {
        case iosOperationConstants["NOTIFICATION_OPERATION_CODE"]:
            operationType = operationTypeConstants["PROFILE"];
            payload = {
                "operation": {
                    "messageTitle": operationData["messageTitle"],
                    "messageText": operationData["messageText"]
                }
            };
            break;
        case iosOperationConstants["APPLICATION_OPERATION_CODE"]:
            payload = {
                "operation": {
                    "restriction-type": operationData["restrictionType"],
                    "restricted-applications": operationData["restrictedApplications"]
                }
            };
            break;
        default:
            // If the operation is neither of above, it is a command operation
            operationType = operationTypeConstants["COMMAND"];
            // Operation payload of a command operation is simply an array of device IDs
            payload = deviceList;
    }

    if (operationType == operationTypeConstants["PROFILE"] && deviceList) {
        payload["deviceIDs"] = deviceList;
    }
    return payload;
};


// Constants to define iOS Operation Constants
var iosOperationConstants = {
    "PASSCODE_POLICY_OPERATION_CODE": "PASSCODE_POLICY",
    "RESTRICTIONS_OPERATION_CODE": "RESTRICTION",
    "VPN_OPERATION_CODE": "VPN",
    "WIFI_OPERATION_CODE": "WIFI",
    "EMAIL_OPERATION_CODE": "EMAIL",
    "AIRPLAY_OPERATION_CODE": "AIR_PLAY",
    "LDAP_OPERATION_CODE": "LDAP",
    "DOMAIN_OPERATION_CODE": "DOMAIN",
    "CALENDAR_OPERATION_CODE": "CALDAV",
    "NOTIFICATION_OPERATION_CODE": "NOTIFICATION",
    "CALENDAR_SUBSCRIPTION_OPERATION_CODE": "CALENDAR_SUBSCRIPTION",
    "APN_OPERATION_CODE": "APN",
    "APPLICATION_OPERATION_CODE": "APP-RESTRICTION",
    "CELLULAR_OPERATION_CODE": "CELLULAR",
    "PER_APP_VPN_OPERATION_CODE": "PER_APP_VPN",
    "APP_TO_PER_APP_VPN_MAPPING_OPERATION_CODE": "APP_TO_PER_APP_VPN_MAPPING"
};