//Vertex Shader
#version 430 core




layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec4 aWeights;
layout (location = 3) in vec4 aIndex;
layout (location = 4) in vec2 aText;



uniform mat4 transform;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;


const int MAX_BONES = 100;
uniform mat4 bones[MAX_BONES];
uniform int hasBones;
uniform int numBones;

uniform int hasTexture;



out vec3 Normal;
out vec3 FragPos;
out vec2 TexCoords;




void main()
{
    vec4 FinalVertex = vec4(aPos, 1.0);
    vec4 FinalNormal = vec4(aNormal, 0.0);
    if(hasBones == 1){
        //mat4 BoneTransform;
        //for(int i = 0; i < MAX_BONES; i++){
        //    if(i < numBones){
        //        BoneTransform += bones[i] * aWeights[i];
        //    }
        //}
        //vec4 FinalVertex = vec4(aPos, 1.0);
        //vec4 FinalNormal = vec4(aNormal, 1.0);
        //mat4 BoneTransform;
        //BoneTransform = mat4(
        //    1.0,0.0,0.0,0.0,
        //    0.0,1.0,0.0,0.0,
        //    0.0,0.0,1.0,0.0,
        //    0.0,0.0,0.0,1.0);
        //BoneTransform = BoneTransform * mat4(
        //    1.0,0.0,0.0,0.0,
        //    0.0,1.0,0.0,0.0,
        //    0.0,0.0,2.0,0.0,
        //    0.0,0.0,0.0,1.0);
        //BoneTransform = BoneTransform * bones[aIndex[0]] * aWeights[0];
        //BoneTransform += bones[aIndex[0]] * 1.0;
        //BoneTransform += bones[aIndex[1]] * aWeights[1];
        //BoneTransform += bones[aIndex[2]] * aWeights[2];
        //BoneTransform += bones[aIndex[3]] * aWeights[3];
        //FinalVertex = BoneTransform * FinalVertex;
        vec4 inputVertex = FinalVertex;
        vec4 inputNormal = FinalNormal;
        //mat4 BoneTransform;
        //BoneTransform = bones[int(aIndex[0])] * aWeights[0];
        //BoneTransform += bones[int(aIndex[1])] * aWeights[1];
        //BoneTransform += bones[int(aIndex[2])] * aWeights[2];
        //BoneTransform += bones[int(aIndex[3])] * aWeights[3];
        //FinalVertex = BoneTransform * inputVertex;
        //FinalNormal = BoneTransform * inputNormal;
        FinalVertex = (bones[int(aIndex[0])]  * inputVertex) * aWeights[0];
        FinalVertex = (bones[int(aIndex[1])]  * inputVertex) * aWeights[1] + FinalVertex;
        FinalVertex = (bones[int(aIndex[2])]  * inputVertex) * aWeights[2] + FinalVertex;
        FinalVertex = (bones[int(aIndex[3])]  * inputVertex) * aWeights[3] + FinalVertex;
        FinalNormal = (bones[int(aIndex[0])]  * inputNormal) * aWeights[0];
        FinalNormal = (bones[int(aIndex[1])]  * inputNormal) * aWeights[1] + FinalNormal;
        FinalNormal = (bones[int(aIndex[2])]  * inputNormal) * aWeights[2] + FinalNormal;
        FinalNormal = (bones[int(aIndex[3])]  * inputNormal) * aWeights[3] + FinalNormal;
    } else {
        //gl_Position = projection * view * model * vec4(aPos, 1.0);
    }

    vec2 FinalTexture = vec2(1.0,1.0);
    if(hasTexture == 1){
        FinalTexture = aText;
    }
    TexCoords = FinalTexture;

    FragPos = vec3(model * FinalVertex);
    Normal = vec3(transpose(inverse(model)) * FinalNormal);
    gl_Position = projection * view * model * FinalVertex;
}