/**
 * Creates a Mesh Lambert Material, which can be extended with GL nodes.
 *
 * @remarks
 * This node can create children, which will be GL nodes. The GLSL code generated by the nodes will extend the Material.
 *
 */
import {NodeParamsConfig} from '../utils/params/ParamsConfig';
import {
	UniformsTransparencyParamConfig,
	UniformsTransparencyController,
	UniformsTransparencyControllers,
} from './utils/UniformsTransparencyController';
import {
	AdvancedCommonController,
	AdvancedCommonControllers,
	AdvancedCommonParamConfig,
} from './utils/AdvancedCommonController';
import {MapParamConfig, TextureMapController, TextureMapControllers} from './utils/TextureMapController';
import {
	AlphaMapParamConfig,
	TextureAlphaMapController,
	TextureAlphaMapControllers,
} from './utils/TextureAlphaMapController';
import {BaseBuilderParamConfig, TypedBuilderMatNode} from './_BaseBuilder';
import {ShaderAssemblerLambert} from '../gl/code/assemblers/materials/Lambert';
import {AssemblerName} from '../../poly/registers/assemblers/_BaseRegister';
import {Poly} from '../../Poly';
import {FogParamConfig, UniformFogController, UniformFogControllers} from './utils/UniformsFogController';
import {
	WireframeShaderMaterialController,
	WireframeShaderMaterialControllers,
	WireframeShaderMaterialParamsConfig,
} from './utils/WireframeShaderMaterialController';
import {TextureAOMapController, AOMapParamConfig, TextureAOMapControllers} from './utils/TextureAOMapController';
import {
	TextureEnvMapSimpleController,
	EnvMapSimpleParamConfig,
	TextureEnvMapSimpleControllers,
} from './utils/TextureEnvMapSimpleController';
import {
	TextureLightMapController,
	LightMapParamConfig,
	TextureLightMapControllers,
} from './utils/TextureLightMapController';
import {
	TextureEmissiveMapController,
	EmissiveMapParamConfig,
	TextureEmissiveMapControllers,
} from './utils/TextureEmissiveMapController';
import {PCSSController, PCSSParamConfig, PCSSControllers} from './utils/PCSSController';
import {DefaultFolderParamConfig} from './utils/DefaultFolder';
import {TexturesFolderParamConfig} from './utils/TexturesFolder';
import {AdvancedFolderParamConfig} from './utils/AdvancedFolder';
import {Material} from 'three';
import {MeshLambertMaterial} from 'three';
import {CustomMaterialName, IUniforms} from '../../../core/geometry/Material';
import {MatType} from '../../poly/registers/nodes/types/Mat';
import {
	CustomMaterialMeshParamConfig,
	materialMeshAssemblerCustomMaterialRequested,
} from './utils/customMaterials/CustomMaterialMesh';
interface MeshLambertBuilderMaterial extends MeshLambertMaterial {
	vertexShader: string;
	fragmentShader: string;
	uniforms: IUniforms;
	customMaterials: {
		[key in CustomMaterialName]?: Material;
	};
}
interface MeshLambertBuilderControllers
	extends AdvancedCommonControllers,
		PCSSControllers,
		TextureAlphaMapControllers,
		TextureAOMapControllers,
		TextureEmissiveMapControllers,
		TextureEnvMapSimpleControllers,
		TextureLightMapControllers,
		TextureMapControllers,
		UniformFogControllers,
		UniformsTransparencyControllers,
		WireframeShaderMaterialControllers {}
class MeshLambertBuilderMatParamsConfig extends CustomMaterialMeshParamConfig(
	PCSSParamConfig(
		FogParamConfig(
			WireframeShaderMaterialParamsConfig(
				AdvancedCommonParamConfig(
					BaseBuilderParamConfig(
						/* advanced */
						AdvancedFolderParamConfig(
							LightMapParamConfig(
								EnvMapSimpleParamConfig(
									EmissiveMapParamConfig(
										AOMapParamConfig(
											AlphaMapParamConfig(
												MapParamConfig(
													/* textures */
													TexturesFolderParamConfig(
														UniformsTransparencyParamConfig(
															DefaultFolderParamConfig(NodeParamsConfig)
														)
													)
												)
											)
										)
									)
								)
							)
						)
					)
				)
			)
		)
	)
) {}
const ParamsConfig = new MeshLambertBuilderMatParamsConfig();

export class MeshLambertBuilderMatNode extends TypedBuilderMatNode<
	MeshLambertBuilderMaterial,
	ShaderAssemblerLambert,
	MeshLambertBuilderMatParamsConfig
> {
	override paramsConfig = ParamsConfig;
	static override type() {
		return MatType.MESH_LAMBERT_BUILDER;
	}
	public override usedAssembler(): Readonly<AssemblerName.GL_MESH_LAMBERT> {
		return AssemblerName.GL_MESH_LAMBERT;
	}
	protected _createAssemblerController() {
		return Poly.assemblersRegister.assembler(this, this.usedAssembler());
	}
	public override customMaterialRequested(customName: CustomMaterialName): boolean {
		return materialMeshAssemblerCustomMaterialRequested(this, customName);
	}

	readonly controllers: MeshLambertBuilderControllers = {
		advancedCommon: new AdvancedCommonController(this),
		alphaMap: new TextureAlphaMapController(this),
		aoMap: new TextureAOMapController(this),
		emissiveMap: new TextureEmissiveMapController(this),
		envMap: new TextureEnvMapSimpleController(this),
		lightMap: new TextureLightMapController(this),
		map: new TextureMapController(this),
		PCSS: new PCSSController(this),
		uniformFog: new UniformFogController(this),
		uniformTransparency: new UniformsTransparencyController(this),
		wireframeShader: new WireframeShaderMaterialController(this),
	};
	protected override controllersList = Object.values(this.controllers);

	override async cook() {
		this._material = this._material || this.createMaterial();
		await Promise.all(this.controllersPromises(this._material));

		this.compileIfRequired(this._material);

		this.setMaterial(this._material);
	}
}
