/*!
 * Devices.cpp
 *
 * Copyright (c) 2015, 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: 27/03/2015
 *
 */

#error deprecated
 
/*------------------------------- INCLUDES ----------------------------------*/

#include <cstdio>
#include <iostream>
#include <fstream>

#include "helpers/Strings.h"

#include "Devices.h"



/*!  ----------------------------------------------------------------------------
 *  @fn Devices
 *
 *  @brief Constructor of the Devices Models.
 */
Devices::Devices (std::string aPath): 
mDevicesPath (aPath)
{
}


/*!  ----------------------------------------------------------------------------
 *  @fn ~Devices
 *
 *  @brief Destructor of the Devices Models.
 */
Devices::~Devices (void)
{
}


/*!  ----------------------------------------------------------------------------
 *  @fn Load
 *
 *  @brief Load the Device object.
 */
int Devices::Load (void)
{
	Json::Reader theReader;
	std::ifstream theDevicesFile (mDevicesPath.c_str());

	if (!theReader.parse (theDevicesFile, mRoot)) {

		fprintf (stderr, "Failed to parse the Devices File (%s).\n",
				 mDevicesPath.c_str());
		return -1;
	}

	return 0;
}


/*!  ----------------------------------------------------------------------------
 *  @fn get
 *
 *  @brief get the list of devices of a specific capabilities.
 */
std::string Devices::get (const std::string &aCapability)
{
	Json::StyledWriter theWriter;
	Json::Value theOutput;

	printf ("Get aCapability: %s\n", aCapability.c_str());

	theOutput[aCapability] = mRoot[aCapability]["data"];
	return theWriter.write (theOutput).c_str();
}


/*!  ----------------------------------------------------------------------------
 *  @fn get
 *
 *  @brief get the device of a specific capabilities, with the ID.
 */
Json::Value Devices::get (const std::string &aCapability, int anId)
{
	Json::Value theResult;
	for (const Json::Value& theElement : mRoot[aCapability]["data"]) {

    	if (theElement["id"].asInt() == anId) {

    		theResult = theElement;
    	}
	}

    return theResult;
}


/*!  ----------------------------------------------------------------------------
 *  @fn set
 *
 *  @brief get the device of a specific capabilities, with the ID.
 */
int Devices::set (const std::string &aCapability, int anId, const Json::Value &aNewElement)
{
	bool theStateChange = false;
	for (Json::Value& theElement : mRoot[aCapability]["data"]) {

    	if (theElement["id"].asInt() == anId) {

    		Json::Value::Members memberNames = theElement.getMemberNames();
			for(unsigned int i=0; i<memberNames.size(); ++i) {
				std::string theMName = memberNames[i];
				if (theElement[theMName] != aNewElement[theMName]) {
					theElement[theMName] = aNewElement[theMName];
					theStateChange = true;
				}
			}
    	}
	}

	if (theStateChange) {
		printf ("Model Change, We save it....\n");
		Json::StyledStreamWriter theWriter;
		std::ofstream theOutput (mDevicesPath);
		theWriter.write (theOutput,mRoot);
	}

	return 0;
}


/*!  ----------------------------------------------------------------------------
 *  @fn setDevice
 *
 *  @brief get the device of a specific capabilities, with the ID.
 */
bool Devices::setDevice (const std::string &aCapability,
						 const std::string &aDeviceName, bool aState)
{
	fprintf (stdout, "setDevice cap:%s deviceName: %s\n", aCapability.c_str(),
	 aDeviceName.c_str());

	for (Json::Value& theCap : mRoot) {
		fprintf (stdout, "ici: %s\n", theCap["speach_name"].asString().c_str());
		if (theCap["speach_name"].asString() == aCapability) {
			for (Json::Value& theElement : theCap["data"]) {

    			if (theElement["speach_name"].asString() == aDeviceName) {
    				fprintf (stdout, "Device found.\n");
    				if (theElement["state"] != aState) {

    					printf ("Model Change, We save it....\n");
						Json::StyledStreamWriter theWriter;
						std::ofstream theOutput (mDevicesPath);
						theWriter.write (theOutput,mRoot);
						//TODO faire le chemin la bonne capability.
						return true;
					}
    			}
			}
		}
	}	
	
	return false;
}


/*!  ----------------------------------------------------------------------------
 *  @fn isCapability
 *
 *  @brief return true if the capability is available.
 */
bool Devices::isCapability (const std::string &aCapability)
{
	fprintf (stdout, "try to found: %s\n", aCapability.c_str());
	for (Json::Value& theElement : mRoot) {
		fprintf (stdout, "ici: %s\n", theElement["speach_name"].asString().c_str());
		
		if (toupper(theElement["speach_name"].asString()) == aCapability)
			return true;
	}

	return false;
}
