/*
 * Copyright 2010, Intel Corporation
 *
 * This file is part of PowerTOP
 *
 * This program file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; version 2 of the License.
 *
 * This program 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 General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program in a file named COPYING; if not, write to the
 * Free Software Foundation, Inc,
 * 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 * or just google for it.
 *
 * Authors:
 *	Arjan van de Ven <arjan@linux.intel.com>
 */
#include <iostream>
#include <fstream>

#include <cstdio>
#include <climits>


#include "device.h"
#include "backlight.h"
#include "../parameters/parameters.h"
#include "../lib.h"

#include <format>


backlight::backlight(const std::string &_name, const std::string &path): device()
{
	min_level = 0;
	max_level = 0;
	start_level = 0;
	end_level = 0;
	sysfs_path = path;
	register_sysfs_path(sysfs_path);
	name = std::format("backlight:{}", _name);
	r_index = get_result_index(name);
	r_index_power = 0;
}

void backlight::start_measurement(void)
{
	max_level = read_sysfs(std::format("{}/max_brightness", sysfs_path));
	start_level = read_sysfs(std::format("{}/actual_brightness", sysfs_path));
}

static int dpms_screen_on(void)
{
	for (const auto &card : list_directory("/sys/class/drm")) {
		if (!card.starts_with("card"))
			continue;
		/* skip connector entries like card0-HDMI-A-1 */
		if (card.find('-') != std::string::npos)
			continue;

		std::string card_path = std::format("/sys/class/drm/{}", card);
		for (const auto &entry : list_directory(card_path)) {
			if (!entry.starts_with("card"))
				continue;

			std::string line = read_sysfs_string(std::format("{}/{}/enabled", card_path, entry));
			if (line != "enabled")
				continue;

			line = read_sysfs_string(std::format("{}/{}/dpms", card_path, entry));
			if (line == "On")
				return 1;
		}
	}
	return 0;
}

int backlight::display_is_on(void) const
{
	return dpms_screen_on();
}

void backlight::end_measurement(void)
{
	double p;
	int _backlight = 0;

	end_level = read_sysfs(std::format("{}/actual_brightness", sysfs_path));

	if (display_is_on()) {
		if (max_level > 0)
			p = 100.0 * (end_level + start_level) / 2 / max_level;
		else
			p = 0;
		_backlight = 100;
	} else {
		p = 0;
	}

	report_utilization(name, p);
	report_utilization(std::format("{}-power", name), _backlight);
}


double backlight::utilization(void) const
{
	if (max_level <= 0)
		return 0.0;
	return 100.0 * (end_level + start_level) / 2 / max_level;
}

static void create_all_backlights_callback(const std::string &d_name)
{
	class backlight *bl;
	bl = new backlight(d_name, std::format("/sys/class/backlight/{}", d_name));
	all_devices.push_back(bl);
}

void create_all_backlights(void)
{
	process_directory("/sys/class/backlight/", create_all_backlights_callback);
	register_parameter("backlight");
	register_parameter("backlight-power");
	register_parameter("backlight-boost-40", 0, 0.5);
	register_parameter("backlight-boost-80", 0, 0.5);
	register_parameter("backlight-boost-100", 0, 0.5);
}

double backlight::power_usage(struct result_bundle *result, struct parameter_bundle *bundle)
{
	double power;
	double factor;
	double _utilization;
	static int bl_index = 0, blp_index = 0, bl_boost_index40 = 0, bl_boost_index80, bl_boost_index100;

	if (!bl_index)
		bl_index = get_param_index("backlight");
	if (!blp_index)
		blp_index = get_param_index("backlight-power");
	if (!bl_boost_index40)
		bl_boost_index40 = get_param_index("backlight-boost-40");
	if (!bl_boost_index80)
		bl_boost_index80 = get_param_index("backlight-boost-80");
	if (!bl_boost_index100)
		bl_boost_index100 = get_param_index("backlight-boost-100");

	power = 0;
	factor = get_parameter_value(bl_index, bundle);
	_utilization = get_result_value(r_index, result);

	power += _utilization * factor / 100.0;

	/*
	 * most machines have a non-linear backlight scale. to compensate, add a fixed value
	 * once the brightness hits 40% and 80%
	 */

	if (_utilization >=99)
		power += get_parameter_value(bl_boost_index100, bundle);
	else if (_utilization >=80)
		power += get_parameter_value(bl_boost_index80, bundle);
	else if (_utilization >=40)
		power += get_parameter_value(bl_boost_index40, bundle);

	factor = get_parameter_value(blp_index, bundle);

	if (!r_index_power) {
		r_index_power = get_result_index(std::format("{}-power", name));
	}
	_utilization = get_result_value(r_index_power, result);

	power += _utilization * factor / 100.0;

	return power;
}

void backlight::collect_json_fields(std::string &_js)
{
    device::collect_json_fields(_js);
    JSON_FIELD(min_level);
    JSON_FIELD(max_level);
    JSON_FIELD(start_level);
    JSON_FIELD(end_level);
    JSON_FIELD(sysfs_path);
    JSON_FIELD(name);
    JSON_FIELD(r_index);
    JSON_FIELD(r_index_power);
}
