/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package electrosphere.renderer;

import electrosphere.main.Globals;
import electrosphere.renderer.texture.Texture;
import org.lwjgl.PointerBuffer;
import org.lwjgl.assimp.AIMaterial;
import org.lwjgl.assimp.AIMaterialProperty;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glBindTexture;
import static org.lwjgl.opengl.GL13.GL_TEXTURE0;
import static org.lwjgl.opengl.GL13.glActiveTexture;

/**
 *
 * @author amaterasu
 */
public class Material {
    
    String diffuse;
    String specular;
    boolean hasTransparency = false;
    
    boolean usesFetch = true;
    
    int texturePointer;
    
    public Material(){
        
    }
    //basically useless because blender doesn't support exporting mats with fbx
    public static Material load_material_from_aimaterial(AIMaterial input){
        Material rVal = new Material();
        if(input.mNumProperties() > 0){
            PointerBuffer property_buffer = input.mProperties();
            while(property_buffer.hasRemaining()){
                AIMaterialProperty new_property = AIMaterialProperty.create(property_buffer.get());
//                System.out.println("Property: " + new_property.mKey().dataString());
            }
        }
        return rVal;
    }
    public String get_diffuse(){
        return diffuse;
    }
    public String get_specular(){
        return specular;
    }
    public void set_diffuse(String t){
        diffuse = t;
//        if(t.isTransparent()){
//            hasTransparency = true;
//        }
    }
    public void set_specular(String t){
        specular = t;
//        if(t.isTransparent()){
//            hasTransparency = true;
//        }
    }
    
    public void setTexturePointer(int pointer){
        texturePointer = pointer;
        usesFetch = false;
    }
    
    public void apply_material(){
        //basically a switch for the case where we want to manually set texture pointer
        if(usesFetch){
            Texture diffuseTexture = Globals.assetManager.fetchTexture(diffuse);
            if(diffuseTexture != null){
                diffuseTexture.bind(0);
            }
            Texture specularTexture = Globals.assetManager.fetchTexture(specular);
            if(specularTexture != null){
                specularTexture.bind(1);
            }
        } else {
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, texturePointer);
        }
    }
    public void apply_material(int diffuse_channel, int specular_channel){
        Texture diffuseTexture = Globals.assetManager.fetchTexture(diffuse);
        if(diffuseTexture != null){
            diffuseTexture.bind(diffuse_channel);
        }
        Texture specularTexture = Globals.assetManager.fetchTexture(specular);
        if(specularTexture != null){
            specularTexture.bind(specular_channel);
        }
    }
    
    public boolean isTransparent(){
        boolean rVal = false;
        Texture diffuseTexture = Globals.assetManager.fetchTexture(diffuse);
        if(diffuseTexture != null && diffuseTexture.isTransparent()){
            rVal = true;
        }
        Texture specularTexture = Globals.assetManager.fetchTexture(specular);
        if(specularTexture != null && specularTexture.isTransparent()){
            rVal = true;
        }
        return rVal;
    }
}
