/*!
 * speach_command.cpp
 *
 * 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: 04/05/2015
 *
 */

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

#include <vector>

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

#include <ubus-cpp/UBusCall.h>

#include "devices/Devices.h"

#include "helpers/Tokenizer.h"
#include "helpers/Strings.h"

#include "speach_command.h"

namespace { 

using namespace UBus;

static ObjectType gSpeachCommandModelUbus_types(
	"speach",
	Method("post", UBUS_CPP(SpeachCommandModel, Post))
);

}

/*!  ----------------------------------------------------------------------------
 *  @fn SpeachCommandModel
 *
 *  @brief Constructor of the UBus Speach Command execution.
 */
SpeachCommandModel::SpeachCommandModel (Devices *aDevice) : 
	UBusObject (gSpeachCommandModelUbus_types, "domo.speach.command"),
	mDevices (aDevice)
{
}


/*!  ----------------------------------------------------------------------------
 *  @fn ~SpeachCommandModel
 *
 *  @brief Destructor of the UBus Speach Command execution.
 */
SpeachCommandModel::~SpeachCommandModel (void)
{
}


/*!  ----------------------------------------------------------------------------
 *  @fn Post
 *
 *  @brief Get the List of the Capabilities.
 */
int SpeachCommandModel::Post (struct ubus_context *aCtx, struct ubus_request_data *aReq, 
							struct blob_attr *aMsg)
{
	int theResult = 0;
	struct blob_buf theBuf = {0};
	char *theString = blobmsg_format_json (aMsg, true);
	Json::Reader theReader;
	Json::Value theRoot;
	std::string theCommand;

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

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

  	theCommand = theRoot["command"].asString();
	
	if (theCommand.empty()) {

		fprintf (stderr, "the Command parameter is not present or empty...\n");
		return UBUS_STATUS_INVALID_ARGUMENT;
	}

	theResult = AnalyseAndExecute (theCommand);

	blob_buf_init (&theBuf, 0);

	blobmsg_add_json_from_string (&theBuf,"{}");
	
	ubus_send_reply (aCtx, aReq, theBuf.head);

	blob_buf_free (&theBuf);

	return theResult;
}


/*!  ----------------------------------------------------------------------------
 *  @fn AnalyseAndExecute
 *
 *  @brief analyse and Execute the following commmand.
 */
int SpeachCommandModel::AnalyseAndExecute (std::string aCommand)
{
	std::vector <std::string> theTokens;
	Tokenizer s(aCommand, " ");
	fprintf (stdout, "Analyse: <%s>\n", aCommand.c_str());
	int theSM  = 0;
	bool theState = false;
	std::string theCapability;
	std::string theDeviceName;

	while (s.NextToken()) {

		theTokens.push_back (s.GetToken());
		fprintf (stdout, "word: <%s>\n", s.GetToken().c_str());
		
		// Check the Command
		if (theSM == 0) {
			if (s.GetToken() == "allumer") {
				theState = true;
			}
			else if (s.GetToken() == "Éteindre") {
				theState = false;
			}
			else if (s.GetToken() == "ouvrir") {
				theState = true;
			}
			else if (s.GetToken() == "fermer") {
				theState = false;
			}
			else {
				fprintf (stderr, "Uknown command: %s:\n", s.GetToken().c_str());
				return -1;
			}
		}
/* TODO		
		else if (theSM == 1) {

			theCapability = s.GetToken();
			 
			if (!mDevices->isCapability (theCapability)) {

				fprintf (stderr, "Uknown capability: <%s>\n", s.GetToken().c_str());
				return -2;
			}
		}
		else if (theSM == 2) {
			theDeviceName =  s.GetToken();

			if (!mDevices->setDevice (theCapability, theDeviceName, theState)) {
				
				fprintf (stderr, "Uknown Device: <%s>\n", s.GetToken().c_str());
				return -3;
			}
		}
*/
		theSM++;
	}

	return 0;
}
