'use strict';

/*
 * *******************************************************************************
 *  * Copyright (c) 2018 Edgeworx, Inc.
 *  *
 *  * This program and the accompanying materials are made available under the
 *  * terms of the Eclipse Public License v. 2.0 which is available at
 *  * http://www.eclipse.org/legal/epl-2.0
 *  *
 *  * SPDX-License-Identifier: EPL-2.0
 *  *******************************************************************************
 *
 */

//Set up the click handler for the create new element cancel button
$(document).apwClickAction({
	'clickablesSelector': '#auth-create-catalog-element-button-cancel',
	'actionFunction': 'clearCreateCatalogElementForm'
});

//Set up the click handler for the create element confirm button
$(document).apwClickAction({
	'clickablesSelector': '#auth-create-catalog-element-button-confirm',
	'actionFunction': 'createNewCatalogElement'
});

//Set up the click handler for the update element buttons
$(document).apwClickAction({
	'clickablesSelector': '#catalogelementlist a.publish-element-update-button',
	'actionFunction': 'clickUpdateCatalogElement',
	'loadDataFields': '{"elementKey":"elementkey"}'
});

//Set up the click handler for the update element save button
$(document).apwClickAction({
	'clickablesSelector': '#auth-update-catalog-element-button-confirm',
	'actionFunction': 'saveUpdateCatalogElement'
});

//Set up the click handler for the delete element buttons
$(document).apwClickAction({
	'clickablesSelector': '#catalogelementlist a.publish-element-delete-button',
	'actionFunction': 'clickDeleteCatalogElement',
	'loadDataFields': '{"elementKey":"elementkey"}'
});

//Set up the click handler for the delete element confirm button
$(document).apwClickAction({
	'clickablesSelector': '#auth-delete-catalog-element-button-confirm',
	'actionFunction': 'confirmDeleteCatalogElement'
});

function confirmDeleteCatalogElement() {
	var apiEndpointDeleteCatalogElement = 'https://iofog.org/api/v2/authoring/organization/element/delete';

	var deleteFields = "ElementKey=";
	var iID = $("#auth-delete-catalog-element-message-name").data("elementkey");

	deleteFields += iID;

	$(document).apwManualPostUp({
		'postingDisplayFunction': 'apwManualPostUpPostingDisplayBlank',
		'postingFinishDisplayFunction': 'apwManualPostUpPostingDisplayFinishBlank',
		'postingResponseHandlerFunction': 'deleteCatalogElementAPI',
		'passToken': 'Y',
		'postURL': apiEndpointDeleteCatalogElement,
		'postFailedFunction': 'deleteCatalogElementAPIFail',
		'postData': deleteFields
	});
}

function deleteCatalogElementAPI(response) {
	var respObj = $.parseJSON(response);

	if (respObj.status == "ok") {
		var theElementKey = respObj.elementkey;

		//Fetch the screen object and update its fields accordingly
		var lookString = "#publish-element-list-item-" + theElementKey;
		var curItemFromDOM = $(lookString);

		$(curItemFromDOM).remove();
	}
}

function clickDeleteCatalogElement(data) {
	var elementKey = data.elementKey;

	//Get the current information about the ioFog instance from the object itself
	var lookString = "#publish-element-list-item-" + elementKey;
	var curItemFromDOM = $(lookString);

	var curName = "";
	var curCat = "";

	//If this item exists, get the fields
	if (curItemFromDOM.length) {
		curName = $(curItemFromDOM).find(".publish-element-name").text();
		curCat = $(curItemFromDOM).find(".publish-element-category").text();
	}

	//Put the information into the delete track modal window
	$("#auth-delete-catalog-element-message-name").text(curName);
	$("#auth-delete-catalog-element-message-category").text(curCat);

	$("#auth-delete-catalog-element-message-name").data("elementkey", elementKey);
}

function saveUpdateCatalogElement() {
	var apiEndpointUpdateCatalogElement = 'https://iofog.org/api/v2/authoring/organization/element/update';

	var updateFields = "ElementKey=";
	var iID = $("#publish-element-update-elementname").data("elementkey");
	var iName = $("#publish-element-update-elementname").val();
	var iDesc = $("#publish-element-update-elementdescription").val();
	var iCat = $("#publish-element-update-elementcategory").val();
	var iPub = $("#publish-element-update-elementpublisher").val();
	var iCont = $("#publish-element-update-elementcontainerimage").val();

	updateFields += iID + "&ElementName=" + iName + "&ElementDescription=" + iDesc + "&ElementCategory=" + iCat + "&ElementPicture=&ElementPublisher=" + iPub + "&ElementContainerImage=" + iCont + "&ElementFogTypes=[1]";

	$(document).apwManualPostUp({
		'postingDisplayFunction': 'apwManualPostUpPostingDisplayBlank',
		'postingFinishDisplayFunction': 'apwManualPostUpPostingDisplayFinishBlank',
		'postingResponseHandlerFunction': 'updateCatalogElementAPI',
		'passToken': 'Y',
		'postURL': apiEndpointUpdateCatalogElement,
		'postFailedFunction': 'updateCatalogElementAPIFail',
		'postData': updateFields
	});
}

function updateCatalogElementAPI(response) {
	var respObj = $.parseJSON(response);

	if (respObj.status == "ok") {
		var theElementKey = respObj.elementkey;

		//Fetch the screen object and update its fields accordingly
		var lookString = "#publish-element-list-item-" + theElementKey;
		var curItemFromDOM = $(lookString);

		//Get the values out of the update form
		var iName = $("#publish-element-update-elementname").val();
		var iDesc = $("#publish-element-update-elementdescription").val();
		var iCat = $("#publish-element-update-elementcategory").val();
		var iPub = $("#publish-element-update-elementpublisher").val();
		var iCont = $("#publish-element-update-elementcontainerimage").val();

		$(curItemFromDOM).find(".publish-element-name").text(iName);
		$(curItemFromDOM).find(".publish-element-description").text(iDesc);
		$(curItemFromDOM).find(".publish-element-category").text(iCat);
		$(curItemFromDOM).find(".publish-element-publisher").text(iPub);
		$(curItemFromDOM).find(".publish-element-containerimage").text(iCont);
	}
}

function clickUpdateCatalogElement(data) {
	var elementKey = data.elementKey;

	//Get the current information about the ioFog instance from the object itself
	var lookString = "#publish-element-list-item-" + elementKey;
	var curItemFromDOM = $(lookString);

	var curName = "";
	var curDesc = "";
	var curCat = "";
	var curPub = "";
	var curCont = "";

	//If this item exists, get the fields
	if (curItemFromDOM.length) {
		curName = $(curItemFromDOM).find(".publish-element-name").text();
		curDesc = $(curItemFromDOM).find(".publish-element-description").text();
		curCat = $(curItemFromDOM).find(".publish-element-category").text();
		curPub = $(curItemFromDOM).find(".publish-element-publisher").text();
		curCont = $(curItemFromDOM).find(".publish-element-containerimage").text();
	}

	//Put the information into the update integrator modal window
	$("#publish-element-update-elementname").val(curName);
	$("#publish-element-update-elementdescription").val(curDesc);
	$("#publish-element-update-elementcategory").val(curCat);
	$("#publish-element-update-elementpublisher").val(curPub);
	$("#publish-element-update-elementcontainerimage").val(curCont);

	$("#publish-element-update-elementname").data("elementkey", elementKey);
}

function createNewCatalogElement() {
	var apiEndpointCreateCatalogElement = 'https://iofog.org/api/v2/authoring/organization/element/create';

	var createFields = "ElementName=";
	var iName = $("#publish-element-create-elementname").val();
	var iDesc = $("#publish-element-create-elementdescription").val();
	var iCat = $("#publish-element-create-elementcategory").val();
	var iPub = $("#publish-element-create-elementpublisher").val();
	var iCont = $("#publish-element-create-elementcontainerimage").val();

	createFields += iName + "&ElementDescription=" + iDesc + "&ElementCategory=" + iCat + "&ElementPicture=&ElementPublisher=" + iPub + "&ElementContainerImage=" + iCont + "&ElementFogTypes=[1]";

	$(document).apwManualPostUp({
		'postingDisplayFunction': 'apwManualPostUpPostingDisplayBlank',
		'postingFinishDisplayFunction': 'apwManualPostUpPostingDisplayFinishBlank',
		'postingResponseHandlerFunction': 'createCatalogElementAPI',
		'passToken': 'Y',
		'postURL': apiEndpointCreateCatalogElement,
		'postFailedFunction': 'createCatalogElementAPIFail',
		'postData': createFields
	});

	clearCreateCatalogElementForm();
}

function createCatalogElementAPI(response) {
	var respObj = $.parseJSON(response);

	if (respObj.status == "ok") {
		var theElementObj = respObj.element;

		var elementContainer = $("#catalogelementlist");

		var tmpNewElement = $('<li class="publish-element-list-item" id="publish-element-list-item-' + theElementObj.elementkey + '" data-elementkey="' + theElementObj.elementkey + '"><div class="collapsible-header publish-element-name">' + theElementObj.name + '</div><div class="collapsible-body"><p class="publish-element-details"><strong>Description: </strong><span class="publish-element-description">' + theElementObj.description + '</span></p><p class="publish-element-details"><strong>Category: </strong><span class="publish-element-category">' + theElementObj.category + '</span></p><p class="publish-element-details"><strong>Publisher: </strong><span class="publish-element-publisher">' + theElementObj.publisher + '</span></p><p class="publish-element-details"><strong>Container Image: </strong><span class="publish-element-containerimage">' + theElementObj.containerimage + '</span></p><p class="publish-element-details">&nbsp;</p><div class="row action"><div class="col s2"><a class="modal-trigger cyan-text publish-element-update-button" href="#updatecatalogelement" data-elementkey="' + theElementObj.elementkey + '"><i class="fa fa-pencil"></i></a></div><div class="col s2"><a class="modal-trigger cyan-text publish-element-delete-button" href="#deletecatalogelement" data-elementkey="' + theElementObj.elementkey + '"><i class="fa fa-trash"></i></a></div></div><!--end row--></div></li>');

		elementContainer.append(tmpNewElement);

		$('.collapsible').collapsible();
		tmpNewElement.find('.modal-trigger').leanModal();
	}
}

function clearCreateCatalogElementForm() {
	$("#publish-element-create-elementname").val("");
	$("#publish-element-create-elementdescription").val("");
	$("#publish-element-create-elementcategory").val("");
	$("#publish-element-create-elementpublisher").val("");
	$("#publish-element-create-elementcontainerimage").val("");
}