            package electrosphere.renderer;

import electrosphere.main.Globals;
import electrosphere.renderer.anim.Animation;
import electrosphere.renderer.texture.Texture;
import org.joml.AxisAngle4f;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import org.joml.Vector4f;

/**
 *
 * @author amaterasu
 */
public class Actor {
    String modelPath;
    String textureOverride;
    String animation;
    double animationTime;
    boolean playingAnimation;
    float animationScalar = 1.0f;
    
    public Actor(String modelPath){
        playingAnimation = false;
        this.modelPath = modelPath;
    }
    
    public void incrementAnimationTime(double deltaTime){
        Model model = Globals.assetManager.fetchModel(modelPath);
        if(playingAnimation){
            animationTime = animationTime + deltaTime * animationScalar;
        }
        if(model != null){
            if(animation != null){
                model.playAnimation(animation);
                model.incrementTime(animationTime);
                if(model.currentAnimation == null){
                    playingAnimation = false;
                }
            }
        }
    }
    
    public double getAnimationTime(){
        return animationTime;
    }
    
    public String getCurrentAnimation(){
        return animation;
    }
    
    public void playAnimation(String animationName){
        animationTime = 0;
        playingAnimation = true;
        animation = animationName;
    }
    
    public boolean isPlayingAnimation(){
        return playingAnimation;
    }

    public void setAnimationScalar(float animationScalar) {
        this.animationScalar = animationScalar;
    }
    
    public void applyModelMatrix(Matrix4f modelMatrix){
        Model model = Globals.assetManager.fetchModel(modelPath);
        if(model != null){
            model.modelMatrix = modelMatrix;
        }
    }
    
    public void draw(){
        Model model = Globals.assetManager.fetchModel(modelPath);
        boolean hasDrawn = false;
        if(model != null){
            if(animation != null){
                model.playAnimation(animation);
                model.incrementTime(0.001);
                model.incrementTime(animationTime);
                if(model.currentAnimation == null){
                    playingAnimation = false;
                }
            }
            if(textureOverride != null){
                Texture overrideTextureObject = Globals.assetManager.fetchTexture(textureOverride);
                if(overrideTextureObject != null){
                    overrideTextureObject.bind();
                    hasDrawn = true;
                    model.draw(true, true, false, false, true, true, true);
                }
            }
            if(!hasDrawn){
                model.draw(true, true, false, true, true, true, true);
            }
        }
    }
    
    public void drawForDepthBuffer(){
        Model model = Globals.assetManager.fetchModel(modelPath);
        if(model != null){
            if(animation != null){
                model.playAnimation(animation);
                model.incrementTime(animationTime);
                if(model.currentAnimation == null){
                    playingAnimation = false;
                }
            }
            model.drawForDepthBuffer();
        }
    }
    
    public void drawUI(){
        Model model = Globals.assetManager.fetchModel(modelPath);
        if(model != null){
            model.drawUI();
        }
    }
    
    public String getModelPath(){
        return modelPath;
    }
    
    public Vector3f getBonePosition(String boneName){
        Vector3f rVal = new Vector3f();
        Model model = Globals.assetManager.fetchModel(modelPath);
        if(model != null){
            if(animation != null){
                model.playAnimation(animation);
                model.incrementTime(animationTime);
                model.update_node_transform(model.root_anim_node);
                Bone currentBone = model.boneMap.get(boneName);
                if(currentBone != null){
                    Vector4f result = currentBone.final_transform.transform(new Matrix4f(currentBone.inverseBindPoseMatrix).invert().transform(new Vector4f(rVal.x,rVal.y,rVal.z,1)));
//                    currentBone.inverseBindPoseMatrix
                    rVal.x = result.x;
                    rVal.y = result.y;
                    rVal.z = result.z;
                }
            }
        }
        return rVal;
    }
    
    public Quaternionf getBoneRotation(String boneName){
        Quaternionf rVal = new Quaternionf();
        Model model = Globals.assetManager.fetchModel(modelPath);
        if(model != null){
            if(animation != null){
                model.playAnimation(animation);
                model.incrementTime(animationTime);
                Bone currentBone = model.boneMap.get(boneName);
                if(currentBone != null){
                    AxisAngle4f axisAngle = new AxisAngle4f();
                    currentBone.final_transform.getRotation(axisAngle);
                    Quaternionf rotation = new Quaternionf(axisAngle);
                    rVal.set(rotation);
                }
            }
        }
        return rVal;
    }
    
    public Matrix4f getBoneTransform(String boneName){
        Matrix4f rVal = new Matrix4f();
        Model model = Globals.assetManager.fetchModel(modelPath);
        if(model != null){
            if(animation != null){
                model.playAnimation(animation);
                model.incrementTime(animationTime);
                model.update_node_transform(model.root_anim_node);
                Bone currentBone = model.boneMap.get(boneName);
                if(currentBone != null){
                    rVal = currentBone.final_transform;
//                    currentBone.inverseBindPoseMatrix
                }
            }
        }
        return rVal;
    }
    
    public void setTextureOverride(String override){
        textureOverride = override;
    }
    
}
