/home/caleb/ASDV-Java/Semester 3/Assignments/MP2-chapter4_CalebFontenot/src/MP2_chapter4_CalebFontenot/BouncingBall.java
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package MP2_chapter4_CalebFontenot;

import java.io.File;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;

/**
 *
 * @author caleb
 */
public class BouncingBall extends Pane {

    GridPane textPane = new GridPane();
    private long timeSinceLastCollisionEvent = 0;
    final double RACKET_LENGTH = 100;
    final double radius = 20;
    private double x = radius, y = radius;
    private double dx = 1, dy = 1;
    private Circle circle = new Circle(x, y, radius);
    private Rectangle racket = new Rectangle(RACKET_LENGTH, 20);
    private Label infoLabel = new Label("Your mouse cursor must be inside the bounds of the window to play!");
    private Label racketTime = new Label();
    private Label ballCords = new Label();
    private Label playerScoreLabel = new Label("Player Score: 0");
    private Label computerScoreLabel = new Label("Computer Score: 0");



    private Timeline animation;

    private int computerScore, playerScore = 0;

    public BouncingBall() {
        circle.setFill(Color.RED); // Set ball color
        racket.setFill(Color.BLUE);
        textPane.add(racketTime, 0, 0);
        textPane.add(ballCords, 0, 1);
        textPane.add(playerScoreLabel, 0, 2);
        textPane.add(computerScoreLabel, 0, 3);
        getChildren().addAll(circle, racket, textPane, infoLabel); // Place a ball into this pane
        racket.relocate(0, 580);
        infoLabel.relocate(getHeight() / 4.0, getWidth() / 2.0);
        // Create an animation for moving the ball
        animation = new Timeline(new KeyFrame(Duration.millis(1), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                timeSinceLastCollisionEvent++;
                racketTime.setText("Frames since last collision: " + timeSinceLastCollisionEvent);
                moveBall();
                if (y >= (getHeight() - 20) && (timeSinceLastCollisionEvent > 500)) {
                    timeSinceLastCollisionEvent = 0;
                    incrementComputerScore();
                }
            }
        })
        );
        animation.setRate(animation.getRate() * .5);
        animation.setCycleCount(Timeline.INDEFINITE);

    }

    private boolean processRacketCollision() {
        boolean racketCollision = racket.getBoundsInParent().intersects(circle.getBoundsInParent());

        if (racketCollision && (timeSinceLastCollisionEvent > 500)) { // This is second condition is a cooldown. It prevents odd behavior happening with the ball and the racket if the racket hits the ball at certain angles.
            System.out.println("Racket collision detected!");
            timeSinceLastCollisionEvent = 0;
            incrementPlayerScore();
            return true;
        } else {
            return false;
        }
    }

    private void moveBall() {

        // Check boundaries
        if (x < radius || x > getWidth() - radius) {
            dx *= -1; // Change ball move direction
        }
        if (y < radius || y > getHeight() - radius || processRacketCollision()) {
            dy *= -1; // Change ball move direction
        }

        // Adjust ball position by 1 or -1
        x += dx;
        y += dy;
        circle.setCenterX(x);
        circle.setCenterY(y);
        ballCoordsToLabel();
    }

    public void play() {
        animation.play();
    }

    public void pause() {
        animation.pause();
    }

    public void increaseSpeed() {
        animation.setRate(animation.getRate() * 1.5);
        System.out.println(animation.getRate());
    }

    public void decreaseSpeed() {
        animation.setRate(animation.getRate() * 1.5 > 0 ? animation.getRate() / 1.5 : 0);
        System.out.println(animation.getRate());
    }

    public DoubleProperty rateProperty() {
        return animation.rateProperty();
    }

    public void moveRacket(double x) {
        racket.relocate(x, 580);
    }

    public void showInfoLabel() {
        double paneHeight = getHeight();
        double paneWidth = getWidth();
        getChildren().add(infoLabel);
        // Center Text
        infoLabel.relocate(paneWidth / 4.0, paneHeight / 2.0);
    }

    public void hideInfoLabel() {
        getChildren().remove(infoLabel);
    }

    private void incrementPlayerScore() {
        playerScoreLabel.setText("Player score: " + ++playerScore);
        playSound();
    }

    private void incrementComputerScore() {
        computerScoreLabel.setText("Computer score: " + ++computerScore);
        //playSound();
    }

    private void ballCoordsToLabel() {
        ballCords.setText("Ball coords: " + x + ", " + y);
    }
    private void playSound() {
        int randInt = (int) (Math.random() * 2);
        String sample;
        if (randInt == 0) {
            sample = "5";
        } else  {
            sample = "7";
        }
        Media sound = new Media(new File("sound/Sample_000"+sample+".wav").toURI().toString());
        MediaPlayer mediaPlayer = new MediaPlayer(sound);
        mediaPlayer.setStartTime(Duration.ZERO);
        mediaPlayer.play();
        //mediaPlayer.setOnEndOfMedia();
    }
}