/*
 * 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.*;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.scene.media.AudioClip;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.transform.Rotate;
import javafx.util.Duration;
import static pacman.Pacman.*;


/**
 *
 * @author akouznet
 */
public class Mikhalych extends Creature {

    private Mikhalych2 mikhalych2;
    private int coinsAte;
    private int balance;
    private Rotate rotate = new Rotate(0, CELL_WIDTH / 2, CELL_WIDTH / 2);

    private void gameOver() {
        MediaPlayer mediaPlayer = new MediaPlayer(new Media(
                Mikhalych.class.getResource("sounds/gameOver.mp3")
                .toExternalForm()));
        mediaPlayer.setAutoPlay(true);
        getGame().gameOver();
    }

    public enum State { ALIVE, DYING, DEAD };
    private State state = State.ALIVE;
    private static AudioClip[] coinAte = {
        new AudioClip(
            Mikhalych.class.getResource("sounds/coinAte1.wav").toExternalForm()),
        new AudioClip(
            Mikhalych.class.getResource("sounds/coinAte2.wav").toExternalForm()),
    };
    private static AudioClip dyingAudio = new AudioClip(
            Mikhalych.class.getResource("sounds/dyingM.wav").toExternalForm());
    
    private static AudioClip palevoAudio = new AudioClip(
            Mikhalych.class.getResource("sounds/palevo.wav").toExternalForm());
    
    private static AudioClip catchedAudio = new AudioClip(
            Mikhalych.class.getResource("sounds/catch.wav").toExternalForm());

    public Mikhalych() {
        mikhalych2 = new Mikhalych2();

        getChildren().add(mikhalych2);
        
        InvalidationListener invalidationListener = new InvalidationListener() {

            @Override
            public void invalidated(Observable arg0) {
                getGame().getField().tryToEat(Mikhalych.this);
            }
        };
        
        translateXProperty().addListener(invalidationListener);
        translateYProperty().addListener(invalidationListener);  
        
        getTransforms().setAll(rotate);
        
        for (AudioClip clip : coinAte) {
            clip.setVolume(0.3);
        }
        
        setPosition(
                getGame().getField().getLevel().michalych_position[0], 
                getGame().getField().getLevel().michalych_position[1]);        
    }

    @Override
    public void stop() {
        super.stop();
        mikhalych2.closeMouth();
    }

    @Override
    public void movementStarted() {
        super.movementStarted();
        state = State.ALIVE;
        mikhalych2.openMouth();
        if (getMove().getByX() > 0) {
            rotateTo(0);
        } else if (getMove().getByX() < 0) {
            rotateTo(180);
        } else if (getMove().getByY() > 0) {
            rotateTo(90);
        } else if (getMove().getByY() < 0) {
            rotateTo(-90);
        }
    }

    void eatCoin(Coin coin) {
        coinsAte++;
        balance++;
        getGame().updateBalance(balance);
        coinAte[(int) (Math.random() * coinAte.length)].play();
//        coinAte[coinAteIndex].play();
//        coinAteIndex = (coinAteIndex + 1) % coinAte.length;
    }

    private void rotateTo(double angle) {
        double deltaAngle = angle - rotate.getAngle();
        while (deltaAngle > 180) {
            deltaAngle -= 360;
        }
        while (deltaAngle < -180) {
            deltaAngle += 360;
        }
        Timeline rotation = new Timeline(
            new KeyFrame(Duration.seconds(0.2), 
                new KeyValue(rotate.angleProperty(), rotate.getAngle() + deltaAngle, Interpolator.EASE_BOTH))
        );
        rotation.play();
    }

    public void die(Enemy enemy) {
        if (state == State.ALIVE) {
            balance -= enemy.getRequestAmount();
            getGame().updateBalance(balance);
            if (balance < 0) {
                gameOver();
            }
            state = State.DYING;
            stop();
            mikhalych2.die();
            dyingAudio.play();
        }
    }

    public void dead() {
        state = State.DEAD;
        
        int x = getCellX();
        int y = getCellY();
        
        setPosition(x, y);
    }
    
    @Override
    public void move(double byX, double byY) {
        if (state != State.DYING) {
            super.move(byX, byY);
        }
    }

    public State getState() {
        return state;
    }
    
    void eatPalevo(Palevo palevo) {
        this.palevoAudio.play();
        getGame().frightenEnemies();
        mikhalych2.setFill(Color.RED);
    }
    
    void catchEnemy(Enemy enemy) {
        balance += enemy.getRequestAmount();
        getGame().updateBalance(balance);
        catchedAudio.play();
        enemy.die();
    }
    
    void calm() {
        getGame().calmEnemies();
        mikhalych2.setFill(Color.ORANGE);
    }

}
