package gy2balls;

import java.awt.Dimension;
import java.util.Random;

class Ball implements Runnable {

    Dimension space;
    int x, y;

    enum Direction {

        up, down, left, right, steady
    };
    Direction direction;

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    Ball(Dimension space) {
        this.space = space;
        Random rnd = new Random();
        this.x = rnd.nextInt(space.width);
        this.y = rnd.nextInt(space.height);
        direction = Direction.steady;
    }

    public void run() {
        while (true) {
            switch (direction) {
                case up:
                    y--;
                    break;
                case down:
                    y++;
                    break;
                case left:
                    x--;
                    break;
                case right:
                    x++;
                    break;
            }
            if (y == -1) {
                y = 0;
                direction = Direction.down;
            } else if (y == space.height + 1) {
                y = space.height;
                direction = Direction.up;
            } else if (x == -1) {
                x = 0;
                direction = Direction.right;
            } else if (x == space.width) {
                x = space.width;
                direction = Direction.left;
            }
            try {
                Thread.sleep(Main.sleepInterval);
            } catch (InterruptedException iex) {
            }
            //System.out.println(x+" "+y);
        }
    }

    public void goUp() {
        if (direction == Direction.down) {
            direction = Direction.steady;
        } else {
            direction = Direction.up;
        }
    }

    public void goDown() {
        if (direction == Direction.up) {
            direction = Direction.steady;
        } else {
            direction = Direction.down;
        }
    }

    public void goLeft() {
        if (direction == Direction.right) {
            direction = Direction.steady;
        } else {
            direction = Direction.left;
        }
    }

    public void goRight() {
        if (direction == Direction.left) {
            direction = Direction.steady;
        } else {
            direction = Direction.right;
        }
    }
}
