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

package model;

/**
 *
 * @author eccastro
 */

import com.jme.scene.Node;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import com.jme.scene.state.MaterialState;

import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jme.image.Texture;
import com.jme.math.Quaternion;
import com.jme.renderer.ColorRGBA;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.OutputCapsule;
import com.jme.util.export.Savable;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.Constants;

public class PerishableFood extends Food {

    private Timer timer;

    public PerishableFood() { //Savable matters only!
    }

    public PerishableFood(int periodInSecs, double x, double y, Environment ev, MaterialState ms, TextureState ts){
        super(x,y,ev,ms);
        try {
            this.category = Constants.categoryPFOOD;
            this.subCategory = Constants.categoryPFOOD;
            this.ts = ts;
            this.validPeriod = periodInSecs;
            sf = new ThingShapeFactory("images/apple.3ds", this);
            shape = sf.getNode(0.03f);

            shape.setRenderState(ms);
            shape.setRenderState(ev.ls);
            shape.updateRenderState();
            perishable = true;
            stillValid = true;
            myLifeCycle();
        } catch (IOException ex) {
            System.out.println("!!!!!PerishableFood: Erro ! ");
            ex.printStackTrace();
        }
    }

    
    @Override
    void myLifeCycle() {
        timer = new Timer();
        timer.schedule(new RemindTask(this, "images/green_metalic.jpg"), validPeriod*1000);
    }

    public void setSpoiledTexture(String pathToTexture){

         ts.setTexture(TextureManager.loadTexture(PerishableFood.class.getClassLoader().getResource(
                pathToTexture),
                Texture.MinificationFilter.Trilinear,
                Texture.MagnificationFilter.Bilinear));
         ts.setEnabled(true);
         shape.setRenderState(ts);
         //maybe has to be called from external class (due to enhiritance):
         material.setColor(ColorRGBA.lightGray);//for client display only

    }

   public Node myLocalTransformations(Node modelw){
        //modelw.setLocalScale(0.03f);
        Quaternion quat90 = new Quaternion();
        quat90.fromAngles(270 * 3.141592f/180, 3.141592f, 0f);
        modelw.setLocalRotation(quat90);
        //modelw.setLocalTranslation(0,-1f,0);
        modelw.setLocalTranslation(0,0.8f,0);
        return modelw;
    }
   @Override
 public void setID(Long id, Environment e){
      this.ID = id;
      String name = new String("PFood_");
      this.shape.setName(name.concat(id.toString()));
      myName  = name.concat(id.toString());
      //System.out.println("====  My name is "+this.shape.getName());
      e.oMap.put(myName, this);
  }

    @Override
    public Knapsack putMeInKnapsack(Knapsack sack) {
        sack.incPerishableFoodInKnapsack(this);
        return sack;
    }



    class RemindTask extends TimerTask implements Savable{
        PerishableFood pf;
        String pathToTexture;
        RemindTask(PerishableFood pf, String pathToTexture){
          this.pf = pf;
          this.pathToTexture = pathToTexture;
        }
        public void run() {
            System.out.println("***** Food expired! *****");
            this.pf.setSpoiledTexture(pathToTexture);
            //material.setTaste(0.0); maybe
            material.setEnergy(0.0);
            stillValid = false;
            timer.cancel(); //Terminate the timer thread
        }

        public void write(JMEExporter jmee) throws IOException {
            OutputCapsule capsule = jmee.getCapsule(this);
             capsule.write(pf, "pf", null);
             capsule.write(pathToTexture, "pathToTexture", null);

        }

        public void read(JMEImporter jmei) throws IOException {
            InputCapsule ic = jmei.getCapsule(this);
            pf = (PerishableFood) ic.readSavable("pf", null);
            pathToTexture = ic.readString("pathToTexture", null);

        }

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

}
