/*!
 * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 *
 * @Author:  NADAL Jean-Baptiste
 * @Date: 07/07/2017
 */

/*------------------------------- INCLUDES ----------------------------------*/

extern "C" {
#include <libubus.h>
}

#include <ubus-cpp/ubus-call.h>

#include "ubus-reason.h"

/*!  ----------------------------------------------------------------------------
 *  @fn UbusReason
 *
 *  @brief constructor of the ubus reason.
 */
UbusReason::UbusReason(void)
{
}

/*!  ----------------------------------------------------------------------------
 *  @fn get
 *
 *  @brief return as an http code from an ubus code.
 */
uint16_t UbusReason::get(int16_t an_exec_result)
{
	int the_result_code;
	// printf("%s result code: %d\n", __PRETTY_FUNCTION__, an_exec_result);

	switch (an_exec_result)
	{
	case 0:
		the_result_code = 200; // HTTP_OK
		break;
	case -1:
		the_result_code = 404; // HTTP_NOT_FOUND
		break;
	case -2:
		the_result_code = 507; // HTTP_INSUFFICIENT_STORAGE
		break;
	case UBUS_STATUS_METHOD_NOT_FOUND:
		the_result_code = 405; // HTTP_METHOD_NOT_ALLOWED
		break;
	case -3:
		the_result_code = 500; // HTTP_INTERNAL_SERVER_ERROR
		break;
	case -4: // NotImplemented
	case 4:
		the_result_code = 501; // HTTP_NOT_IMPLEMENTED
		break;
	case kExecInvalidArguments:
	case UBUS_STATUS_INVALID_ARGUMENT:
		the_result_code = 412; // HTTP_PRECONDITION_FAILED
		break;
	case kExecFailed:
		the_result_code = 400; // HTTP_BAD_REQUEST
		break;
	default:
		the_result_code = 500; // HTTP_INTERNAL_SERVER_ERROR
		break;
	}

	return the_result_code;
}
