/**
 * Function of SDF Rhombus
 *
 * @remarks
 *
 * based on [https://iquilezles.org/articles/distfunctions/](https://iquilezles.org/articles/distfunctions/)
 */

import {BaseSDFGlNode} from './_BaseSDF';
import {ThreeToGl} from '../../../../src/core/ThreeToGl';
import GL_CODE from './gl/raymarching/sdfRhombus.glsl';
import {NodeParamsConfig, ParamConfig} from '../utils/params/ParamsConfig';
import {GlConnectionPointType, GlConnectionPoint} from '../utils/io/connections/Gl';
import {ShadersCollectionController} from './code/utils/ShadersCollectionController';
import {FunctionGLDefinition} from './utils/GLDefinition';
import {GlType} from '../../poly/registers/nodes/types/Gl';

const OUTPUT_NAME = 'float';
class SDFRhombusGlParamsConfig extends NodeParamsConfig {
	position = ParamConfig.VECTOR3([0, 0, 0], {hidden: true});
	center = ParamConfig.VECTOR3([0, 0, 0]);
	length1 = ParamConfig.FLOAT(1);
	length2 = ParamConfig.FLOAT(0.5);
	height = ParamConfig.FLOAT(0.2);
	radius = ParamConfig.FLOAT(0.05);
}
const ParamsConfig = new SDFRhombusGlParamsConfig();
export class SDFRhombusGlNode extends BaseSDFGlNode<SDFRhombusGlParamsConfig> {
	override paramsConfig = ParamsConfig;
	static override type() {
		return GlType.SDF_RHOMBUS;
	}

	override initializeNode() {
		super.initializeNode();

		this.io.outputs.setNamedOutputConnectionPoints([
			new GlConnectionPoint(OUTPUT_NAME, GlConnectionPointType.FLOAT),
		]);
	}

	override setLines(shadersCollectionController: ShadersCollectionController) {
		const position = this.position();
		const center = ThreeToGl.vector3(this.variableForInputParam(this.p.center));
		const length1 = ThreeToGl.float(this.variableForInputParam(this.p.length1));
		const length2 = ThreeToGl.float(this.variableForInputParam(this.p.length2));
		const height = ThreeToGl.float(this.variableForInputParam(this.p.height));
		const radius = ThreeToGl.float(this.variableForInputParam(this.p.radius));

		const float = this.glVarName(OUTPUT_NAME);
		const bodyLine = `float ${float} = sdRhombus(${position} - ${center}, ${length1}, ${length2}, ${height}, ${radius})`;
		shadersCollectionController.addBodyLines(this, [bodyLine]);

		this._addSDFMethods(shadersCollectionController);
		shadersCollectionController.addDefinitions(this, [new FunctionGLDefinition(this, GL_CODE)]);
	}
}
