/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "lmsensorsbackend.h"
#include <QtDebug>
#include <sensors/sensors.h>

namespace LeechCraft
{
namespace HotSensors
{
	struct StoredChipName
	{
		QByteArray Prefix_;
		sensors_bus_id Bus_;
		int Addr_ = 0;
		QByteArray Path_;

		StoredChipName () = default;
		StoredChipName (const sensors_chip_name*);

		sensors_chip_name ToSensorsChip ();
	};

	struct StoredSubfeature
	{
		StoredChipName Chip_;
		int SF_;
	};

	struct StoredTemp
	{
		double Max_;
		double Crit_;
		StoredSubfeature SF_;
		QString Name_;
	};

	StoredChipName::StoredChipName (const sensors_chip_name *chipName)
	: Prefix_ (chipName->prefix)
	, Bus_ (chipName->bus)
	, Addr_ (chipName->addr)
	, Path_ (chipName->path)
	{
	}

	sensors_chip_name StoredChipName::ToSensorsChip ()
	{
		return { Prefix_.data (), Bus_, Addr_, Path_.data () };
	}

	LmSensorsBackend::LmSensorsBackend (QObject *parent)
	: Backend (parent)
	{
		sensors_init (nullptr);

		EnumerateSensors ();
	}

	LmSensorsBackend::~LmSensorsBackend ()
	{
		sensors_cleanup ();
	}

	void LmSensorsBackend::EnumerateSensors ()
	{
		int nr = 0;
		const sensors_chip_name *chipName = 0;
		while ((chipName = sensors_get_detected_chips (nullptr, &nr)))
		{
			int fnr = 0;
			const sensors_feature *feature = 0;
			while ((feature = sensors_get_features (chipName, &fnr)))
			{
				if (feature->type != SENSORS_FEATURE_TEMP)
					continue;

				int sfnr = 0;
				const sensors_subfeature *subfeature = 0;

				StoredTemp temp
				{
					100,
					100,
					{},
					QString ("%1/%2")
							.arg (chipName->prefix)
							.arg (sensors_get_label (chipName, feature)),
				};
				while ((subfeature = sensors_get_all_subfeatures (chipName, feature, &sfnr)))
				{
					switch (subfeature->type)
					{
					case SENSORS_SUBFEATURE_TEMP_MAX:
						sensors_get_value (chipName, subfeature->number, &temp.Max_);
						break;
					case SENSORS_SUBFEATURE_TEMP_CRIT:
						sensors_get_value (chipName, subfeature->number, &temp.Crit_);
						break;
					case SENSORS_SUBFEATURE_TEMP_INPUT:
						temp.SF_ = StoredSubfeature { { chipName }, subfeature->number };
						break;
					default:
						break;
					}
				}
				Features_ << temp;
			}
		}
	}

	void LmSensorsBackend::update ()
	{
		Readings_t readings;
		for (auto feature : Features_)
		{
			const auto chipName = feature.SF_.Chip_.ToSensorsChip ();

			double value = 0;
			sensors_get_value (&chipName, feature.SF_.SF_, &value);

			readings.append ({ feature.Name_, value, feature.Max_, feature.Crit_ });
		}
		emit gotReadings (readings);
	}
}
}
