const nm_assert = require("assert");
const nm_fs = require("fs");

declare var describe;
declare var it;

_heimdall.init();
describe(
	"heimdall",
	() => {
		function datetimestring_to_timestamp(datetimestring) {
			return Math.floor((new Date(datetimestring)).getTime() / 1000);
		};
		function resolve_condition(condition_raw) {
			return {
				"unknown": _heimdall.enum_condition.unknown,
				"ok": _heimdall.enum_condition.ok,
				"concerning": _heimdall.enum_condition.concerning,
				"critical": _heimdall.enum_condition.critical,
			}[condition_raw];
		};
		const data = JSON.parse(nm_fs.readFileSync("./test-data.json"));
		describe(
			"master.determine_due",
			() => {
				data.cases.forEach(
					case_ => {
						it(
							case_.name,
							() => {
								// setup
								const check : _heimdall.type_check = Object.assign(
									data.parameters["check"],
									{
										"annoy": false,
									}
								);
								const old_item_state : _heimdall.type_item_state = {
									"timestamp": datetimestring_to_timestamp(data.parameters["old_item_state"]["timestamp"]),
									"condition": resolve_condition(case_.input["condition"]),
									"count": (case_.input["threshold_reached"] ? null : 0),
									"last_notification_timestamp": datetimestring_to_timestamp(data.parameters["old_item_state"]["last_notification_timestamp"]),
								};
								const timestamp : int = (
									datetimestring_to_timestamp(
										data.parameters["old_item_state"]["timestamp"]
									)
									+
									(
										{
											"none": 0,
											"attentive": data.parameters["check"]["schedule"]["attentive_interval"],
											"regular": data.parameters["check"]["schedule"]["regular_interval"],
											"reminding": data.parameters["check"]["schedule"]["reminding_interval"]
										}[case_.input["passed"]]
									)
									+
									60
								);
								
								// execution
								const result : boolean = _heimdall.master.determine_due(
									check,
									old_item_state,
									{
										"timestamp": timestamp,
									}
								);
								
								// assertions
								nm_assert.equal(result, case_.output);
							}
						);
					}
				);
			}
		);
		/*
		describe(
			"master.determine_shall_send_notification",
			() => {
				it(
					"test",
					() => {
						// setup
						const check : _heimdall.type_check = {
							"name": "iks.web.2",
							"title": "web.2",
							"kind": "http_request",
							"parameters": {
								"request": {
									"method": "GET",
									"target": "https://iks-sachsen.de"
								},
								"timeout": 5,
								"follow_redirects": true,
								"response": {
									"status_code": 200
								},
								"critical": true
							},
							"custom": null,
							"active": false,
							"threshold": 3,
							"annoy": false,
							"schedule": {
								"regular_interval": 60,
								"attentive_interval": 10,
								"reminding_interval": 86400
							},
							"notifications": [
								{
									"kind": "console",
									"parameters": {
									}
								}
							]
						};
						const old_item_state : _heimdall.type_item_state = {
							"timestamp": 1693493942,
							"condition": _heimdall.enum_condition.critical,
							"count": 2,
							"last_notification_timestamp": 1693493942,
						};
						const timestamp : int = 1693495622;
						
						// execution
						const result : boolean = _heimdall.master.determine_shall_send_notification(
							check,
							false,
							3,
							timestamp,
							old_item_state,
							{
								"condition": _heimdall.enum_condition.critical,
								"info": {},
							}
						);
						
						// assertions
						nm_assert.equal(result, false);
					}
				);
			}
		);
		 */
	}
);
