/**
 * This class is the ball used in the Pong game
 * 
 * @author Krassy Boykinov 
 * @version 1.4
 * @since   1.0
 */

import greenfoot.*;

public class Ball extends Actor {
    private int angle;
    private Field field;
    private int[] score;

    public Ball(Field field) {
        this.field = field;
        angle = 180;
        setRotation(angle);
        score = new int[2];
        score[0] = 0;
        score[1] = 0;
    }
    
    /** 
     * The act() method is Greenfoot specific and is an extended function while
     * In this <code>act()</code> the ball movement will be calculated for every frame 
     * 
     * @see             move()
     * @since           1.0
     */
    public void act() {
        move();
    } 

    /** 
     * The move() method is calculating the direction of the ball and its movement
     * 
     * @see             Actor
     * @since           1.0
     */
    private void move() {
        if (getY() > 80 && getY() < 556) {
            if (field.left.getX() + 8 >= getX()) {
                if (field.left.getY() - 20 > getY() || field.left.getY() + 20 < getY()) {
                    scoreRight();
                }
                else {
                    if (field.left.getY() - 20 >= getY()) {
                        angle = 315;
                    }
                    else if (field.left.getY() + 20 <= getY()) {
                        angle = 45;
                    }
                    else {
                        int spin = 0;
                        if (field.left.getY() - 20 < getY()) {
                            spin = -20;
                            while ((field.left.getY() + spin) < getY()) {
                                spin += 1;
                            }
                        }
                        else if (field.left.getY() + 20 > getY()) {
                            spin = 20;
                            while ((field.left.getY() + spin) > getY()) {
                                spin -= 1;
                            }
                        }
                        angle = (180 - angle) + spin;
                    }
                }
            }
            if (field.right.getX() - 8 <= getX()) {
                if (field.right.getY() - 20 > getY() || field.right.getY() + 20 < getY()) {
                    scoreLeft();
                }
                else {
                    if (field.right.getY() - 20 >= getY()) {
                        angle = 225;
                    }
                    else if (field.right.getY() + 20 <= getY()) {
                        angle = 135;
                    }
                    else {
                        int spin = 0;
                        if (field.right.getY() - 20 < getY()) {
                            spin = 20;
                            while ((field.right.getY() - spin) < getY()) {
                                spin -= 1;
                            }
                        }
                        else if (field.right.getY() + 20 > getY()) {
                            spin = -20;
                            while ((field.right.getY() - spin) > getY()) {
                                spin += 1;
                            }
                        }
                        angle = (180 - angle) + spin;
                    }
                }
            }
        }
        else {
           angle = 360 - angle;
        }
        setRotation(angle);
        super.move(4);
    }
    
    /** 
     * The scoreRight() method will update game Score in future releases
     *
     * @since           1.0
     */
    private void scoreRight() {
        if (score[0] < 9) {
            score[0] += 1;
            field.rCount.setScore(score[0]);
            resetElements();
        }
        else {
            System.out.println("Right Wins");
            System.out.println("Press Reset to play again");
            Greenfoot.stop();
        }
    }
    
    /** 
     * The scoreLeft() method will update game Score in future releases
     * 
     * @since           1.0
     */
    private void scoreLeft() {
        if (score[1] < 9) {
            score[1] += 1;
            field.lCount.setScore(score[1]);
            resetElements();
        }
        else {
            System.out.println("Left Wins");
            System.out.println("Press Reset to play again");
            Greenfoot.stop();
    }
    }
    
    /** 
     * The resetElements() method will reset the game objects to start a new
     * round of gaming
     * 
     * @since           1.3
     */
    private void resetElements() {
        field.left.setLocation(125, 320);
        field.right.setLocation(675, 320);
        setLocation(402, 320);
        angle = 180;
        setRotation(angle);
        Greenfoot.stop();
    }
}
