/**
 * Copyright © Volker Schukai and all contributing authors, {{copyRightYear}}. All rights reserved.
 * Node module: @schukai/monster
 *
 * This source code is licensed under the GNU Affero General Public License version 3 (AGPLv3).
 * The full text of the license can be found at: https://www.gnu.org/licenses/agpl-3.0.en.html
 *
 * For those who do not wish to adhere to the AGPLv3, a commercial license is available.
 * Acquiring a commercial license allows you to use this software without complying with the AGPLv3 terms.
 * For more information about purchasing a commercial license, please contact Volker Schukai.
 *
 * SPDX-License-Identifier: AGPL-3.0
 */

import { Base } from "../types/base.mjs";
import { validateInstance, validateInteger } from "../types/validate.mjs";
import { LogEntry } from "./logentry.mjs";
import { ALL, DEBUG, ERROR, FATAL, INFO, OFF, TRACE, WARN } from "./logger.mjs";

export { Handler };

/**
 * The log handler is the interface between the log entries and the log listeners.
 *
 * @fragments /fragments/libraries/logging/handler/
 *
 * @example /examples/libraries/logging/handler/simple/ Filter log entries before forwarding them
 * @example /examples/libraries/logging/handler/level-switch/ Switch handler thresholds with the convenience methods
 * @example /examples/libraries/logging/handler/structured-output/ Forward structured log entry data into a custom buffer
 * @example /examples/libraries/logging/handler/off-state/ Disable a handler completely with the off level
 * @license AGPLv3
 * @since 1.5.0
 * @copyright Volker Schukai
 * @summary A logging handler base class for processing and exporting log entries.
 */
class Handler extends Base {
	constructor() {
		super();

		/**
		 * Loglevel
		 *
		 * @type {integer}
		 */
		this.loglevel = OFF;
	}

	/**
	 * This is the central log function. this method must be
	 * overwritten by derived handlers with their own logic.
	 *
	 * ALL > TRACE > DEBUG > INFO > WARN > ERROR > FATAL > OFF  (ALL = 0xff;OFF = 0x00;
	 *
	 * @param {LogEntry} entry
	 * @return {boolean}
	 */
	log(entry) {
		validateInstance(entry, LogEntry);

		if (this.loglevel < entry.getLogLevel()) {
			return false;
		}

		return true;
	}

	/**
	 * set loglevel
	 *
	 * @param {integer} loglevel
	 * @return {Handler}
	 * @since 1.5.0
	 */
	setLogLevel(loglevel) {
		validateInteger(loglevel);
		this.loglevel = loglevel;
		return this;
	}

	/**
	 * get loglevel
	 *
	 * @return {integer}
	 * @since 1.5.0
	 */
	getLogLevel() {
		return this.loglevel;
	}

	/**
	 *  Set log level to All
	 *
	 * @return {Handler}
	 * @since 1.5.0
	 */
	setAll() {
		this.setLogLevel(ALL);
		return this;
	}

	/**
	 * Set log level to Trace
	 *
	 * @return {Handler}
	 * @since 1.5.0
	 */
	setTrace() {
		this.setLogLevel(TRACE);
		return this;
	}

	/**
	 * Set log level to Debug
	 *
	 * @return {Handler}
	 * @since 1.5.0
	 */
	setDebug() {
		this.setLogLevel(DEBUG);
		return this;
	}

	/**
	 * Set log level to Info
	 *
	 * @return {Handler}
	 * @since 1.5.0
	 */
	setInfo() {
		this.setLogLevel(INFO);
		return this;
	}

	/**
	 * Set log level to Warn
	 *
	 * @return {undefined}
	 * @since 1.5.0
	 */
	setWarn() {
		this.setLogLevel(WARN);
		return this;
	}

	/**
	 * Set log level to Error
	 *
	 * @return {Handler}
	 * @since 1.5.0
	 */
	setError() {
		this.setLogLevel(ERROR);
		return this;
	}

	/**
	 * Set log level to Fatal
	 *
	 * @return {Handler}
	 * @since 1.5.0
	 */
	setFatal() {
		this.setLogLevel(FATAL);
		return this;
	}

	/**
	 * Set log level to Off
	 *
	 * @return {Handler}
	 * @since 1.5.0
	 */
	setOff() {
		this.setLogLevel(OFF);
		return this;
	}
}
