/*
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 */
package pacman;

import javafx.animation.Interpolator;
import javafx.animation.TranslateTransition;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.layout.Pane;
import javafx.util.Duration;
import static pacman.Pacman.*;


/**
 *
 * @author akouznet
 */
public class Creature extends Pane {
    private TranslateTransition move;
    private double speed;
    private double byX, byY;
    
    public Creature() {
        this(1);
    }
    
    public Creature(double speed) {
        this.speed = speed;
        
        InvalidationListener invalidationListener = new InvalidationListener() {

            @Override
            public void invalidated(Observable arg0) {
                if (move == null) {
                    return;
                }
                if (!getGame().getField().canMoveFurther(Creature.this, move)) {
                    wallHit();
                }
            }
        };
        
        translateXProperty().addListener(invalidationListener);
        translateYProperty().addListener(invalidationListener);  
    }

    private boolean needToContinue() {
        return byX != 0 || byY != 0;
    }

    private void startMove() {
        if (move != null) {
            move.stop();
            getGame().removeAnimation(move);
        }
        move = new TranslateTransition(
                Duration.seconds(1 / SPEED / Creature.this.speed), Creature.this);
        move.setByX(byX * CELL_WIDTH);
        move.setByY(byY * CELL_WIDTH);
        move.setInterpolator(Interpolator.LINEAR);
        move.setOnFinished(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                move = null;
                getGame().removeAnimation(move);
                stepFinished();
            }
        });
        move.play();
        getGame().addAnimation(move);
        movementStarted();
    }
            
    public void move(double byX, double byY) {
        if (getGame().isPaused()) {
            return;
        }
        
        this.byX = byX;
        this.byY = byY;
        
        if (move == null) {
            startMove();
        }
    }

    public void moveRight() {
        move(1, 0);
    }

    public void moveLeft() {
        move(-1, 0);
    }

    public void moveUp() {
        move(0, -1);
    }

    public void moveDown() {
        move(0, 1);
    }

    public void stop() {
        if (move != null) {
            move.stop();
            getGame().removeAnimation(move);
            byX = 0;
            byY = 0;
            move = null;
            stepFinished();
        }
        
    }
    
    public void wallHit() {
        stop();
    }
    
    public void stepFinished() {
        if (needToContinue()) {
            startMove();
        }
    }
    
    public void movementStarted() {
    }

    public TranslateTransition getMove() {
        return move;
    }
    
    public int getCellX() {
        return (int) ((getTranslateX() + MIKHALYCH_WIDTH) / CELL_WIDTH);
    }
    
    public int getCellY() {
        return (int) ((getTranslateY() + MIKHALYCH_WIDTH) / CELL_WIDTH);
    }
    
    public double getCellDX(int x) {
        return getTranslateX() - x * CELL_WIDTH;
    }
    
    public double getCellDY(int y) {
        return getTranslateY() - y * CELL_WIDTH;
    }
   
    public void setPosition(int x, int y) {
        setTranslateX(x * CELL_WIDTH);
        setTranslateY(y * CELL_WIDTH);
    }  
}
