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

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

#include <string.h>

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

#include <json-c/json.h>

#include "sequences/sequences-manager.h"

#include "sequences_model.h"

namespace
{

using namespace UBus;

static ObjectType gSequencesModelUbus_types(
	"SequencesModel",
	Method("get", UBUS_CPP(SequencesModel, get)),
	Method("delete", UBUS_CPP(SequencesModel, del)));
}

/*!  ----------------------------------------------------------------------------
 *  @fn SequencesModel
 *
 *  @brief Constructor of the Timer Objects.
 */
SequencesModel::SequencesModel(SequencesManager *a_sequences_manager) : 
	UBusObject(gSequencesModelUbus_types, "domo.sequences"),
	m_sequences_manager(a_sequences_manager)
{
}

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

/*!  ----------------------------------------------------------------------------
 *  @fn get
 *
 *  @brief Get the List of timer objects.
 */
int SequencesModel::get(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg)
{
	int the_result = 0;
#if 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);
#endif
	return the_result;
}

/*!  ----------------------------------------------------------------------------
 *  @fn del
 *
 *  @brief Delete a Timer Object.
 */
int SequencesModel::del(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg)
{
	int the_result = 0;
#if 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.
	}
#endif
	return the_result;
}
