#include "script_common.h"

typedef QCodeEdit::ACEntry ACE;

const QString href_root = "https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects";


QString ScriptCommon::includeACName() {
	return "include(\"file\")";
}


QList<QCodeEdit::ACEntry> ScriptCommon::acKeywords  () {
	return QList<QCodeEdit::ACEntry>()
		<< ACE("", "var"       ).addHelpHRef(QUrl("https://learn.javascript.ru/variables#peremennaya"))
		<< ACE("", "let"       ).addHelpHRef(QUrl("https://learn.javascript.ru/variables#peremennaya"))
		<< ACE("", "const"     ).addHelpHRef(QUrl("https://learn.javascript.ru/variables#konstanty"))
		<< ACE("", "function"  ).addHelpHRef(QUrl("https://learn.javascript.ru/function-basics"))
		<< ACE("", "return"    )
		<< ACE("", "break"     ).addHelpHRef(QUrl("https://learn.javascript.ru/while-for#preryvanie-tsikla-break"))
		<< ACE("", "continue"  ).addHelpHRef(QUrl("https://learn.javascript.ru/while-for#continue"))
		<< ACE("", "case"      ).addHelpHRef(QUrl("https://learn.javascript.ru/switch"))
		<< ACE("", "true"      )
		<< ACE("", "false"     )
		<< ACE("", "arguments" )
		<< ACE("", "length"    )
		<< ACE("", "instanceof").addHelpHRef(QUrl("https://learn.javascript.ru/instanceof"))
		<< ACE("", "prototype" )
		<< ACE("", "with"      )
		<< ACE("", "try"       )
		<< ACE("", "undefined" ).addHelpHRef(QUrl("https://learn.javascript.ru/types#znachenie-undefined"))
		<< ACE("", "null"      ).addHelpHRef(QUrl("https://learn.javascript.ru/types#znachenie-null"))
		<< ACE("", "default"   )
		<< ACE("", "class"     )
		<< ACE("", "finally"   )
		<< ACE("", "new"       )
		<< ACE("", "delete"    )
		<< ACE("", "extends"   )
		<< ACE("", "this"      ).addHelpHRef(QUrl("https://learn.javascript.ru/object-methods"))
		<< ACE("", "while ( )" ).addHelpHRef(QUrl("https://learn.javascript.ru/while-for#tsikl-while"))
		<< ACE("", "for ( )"   ).addHelpHRef(QUrl("https://learn.javascript.ru/while-for#tsikl-for"))
		<< ACE("", "switch (v)").addHelpHRef(QUrl("https://learn.javascript.ru/switch"))
		<< ACE("", "until ( )" )
	;
}


QList<QCodeEdit::ACEntry> ScriptCommon::acFunctions () {
	ACE include("function", includeACName(), tr("Include another script file"));
	include.declaration_pos = 0;
	return QList<QCodeEdit::ACEntry>()
		<< include
		<< ACE("function", "server_started()"              ).addHint(tr("Executed at server start"))
		<< ACE("function", "print(v)"                      ).addHint(tr("Print message to console"))
		<< ACE("function", "connect(func)"                 ).addHint(tr("Connect Qt signal to function"))
		<< ACE("function", "disconnect(func)"              )
		<< ACE("function", "call(func)"                    )
		<< ACE("function", "usleep(us)"                    ).addHint(tr("Sleep microseconds"))
		<< ACE("function", "msleep(ms)"                    ).addHint(tr("Sleep milliseconds"))
		<< ACE("function", "sleep(s)"                      ).addHint(tr("Sleep seconds"))
		<< ACE("function", "callee(v)"                     )
		<< ACE("function", "log(msg, type = SHServer.Info)").addHint(tr("Add message to SHS_Server log"))
		<< ACE("function", "message(msg, type = 0)"        ).addHint(tr("Send message to SHS_Server clients"))
		<< ACE("function", "historyAll(var)"               )
		<< ACE("function", "eval(str)"                     ).addHelpHRef(QUrl(href_root + "/eval"))
		<< ACE("function", "historyLast(var, date)"        )
		<< ACE("function", "lastDate(years, months = 0, days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0)"  )
		<< ACE("function", "dateToSecs(years, months = 0, days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0)")
	;
}


QList<QCodeEdit::ACEntry> ScriptCommon::acObjects   () {
	return QList<QCodeEdit::ACEntry>()
		<< ACE("type", "Error"    ).addHelpHRef(QUrl(href_root + "/Error"))
		<< ACE("type", "TypeError").addHelpHRef(QUrl(href_root + "/TypeError"))
		<< ACE("type", "Number"   ).addHelpHRef(QUrl(href_root + "/Number"))
		<< ACE("type", "String"   ).addHelpHRef(QUrl(href_root + "/String"))
		<< ACE("type", "Object"   ).addHelpHRef(QUrl(href_root + "/Object"))
		<< ACE("type", "Array"    ).addHelpHRef(QUrl(href_root + "/Array"))
		<< ACE("type", "RegExp"   ).addHelpHRef(QUrl(href_root + "/RegExp"))
		<< ACE("type", "Date"     ).addHelpHRef(QUrl(href_root + "/Date"))
		<< ACE("type", "Boolean"  ).addHelpHRef(QUrl(href_root + "/Boolean"))
		<< ACE("type", "Map"      ).addHelpHRef(QUrl(href_root + "/Map"))
		<< ACE("type", "Set"      ).addHelpHRef(QUrl(href_root + "/Set"))
	;
}


QList<QCodeEdit::ACEntry> ScriptCommon::acNamespaces() {
	return QList<QCodeEdit::ACEntry>()
		<< ACE("", "Qt"      ).addHint(tr("Qt enums"))
		<< ACE("", "Math"    ).addHint(tr("Mathematic functions"))
		<< ACE("", "SHS"     )
		<< ACE("", "SHServer").addHint(tr("SHS enums"))
		<< ACE("", "console" ).addHint(tr("Console functions"))
		<< ACE("", "JSON"    ).addHint(tr("JSON serialization"))
	;
}


QList<QCodeEdit::ACEntry> ScriptCommon::acMathConst () {
	return QList<QCodeEdit::ACEntry>()
		<< ACE("Number", "E"      ).addHelpHRef(QUrl(href_root + "/Math/E"))
		<< ACE("Number", "LN10"   ).addHelpHRef(QUrl(href_root + "/Math/LN10"))
		<< ACE("Number", "LN2"    ).addHelpHRef(QUrl(href_root + "/Math/LN2"))
		<< ACE("Number", "LOG2E"  ).addHelpHRef(QUrl(href_root + "/Math/LOG2E"))
		<< ACE("Number", "LOG10E" ).addHelpHRef(QUrl(href_root + "/Math/LOG10E"))
		<< ACE("Number", "PI"     ).addHelpHRef(QUrl(href_root + "/Math/PI"))
		<< ACE("Number", "SQRT1_2").addHelpHRef(QUrl(href_root + "/Math/SQRT1_2"))
		<< ACE("Number", "SQRT2"  ).addHelpHRef(QUrl(href_root + "/Math/SQRT2"))
	;
}


QList<QCodeEdit::ACEntry> ScriptCommon::acMathFunc  () {
	return QList<QCodeEdit::ACEntry>()
		<< ACE("function", "abs(x)"          ).addHelpHRef(QUrl(href_root + "/Math/abs"))
		<< ACE("function", "acos(x)"         ).addHelpHRef(QUrl(href_root + "/Math/acos"))
		<< ACE("function", "asin(x)"         ).addHelpHRef(QUrl(href_root + "/Math/asin"))
		<< ACE("function", "atan(x)"         ).addHelpHRef(QUrl(href_root + "/Math/atan"))
		<< ACE("function", "atan2(y, x)"     ).addHelpHRef(QUrl(href_root + "/Math/atan2"))
		<< ACE("function", "ceil(x)"         ).addHelpHRef(QUrl(href_root + "/Math/ceil"))
		<< ACE("function", "cos(x)"          ).addHelpHRef(QUrl(href_root + "/Math/cos"))
		<< ACE("function", "exp(x)"          ).addHelpHRef(QUrl(href_root + "/Math/exp"))
		<< ACE("function", "floor(x)"        ).addHelpHRef(QUrl(href_root + "/Math/floor"))
		<< ACE("function", "log(x)"          ).addHelpHRef(QUrl(href_root + "/Math/log"))
		<< ACE("function", "max(v0, v1, ...)").addHelpHRef(QUrl(href_root + "/Math/max"))
		<< ACE("function", "min(v0, v1, ...)").addHelpHRef(QUrl(href_root + "/Math/min"))
		<< ACE("function", "pow(x, y)"       ).addHelpHRef(QUrl(href_root + "/Math/pow"))
		<< ACE("function", "random()"        ).addHelpHRef(QUrl(href_root + "/Math/random"))
		<< ACE("function", "round(x)"        ).addHelpHRef(QUrl(href_root + "/Math/round"))
		<< ACE("function", "sin(x)"          ).addHelpHRef(QUrl(href_root + "/Math/sin"))
		<< ACE("function", "sqrt(x)"         ).addHelpHRef(QUrl(href_root + "/Math/sqrt"))
		<< ACE("function", "tan(x)"          ).addHelpHRef(QUrl(href_root + "/Math/tan"))
		   ;
}


QList<QCodeEdit::ACEntry> ScriptCommon::acNumberConst() {
	return QList<QCodeEdit::ACEntry>()
		<< ACE("Number", "EPSILON"			).addHelpHRef(QUrl(href_root + "/Number/EPSILON"))
		<< ACE("Number", "MAX_SAFE_INTEGER"	).addHelpHRef(QUrl(href_root + "/Number/MAX_SAFE_INTEGER"))
		<< ACE("Number", "MAX_VALUE"		).addHelpHRef(QUrl(href_root + "/Number/MAX_VALUE"))
		<< ACE("Number", "MIN_SAFE_INTEGER"	).addHelpHRef(QUrl(href_root + "/Number/MIN_SAFE_INTEGER"))
		<< ACE("Number", "MIN_VALUE"		).addHelpHRef(QUrl(href_root + "/Number/MIN_VALUE"))
		<< ACE("Number", "NaN"				).addHelpHRef(QUrl(href_root + "/Number/NaN"))
		<< ACE("Number", "NEGATIVE_INFINITY").addHelpHRef(QUrl(href_root + "/Number/NEGATIVE_INFINITY"))
		<< ACE("Number", "POSITIVE_INFINITY").addHelpHRef(QUrl(href_root + "/Number/POSITIVE_INFINITY"))
	;
}


QList<QCodeEdit::ACEntry> ScriptCommon::acNumberFunc () {
	return QList<QCodeEdit::ACEntry>()
		<< ACE("Boolean", "isFinite(testValue)"		).addHelpHRef(QUrl(href_root + "/Number/isFinite"))
		<< ACE("Boolean", "isInteger(testValue)"	).addHelpHRef(QUrl(href_root + "/Number/isInteger"))
		<< ACE("Boolean", "isNaN(testValue)"		).addHelpHRef(QUrl(href_root + "/Number/isNaN"))
		<< ACE("Boolean", "isSafeInteger(testValue)").addHelpHRef(QUrl(href_root + "/Number/isSafeInteger"))
		<< ACE("Number", "parseFloat(string)"		).addHelpHRef(QUrl(href_root + "/Number/parseFloat"))
		<< ACE("Number", "parseInt(string)"			).addHelpHRef(QUrl(href_root + "/Number/parseInt"))
	;
}


QList<ScriptCommon::ACSection> ScriptCommon::aclString() {
	QList<ACSection> ret;
	ACSection ac;

	ac.first = Property; ac.second.clear();
	ac.second
		<< ACE("Number", "length"          ).addHelpHRef(QUrl(href_root + "/String/length"))
		   ;
	ret << ac;

	ac.first = Function; ac.second.clear();
	ac.second
		<< ACE("String", "charAt(pos)"							  ).addHelpHRef(QUrl(href_root + "/String/charAt"))
		<< ACE("Number", "charCodeAt(pos)"						  ).addHelpHRef(QUrl(href_root + "/String/charCodeAt"))
		<< ACE("Number", "codePointAt(pos)"						  ).addHelpHRef(QUrl(href_root + "/String/codePointAt"))
		<< ACE("String", "concat([string1 [, string2 [, ...]]])"  ).addHelpHRef(QUrl(href_root + "/String/concat"))
		<< ACE("Boolean", "endsWith(searchString [, endPosition])").addHelpHRef(QUrl(href_root + "/String/endsWith"))
		<< ACE("Boolean", "includes(searchString [, position])"	  ).addHelpHRef(QUrl(href_root + "/String/includes"))
		<< ACE("Number", "indexOf(searchString, position)"		  ).addHelpHRef(QUrl(href_root + "/String/indexOf"))
		<< ACE("Number", "lastIndexOf(searchString, position)"	  ).addHelpHRef(QUrl(href_root + "/String/lastIndexOf"))
		<< ACE("Number", "localeCompare(that)"					  ).addHelpHRef(QUrl(href_root + "/String/localeCompare"))
		<< ACE("Array", "match(regexp)"							  ).addHelpHRef(QUrl(href_root + "/String/match"))
		<< ACE("String", "normalize()"							  ).addHelpHRef(QUrl(href_root + "/String/normalize"))
		<< ACE("String", "padEnd(length [, string])"			  ).addHelpHRef(QUrl(href_root + "/String/padEnd"))
		<< ACE("String", "padStart(length [, string])"			  ).addHelpHRef(QUrl(href_root + "/String/padStart"))
		<< ACE("String", "repeat(count)"						  ).addHelpHRef(QUrl(href_root + "/String/repeat"))
		<< ACE("String", "replace(searchValue, replaceValue)"	  ).addHelpHRef(QUrl(href_root + "/String/replace"))
		<< ACE("Number", "search(regexp)"						  ).addHelpHRef(QUrl(href_root + "/String/search"))
		<< ACE("String", "slice(start, end)"					  ).addHelpHRef(QUrl(href_root + "/String/length"))
		<< ACE("Array", "split(separator, limit)"				  ).addHelpHRef(QUrl(href_root + "/String/slice"))
		<< ACE("String", "startsWith(searchString [, position])"  ).addHelpHRef(QUrl(href_root + "/String/startsWith"))
		<< ACE("String", "substr(start, length)"				  ).addHelpHRef(QUrl(href_root + "/String/substr"))
		<< ACE("String", "substring(start, end)"				  ).addHelpHRef(QUrl(href_root + "/String/substring"))
		<< ACE("String", "toLowerCase()"						  ).addHelpHRef(QUrl(href_root + "/String/toLowerCase"))
		<< ACE("String", "toLocaleLowerCase()"					  ).addHelpHRef(QUrl(href_root + "/String/toLocaleLowerCase"))
		<< ACE("String", "toUpperCase()"						  ).addHelpHRef(QUrl(href_root + "/String/toUpperCase"))
		<< ACE("String", "toLocaleUpperCase()"					  ).addHelpHRef(QUrl(href_root + "/String/toLocaleUpperCase"))
		<< ACE("String", "trim()"								  ).addHelpHRef(QUrl(href_root + "/String/trim"))
		   ;
	ret << ac;

	return ret;
}


QList<ScriptCommon::ACSection> ScriptCommon::aclArray() {
	QList<ACSection> ret;
	ACSection ac;

	ac.first = Property; ac.second.clear();
	ac.second
		<< ACE("Number", "length"          ).addHelpHRef(QUrl(href_root + "/Array/length"))
		   ;
	ret << ac;

	ac.first = Function; ac.second.clear();
	ac.second
		<< ACE("String", "toString()"										   ).addHelpHRef(QUrl(href_root + "/Array/toString"))
		<< ACE("Array", "concat([item1 [, item2 [, ...]]])"					   ).addHelpHRef(QUrl(href_root + "/Array/concat"))
		<< ACE("Array", "copyWithin([item1 [, item2 [, ...]]])"				   ).addHelpHRef(QUrl(href_root + "/Array/copyWithin"))
		<< ACE("Number", "entries()"										   ).addHelpHRef(QUrl(href_root + "/Array/entries"))
		<< ACE("Array", "fill(item [, index1 [, index2]])"					   ).addHelpHRef(QUrl(href_root + "/Array/fill"))
		<< ACE("String", "join(separator)"									   ).addHelpHRef(QUrl(href_root + "/Array/join"))
		<< ACE("???", "find(callbackfn [, thisArg])"						   ).addHelpHRef(QUrl(href_root + "/Array/find"))
		<< ACE("Number", "findIndex(callbackfn [, thisArg])"				   ).addHelpHRef(QUrl(href_root + "/Array/findIndex"))
		<< ACE("Boolean", "includes(item)"									   ).addHelpHRef(QUrl(href_root + "/Array/includes"))
		<< ACE("Iterator", "keys()"											   ).addHelpHRef(QUrl(href_root + "/Array/keys"))
		<< ACE("???", "pop()"												   ).addHelpHRef(QUrl(href_root + "/Array/pop"))
		<< ACE("???", "push([item1 [, item2 [, ...]]])"						   ).addHelpHRef(QUrl(href_root + "/Array/push"))
		<< ACE("Array", "reverse()"											   ).addHelpHRef(QUrl(href_root + "/Array/reverse"))
		<< ACE("???", "shift()"												   ).addHelpHRef(QUrl(href_root + "/Array/shift"))
		<< ACE("Array", "slice(start, end)"									   ).addHelpHRef(QUrl(href_root + "/Array/slice"))
		<< ACE("Array", "sort(comparefn)"									   ).addHelpHRef(QUrl(href_root + "/Array/sort"))
		<< ACE("Array", "splice(start, deleteCount[, item1 [, item2 [, ...]]])").addHelpHRef(QUrl(href_root + "/Array/splice"))
		<< ACE("Number", "unshift([item1 [, item2 [, ...]]])"				   ).addHelpHRef(QUrl(href_root + "/Array/unshift"))
		<< ACE("Number", "indexOf(searchElement [, fromIndex])"				   ).addHelpHRef(QUrl(href_root + "/Array/indexOf"))
		<< ACE("Number", "lastIndexOf(searchElement [, fromIndex])"			   ).addHelpHRef(QUrl(href_root + "/Array/lastIndexOf"))
		<< ACE("Boolean", "every(callbackfn [, thisArg])"					   ).addHelpHRef(QUrl(href_root + "/Array/every"))
		<< ACE("Boolean", "some(callbackfn [, thisArg])"					   ).addHelpHRef(QUrl(href_root + "/Array/some"))
		<< ACE("undefined", "forEach(callbackfn [, thisArg])"				   ).addHelpHRef(QUrl(href_root + "/Array/forEach"))
		<< ACE("Array", "map(callbackfn [, thisArg])"						   ).addHelpHRef(QUrl(href_root + "/Array/map"))
		<< ACE("Array", "filter(callbackfn [, thisArg])"					   ).addHelpHRef(QUrl(href_root + "/Array/filter"))
		<< ACE("???", "reduce(callbackfn [, initialValue])"					   ).addHelpHRef(QUrl(href_root + "/Array/reduce"))
		<< ACE("???", "reduceRight(callbackfn [, initialValue])"			   ).addHelpHRef(QUrl(href_root + "/Array/reduceRight"))
		<< ACE("Iterator", "values()"										   ).addHelpHRef(QUrl(href_root + "/Array/values"))
		   ;
	ret << ac;

	return ret;
}


QList<ScriptCommon::ACSection> ScriptCommon::aclBoolean() {
	QList<ACSection> ret;
	ACSection ac;

	ac.first = Function; ac.second.clear();
	ac.second
		<< ACE("String", "toString()").addHelpHRef(QUrl(href_root + "/Boolean/toString"))
		   ;
	ret << ac;

	return ret;
}


QList<ScriptCommon::ACSection> ScriptCommon::aclNumber() {
	QList<ACSection> ret;
	ACSection ac;

	ac.first = Function; ac.second.clear();
	ac.second
		<< ACE("String", "toString(radix)"				).addHelpHRef(QUrl(href_root + "/Number/toString"))
		<< ACE("String", "toLocaleString()"				).addHelpHRef(QUrl(href_root + "/Number/toLocaleString"))
		<< ACE("String", "toFixed(fractionDigits)"		).addHelpHRef(QUrl(href_root + "/Number/toFixed"))
		<< ACE("String", "toExponential(fractionDigits)").addHelpHRef(QUrl(href_root + "/Number/toExponential"))
		<< ACE("String", "toPrecision(precision)"		).addHelpHRef(QUrl(href_root + "/Number/toPrecision"))
		   ;
	ret << ac;

	return ret;
}

