#include "..\script_component.hpp"

private _recordTitle = "ENVIRONMENTAL CONDITIONS";

private _text = [
	format[
		"<font size='%1' color='%2' face='%3'>%4</font>",
		GVAR(recordTitleSize),
		GVAR(recordTitleColor),
		GVAR(recordTitleFont),
		_recordTitle
	]
];

private _sunriseColor = "#4A86E8";
private _sunsetColor = "#6AA84F";
private _whiteColor = "#FFFFFF";

private _sunTimes = date call BIS_fnc_sunriseSunsetTime;
private _nearestBase = [player] call EFUNC(common,getNearestBase);

if (isNull _nearestBase) exitWith {
	[
		LEVEL_WARNING,
		QUOTE(COMPONENT),
		"WARNING: No bases found nearby to report weather!",
		[player, [
			["position", getPos player]
		]] call EFUNC(common,addPlayerInfoToArray)
	] call EFUNC(common,log);
	["WARNING: No bases found nearby to report weather!"] call BIS_fnc_error;
};

_text pushBack format[
	"<font size='%1'>Current conditions at nearest weather station: %2</font><br/>",
	GVAR(recordTextBodySize),
	[_nearestBase] call EFUNC(common,getNameOfBase)
];

private _weatherData = [];

if (isClass (configFile >> "CfgPatches" >> "ace_weather")) then {
	// get ace_weather data
	private _barometricPressure = ((getPosASL _nearestBase)#2) call ace_weather_fnc_calculateBarometricPressure;
	private _relHumidity = missionNamespace getVariable ["ace_weather_currentHumidity", 0.5];
	private _temperature = ((getPosASL _nearestBase)#2) call ace_weather_fnc_calculateTemperatureAtHeight;
	private _dewPoint = [_temperature, _relHumidity] call ace_weather_fnc_calculateDewPoint;
	private _windSpeed = [getPosASL _nearestBase, false, false, false] call ace_weather_fnc_calculateWindSpeed;
	private _windChill = [_temperature, _windSpeed] call ace_weather_fnc_calculateWindChill;
	private _heatIndex = [_temperature, _relHumidity] call ace_weather_fnc_calculateHeatIndex;

	toFixed 2;
	private _aceData = [
		["Temperature", format["%1°C / %2°F", _temperature, _temperature * (9/5) + 32]],
		["Wind Chill", format["%1°C / %2°F", _windChill, _windChill * (9/5) + 32]],
		["Heat Index", format["%1°C / %2°F", _heatIndex, _heatIndex * (9/5) + 32]],
		["Dew Point", format["%1°C / %2°F", _dewPoint, _dewPoint * (9/5) + 32]],
		["Wind Speed", format["%1mph / %2kph / %3kts", _windSpeed * 2.237, _windSpeed * 3.6, _windSpeed * 1.944]],
		["Wind Direction", ""],
		["Barometric Pressure", format["%1 hPA", _barometricPressure]],
		["Relative Humidity", format["%1%2", _relHumidity * 100, "%"]],
		["Fog Cover", ""],
		["Rain", ""],
		["Overcast", ""]
	];
	toFixed -1;

	{
		_x params ["_name", "_value"];
		[_weatherData, _name, _value] call BIS_fnc_setToPairs;
	} forEach _aceData;
};

// always add built-in weather
toFixed 2;
private _vanillaData = [
	["Temperature", format["%1°C", (ambientTemperature)#0]],
	["Fog Cover", format["%1%2", fog * 100, "%"]],
	["Overcast", format["%1%2", overcast * 100, "%"]],
	["Rain", format["%1%2", rain * 100, "%"]],
	["Wind Speed", format["%1m/s", windStr]],
	["Wind Direction", format["%1°", windDir]]
];
toFixed -1;

_vanillaData pushBack ["Sunrise", ([_sunTimes select 0, "HH:MM"] call BIS_fnc_timeToString)];
_vanillaData pushBack ["Sunset", ([_sunTimes select 1, "HH:MM"] call BIS_fnc_timeToString)];

// override or set keys for vanilla data into weather data
{
	_x params ["_name", "_value"];
	[_weatherData, _name, _value] call BIS_fnc_setToPairs;
} forEach _vanillaData;

// write lines
{
	_x params ["_name", "_value"];
	_text pushBack format[
		"<font size='%1' face='EtelkaMonospacePro'>%2%3</font>",
		GVAR(recordTextBodySize)-4,
		[_name, "right", " ", 23] call EFUNC(common,padString),
		_value
	];
} forEach _weatherData;

_text = _text joinString "<br/>";

[
	GVAR(subjectIntelID),
	_recordTitle,
	_text
] call EFUNC(common,createOrUpdateDiaryRecord);