/**
 * sends a trigger on every frame
 *
 *
 */

import {TRIGGER_CONNECTION_NAME, TypedJsNode} from './_Base';
import {NodeParamsConfig} from '../utils/params/ParamsConfig';
import {JsConnectionPoint, JsConnectionPointType} from '../utils/io/connections/Js';
import {JsType} from '../../poly/registers/nodes/types/Js';
import {JsLinesCollectionController} from './code/utils/JsLinesCollectionController';
import {ComputedValueJsDefinition} from './utils/JsDefinition';
import {Poly} from '../../Poly';

export enum OnTickJsNodeOuput {
	TIME = 'time',
	DELTA = 'delta',
}

class OnTickJsParamsConfig extends NodeParamsConfig {}
const ParamsConfig = new OnTickJsParamsConfig();

export class OnTickJsNode extends TypedJsNode<OnTickJsParamsConfig> {
	override readonly paramsConfig = ParamsConfig;
	static override type() {
		return JsType.ON_TICK;
	}
	override isTriggering() {
		return true;
	}
	override initializeNode() {
		this.io.outputs.setNamedOutputConnectionPoints([
			new JsConnectionPoint(TRIGGER_CONNECTION_NAME, JsConnectionPointType.TRIGGER),
			new JsConnectionPoint(OnTickJsNodeOuput.TIME, JsConnectionPointType.FLOAT),
			new JsConnectionPoint(OnTickJsNodeOuput.DELTA, JsConnectionPointType.FLOAT),
		]);
	}

	override setTriggeringLines(linesController: JsLinesCollectionController, triggeredMethods: string): void {
		linesController.addTriggeringLines(this, [triggeredMethods], {gatherable: true});
	}
	override setLines(linesController: JsLinesCollectionController) {
		const timeVarName = this.jsVarName(OnTickJsNodeOuput.TIME);
		const deltaVarName = this.jsVarName(OnTickJsNodeOuput.DELTA);

		const _time = Poly.namedFunctionsRegister.getFunction('globalsTime', this, linesController);
		const _delta = Poly.namedFunctionsRegister.getFunction('globalsTimeDelta', this, linesController);
		linesController.addDefinitions(this, [
			new ComputedValueJsDefinition(
				this,
				linesController,
				JsConnectionPointType.FLOAT,
				timeVarName,
				_time.asString()
			),
		]);
		linesController.addDefinitions(this, [
			new ComputedValueJsDefinition(
				this,
				linesController,
				JsConnectionPointType.FLOAT,
				deltaVarName,
				_delta.asString()
			),
		]);
	}
}
