import {initShaderProgram,
	unlinkShaderProgram} from './shaders';

/**
 *
 * @param {any} gl the gl context
 * @param {any} shaderProgram the shader program
 * @param {any} fragmentShader the current fragment shader
 * @param {string} fsSource new fragment shader source
 * @param {string} vsSource current vsSource
 * @return {any} the program info object
 */
export function changeFragmentShader(gl: any,
	shaderProgram: any,
	fragmentShader: any,
	fsSource: string,
	vsSource: string) {
	[shaderProgram, fragmentShader] = unlinkShaderProgram(gl,
		fragmentShader,
		shaderProgram);
	[shaderProgram, fragmentShader] = initShaderProgram(gl,
		vsSource,
		fsSource)!;
	const programInfo: any = {
		program: shaderProgram,
		attribLocations: {
			vertexPosition: gl.getAttribLocation(shaderProgram,
				'aVertexPosition'),
			vertexColor: gl.getAttribLocation(shaderProgram,
				'aVertexColor'),
			vertexNormal: gl.getAttribLocation(shaderProgram,
				'aVertexNormal'),
			textureCoord: gl.getAttribLocation(shaderProgram,
				'aTextureCoord'),
		},
		uniformLocations: {
			projectionMatrix: gl.getUniformLocation(
				shaderProgram, 'uProjectionMatrix'),
			viewMatrix: gl.getUniformLocation(
				shaderProgram, 'uviewMatrix'),
			modelMatrix: gl.getUniformLocation(
				shaderProgram, 'umodelMatrix'),
			normalModelMatrix: gl.getUniformLocation(
				shaderProgram, 'unormalModelMatrix'),
			uSampler: gl.getUniformLocation(
				shaderProgram, 'uSampler'),
		},
	};
	return [programInfo, fragmentShader];
}
