/*!
 * TimersModel.cpp
 *
 * Copyright (c) 2016, 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: 02/07/2016
 *
 */

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

#include <string.h>

extern "C" {
#include <libubox/blobmsg_json.h>
}

#include <jsoncpp/json.h>


#include "timers/Timers.h"

#include "timers.h"

namespace { 

using namespace UBus;

static ObjectType gTimersModelUbus_types(
	"TimersModel",
	Method("get", UBUS_CPP(TimersModel, Get)),
	Method("delete", UBUS_CPP(TimersModel, Delete))
);

}

/*!  ----------------------------------------------------------------------------
 *  @fn TimersModel
 *
 *  @brief Constructor of the Timer Objects.
 */
TimersModel::TimersModel (Timers *aTimers) : 
	UBusObject (gTimersModelUbus_types, "domo.timers"),
	mTimers (aTimers)
{
}


/*!  ----------------------------------------------------------------------------
 *  @fn ~TimersModel
 *
 *  @brief Destructor of the Timer Objects.
 */
TimersModel::~TimersModel (void)
{
	
}


/*!  ----------------------------------------------------------------------------
 *  @fn Get
 *
 *  @brief Get the List of timer objects.
 */
int TimersModel::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq, 
					  struct blob_attr *aMsg)
{
	int theResult = 0;
	int theID = -1;
	Json::Reader theReader;
	Json::StyledWriter theWriter;
	Json::Value theRoot;
	struct blob_buf theBuf = {0};
	char *theString = blobmsg_format_json (aMsg, true);

	printf ("Get the TimersModel.\n");

	if (!theReader.parse (theString, theRoot)) {

		fprintf (stderr, "Failed parse the parameters.\n");
		return UBUS_STATUS_INVALID_ARGUMENT;
	}

	theID = theRoot["id"].asInt();
		
	blob_buf_init (&theBuf, 0);

	if (theID == 0)
		blobmsg_add_json_from_string (&theBuf, theWriter.write(mTimers->to_json()).c_str());
	else {
		printf ("we want a specific id :%d\n", theID);
		blobmsg_add_json_from_string (&theBuf, theWriter.write(mTimers->get(theID).to_json()).c_str());
	}

	theResult = ubus_send_reply (aCtx, aReq, theBuf.head);

	blob_buf_free (&theBuf);

	return theResult;
}


/*!  ----------------------------------------------------------------------------
 *  @fn Delete
 *
 *  @brief Delete a Timer Object.
 */
int TimersModel::Delete (struct ubus_context *aCtx, struct ubus_request_data *aReq, 
						 struct blob_attr *aMsg)
{
	int theResult = 0;
	Json::Reader theReader;
	Json::Value theRoot;
	int theID = -1;
	char *theString = blobmsg_format_json (aMsg, true);

	if (!theReader.parse (theString, theRoot)) {

		fprintf (stderr, "Failed parse the parameters.\n");
		return UBUS_STATUS_INVALID_ARGUMENT;
	}

	theID = theRoot["id"].asInt();

	printf ("try to delete id: %d\n", theID);

	theResult = mTimers->remove (theID);

	if (theResult == 0) {

		printf ("Successfully removed.\n");
		//TODO SAVE and return OK.
	}

	return theResult;
}
