/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

/**
 *
 * @author eccastro
 */

import com.jme.renderer.ColorRGBA;
import com.jme.scene.state.MaterialState;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;
import com.jme.util.export.Savable;
import java.io.IOException;
import util.Constants;

public class Material3D implements Savable{


    /**
     * No shininess.
     */
    private static final float NO_SHININESS = 0.0f;

    /**
     * Low shininess.
     */
    private static final float LOW_SHININESS = 5.0f;

    /**
     * High shininess.
     */
    private static final float HIGH_SHININESS = 100.0f;

    private double hardness;

    private double energy;

    private double taste = 1; //temporarily

    private ColorRGBA color = ColorRGBA.blue;

    public MaterialState materialState;
    //this variables stores the original properties (hardness and color) of
    // a hidden obstacle, since whenever it is hidden it's hardness is set to 0.0
    private double previousHardness;
    private ColorRGBA previousColor;

    public Material3D(){ //Savable matters only!

    }

    public Material3D(double hardness, double energy, MaterialState materialState){
        this.hardness = hardness;
        this.energy = energy;
        this.taste = 0;
        this.materialState = materialState;
        this.color = ColorRGBA.yellow;
    }

    public Material3D(ColorRGBA color, double hardness, double energy, MaterialState materialState) {
        this.hardness = hardness;

        this.energy = energy;

        this.color = color;
        this.materialState = materialState;
        
        this.materialState.setAmbient(ColorRGBA.black);
        this.materialState.setDiffuse(color);
        this.materialState.setSpecular(ColorRGBA.black);
        this.materialState.setShininess(0.0f);
        //this.materialState.setShininess(NO_SHININESS);
        this.materialState.setEmissive(ColorRGBA.black);
        this.materialState.setEnabled(true);

    }

    public Material3D(ColorRGBA color, double hardness, double energy, double taste, MaterialState materialState) {
        this(color, hardness, energy, materialState);
        this.taste = taste;
    }
    
    public Material3D(ColorRGBA color, MaterialState materialState) {
        this.hardness = 1.0;
        this.energy = 0.0;

        this.color = color;
        
        this.materialState = materialState;
        if (this.materialState != null) {
        this.materialState.setAmbient(ColorRGBA.black);
        this.materialState.setDiffuse(color);
        this.materialState.setSpecular(ColorRGBA.black);
        this.materialState.setShininess(0.0f);
        //this.materialState.setShininess(NO_SHININESS);
        this.materialState.setEmissive(ColorRGBA.black);
        this.materialState.setEnabled(true);
        }
    }
    //for creature only
    public Material3D(MaterialState materialState) {
        this.hardness = 1.0;
        this.energy = 0.0;

       
        this.materialState = materialState;
         if (this.materialState != null) {
        this.materialState.setShininess(100.0f);
             //this.materialState.setShininess(HIGH_SHININESS);
        this.materialState.setEnabled(true);
        }
    }

    public void setEnergy(double value){
        this.energy = value;
    }

    public void makeItNotHard(){
        this.hardness = 0.0;
    }

    public double getEnergy()   { return energy; }
    public double getHardness() { return hardness; }
    public ColorRGBA getColor(){
        return this.color;
    }

    public void setTaste(double value){
        this.taste = value;
    }
    public double getTaste()    { return taste; }

    public void setHiddenObstacleMaterial() {
        previousHardness = hardness;
        previousColor = color;
        hardness = 0.0;
        color = ColorRGBA.gray;
    }

    public void undoHiddenObstacleMaterial() {
        hardness = previousHardness;
        color = previousColor;
    }

    public void setColor(ColorRGBA c) {
        this.color = c;
        if (this.materialState != null) {
            this.materialState.setAmbient(ColorRGBA.black);
            this.materialState.setDiffuse(color);
            this.materialState.setSpecular(ColorRGBA.black);
            this.materialState.setShininess(0.0f);
        //this.materialState.setShininess(NO_SHININESS);
            this.materialState.setEmissive(ColorRGBA.black);
            this.materialState.setEnabled(true);
        }
    }
     public String getColorName() {
        String st = new String("None");
        if(this.color.equals(ColorRGBA.red) )st = Constants.colorRED;
        else if(this.color.equals(ColorRGBA.green) )st = Constants.colorGREEN;
        else if(this.color.equals(ColorRGBA.blue) )st = Constants.colorBLUE;
        else if(this.color.equals(ColorRGBA.yellow) )st = Constants.colorYELLOW;
        else if(this.color.equals(ColorRGBA.magenta) )st = Constants.colorMAGENTA;
        else if(this.color.equals(ColorRGBA.white) )st = Constants.colorWHITE;
        else if(this.color.equals(ColorRGBA.darkGray) )st = Constants.colorDARKGRAY;
        else if(this.color.equals(ColorRGBA.orange) )st = Constants.colorORANGE;

        return st;
    }

    public int getColorTypeIndex() {

        if(this.color.equals(ColorRGBA.red) )return Constants.getColorIndex(Constants.colorRED);
        else if(this.color.equals(ColorRGBA.green) )return Constants.getColorIndex(Constants.colorGREEN);
        else if(this.color.equals(ColorRGBA.blue) )return Constants.getColorIndex(Constants.colorBLUE);
        else if(this.color.equals(ColorRGBA.yellow) )return Constants.getColorIndex(Constants.colorYELLOW);
        else if(this.color.equals(ColorRGBA.magenta) )return Constants.getColorIndex(Constants.colorMAGENTA);
        else if(this.color.equals(ColorRGBA.white) )return Constants.getColorIndex(Constants.colorWHITE);

        return -1; //invalid
    }
    public void write(JMEExporter jmee) throws IOException {
        OutputCapsule capsule = jmee.getCapsule(this);
        capsule.write(hardness, "hardness", 1.0);
        capsule.write(energy, "energy", 0);
        capsule.write(taste, "taste", 1.0);
        capsule.write(color, "color", ColorRGBA.blue);
        capsule.write(materialState, "materialState", null);
        capsule.write(previousHardness, "previousHardness", 0);
        capsule.write(previousColor, "previousColor", ColorRGBA.blue);

    }

    public void read(JMEImporter jmei) throws IOException {
        InputCapsule ic = jmei.getCapsule(this);
        hardness = ic.readDouble("hardness", 1.0);
        energy = ic.readDouble("energy", 0);
        taste = ic.readDouble("taste", 1.0);
        color = (ColorRGBA) ic.readSavable("color", ColorRGBA.blue);
        materialState = (MaterialState) ic.readSavable("materialState", null);
        previousHardness = ic.readDouble("previousHardness", 0);
        previousColor = (ColorRGBA) ic.readSavable("previousColor", ColorRGBA.blue);

    }

    public Class getClassTag() {
        return this.getClass();
    }

    
}
