/*

	Copyright (c) 2020, 2023, The Unified Company.

	This code is part of Unify.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the ESA Software Community License - Strong Copyleft LICENSE,
	as published by the ESA.
	See the ESA Software Community License - Strong Copyleft LICENSE, for more details.

	https://unifyjs.org 

*/


class consoleColors{

	colors				= new Array();

	backgroundColors	= new Array();


	addColor( colorName, colorCode ) {

		var color	= new Object();

		color.name	= colorName;

		color.code	= colorCode;

		this.colors.push( color );

	}

	addBackgroundColor( colorName, colorCode ) {

		var color	= new Object();

		color.name	= colorName;

		color.code	= colorCode;

		this.backgroundColors.push( color );

	}

	constructor() {

		this.createTextColors();

		this.createBackgroundColors();

		return this.createMethods();

	}

	createTextColors() {

		this.addColor( "white", 37 );

		this.addColor( "black", 30 );

		this.addColor( "blue", 34 );

		this.addColor( "cyan", 36 );

		this.addColor( "green", 32 );

		this.addColor( "magenta", 35 );

		this.addColor( "red", 31 );

		this.addColor( "yellow", 33 );

		this.addColor( "brightBlack", 90 );

		this.addColor( "brightRed", 91 );

		this.addColor( "brightGreen", 92 );

		this.addColor( "brightYellow", 93 );

		this.addColor( "brightBlue", 94 );

		this.addColor( "brightMagenta", 95 );

		this.addColor( "brightCyan", 96 );

		this.addColor( "brightWhite", 97 );

	}

	createBackgroundColors() {

		this.addBackgroundColor( "bgBlack", 40 );

		this.addBackgroundColor( "bgRed", 41 );

		this.addBackgroundColor( "bgGreen", 42 );

		this.addBackgroundColor( "bgYellow", 43 );

		this.addBackgroundColor( "bgBlue", 44 );

		this.addBackgroundColor( "bgMagenta", 45 );

		this.addBackgroundColor( "bgCyan", 46 );

		this.addBackgroundColor( "bgWhite", 47 );

		this.addBackgroundColor( "bgBrightBlack", 100 );

		this.addBackgroundColor( "bgBrightRed", 101 );

		this.addBackgroundColor( "bgBrightGreen", 102 );

		this.addBackgroundColor( "bgBrightYellow", 103 );

		this.addBackgroundColor( "bgBrightBlue", 104 );

		this.addBackgroundColor( "bgBrightMagenta", 105 );

		this.addBackgroundColor( "bgBrightCyan", 106 );

		this.addBackgroundColor( "bgBrightWhite", 107 );

	}

	createMethodsForTextColor( color, colorMethods ) {

		var name	= color.name;

		var code	= color.code;

		var open 	=   '\u001b[' + code + 'm';

		var close 	=   '\u001b[39m';

		colorMethods[ name ] = function ( value ) { 

			return open + value + close;

		}

	}

	createMethodsForBackgroundColor( color, colorMethods ) {

		var name	= color.name;

		var code	= color.code;

		var open 	= '\u001b[' + code + 'm';

		var close 	= '\u001b[49m';


		colorMethods[ name ] = function ( value ) { 

			return open + value + close;

		}

	}

	createMethods() {

		var colors				= this.colors;


		var colorMethods		= new Array();

		for ( var i = 0; i < colors.length; i++ ) {

			var textColor 			= colors[i];

			this.createMethodsForTextColor( textColor, colorMethods );

		}


		var backgroundColors	= this.backgroundColors;

		for ( var i = 0; i < backgroundColors.length; i++ ) {

			var backgroundColor 	= backgroundColors[i];

			this.createMethodsForBackgroundColor( backgroundColor, colorMethods );

		}

		return colorMethods;

	}

}

export default new consoleColors();