import { Ball } from '../../entities/ball/ball';
import { Paddle } from '../../entities/paddle/paddle';
import { processReflection } from './processReflection';

describe('processReflection', () => {
  it('Корректно обрабатывает неверное значения аргумента paddle', () => {
    const ball = new Ball(0, 0, 1, 1, 0);
    const sectorAmount = 1;
    const minAngle = 1;
    const maxAngle = 179;
    const speed = 1;

    const ballX = ball.x;
    const ballY = ball.y;
    const verticalSpeed = ball.verticalSpeed;
    const horizontalSpeed = ball.horizontalSpeed;

    const result = processReflection(null, ball, sectorAmount, minAngle, maxAngle, speed);

    expect(result).toBeNull();
    expect(ball.x).toEqual(ballX);
    expect(ball.y).toEqual(ballY);
    expect(ball.verticalSpeed).toEqual(verticalSpeed);
    expect(ball.horizontalSpeed).toEqual(horizontalSpeed);
  });

  it('Корректно обрабатывает неверное значения аргумента ball', () => {
    const paddle = new Paddle(0, 0, 2, 1);
    const sectorAmount = 1;
    const minAngle = 1;
    const maxAngle = 179;
    const speed = 1;

    const result = processReflection(paddle, null, sectorAmount, minAngle, maxAngle, speed);

    expect(result).toBeNull();
  });

  it('Корректно обрабатывает неверное значения аргумента paddleSectorAmount', () => {
    const paddle = new Paddle(0, 0, 2, 1);
    const ball = new Ball(0, 0, 1, 1, 0);
    const minAngle = 1;
    const maxAngle = 179;
    const speed = 1;

    const ballX = ball.x;
    const ballY = ball.y;
    const verticalSpeed = ball.verticalSpeed;
    const horizontalSpeed = ball.horizontalSpeed;

    const result = processReflection(paddle, ball, null, minAngle, maxAngle, speed);

    expect(result).toBeNull();
    expect(ball.x).toEqual(ballX);
    expect(ball.y).toEqual(ballY);
    expect(ball.verticalSpeed).toEqual(verticalSpeed);
    expect(ball.horizontalSpeed).toEqual(horizontalSpeed);

    expect(processReflection(paddle, ball, 0, minAngle, maxAngle, speed)).toBeNull();
    expect(processReflection(paddle, ball, -1, minAngle, maxAngle, speed)).toBeNull();
    expect(processReflection(paddle, ball, 1.5, minAngle, maxAngle, speed)).toBeNull();
  });

  it('Корректно обрабатывает неверное значения аргумента minAngle', () => {
    const paddle = new Paddle(0, 0, 2, 1);
    const ball = new Ball(0, 0, 1, 1, 0);
    const sectorAmount = 1;
    const maxAngle = 178;
    const speed = 1;

    const ballX = ball.x;
    const ballY = ball.y;
    const verticalSpeed = ball.verticalSpeed;
    const horizontalSpeed = ball.horizontalSpeed;

    const result = processReflection(paddle, ball, sectorAmount, null, maxAngle, speed);

    expect(result).toBeNull();
    expect(ball.x).toEqual(ballX);
    expect(ball.y).toEqual(ballY);
    expect(ball.verticalSpeed).toEqual(verticalSpeed);
    expect(ball.horizontalSpeed).toEqual(horizontalSpeed);

    expect(processReflection(paddle, ball, sectorAmount, -1, maxAngle, speed)).toBeNull();
    expect(processReflection(paddle, ball, sectorAmount, 360, maxAngle, speed)).toBeNull();

    const minAngleBiggerThanMaxAngle = 179;
    expect(processReflection(paddle, ball, sectorAmount, minAngleBiggerThanMaxAngle, maxAngle, speed)).toBeNull();
  });

  it('Корректно обрабатывает неверное значения аргумента maxAngle', () => {
    const paddle = new Paddle(0, 0, 2, 1);
    const ball = new Ball(0, 0, 1, 1, 0);
    const sectorAmount = 1;
    const minAngle = 1;
    const speed = 1;

    const ballX = ball.x;
    const ballY = ball.y;
    const verticalSpeed = ball.verticalSpeed;
    const horizontalSpeed = ball.horizontalSpeed;

    const result = processReflection(paddle, ball, sectorAmount, minAngle, null, speed);

    expect(result).toBeNull();
    expect(ball.x).toEqual(ballX);
    expect(ball.y).toEqual(ballY);
    expect(ball.verticalSpeed).toEqual(verticalSpeed);
    expect(ball.horizontalSpeed).toEqual(horizontalSpeed);

    expect(processReflection(paddle, ball, sectorAmount, minAngle, -1, speed)).toBeNull();
    expect(processReflection(paddle, ball, sectorAmount, minAngle, 360, speed)).toBeNull();
  });

  it('Корректно обрабатывает неверное значения аргумента speed', () => {
    const paddle = new Paddle(0, 0, 2, 1);
    const ball = new Ball(0, 0, 1, 1, 0);
    const sectorAmount = 1;
    const minAngle = 1;
    const maxAngle = 179;

    const ballX = ball.x;
    const ballY = ball.y;
    const verticalSpeed = ball.verticalSpeed;
    const horizontalSpeed = ball.horizontalSpeed;

    const result = processReflection(paddle, ball, sectorAmount, minAngle, maxAngle, null);

    expect(result).toBeNull();
    expect(ball.x).toEqual(ballX);
    expect(ball.y).toEqual(ballY);
    expect(ball.verticalSpeed).toEqual(verticalSpeed);
    expect(ball.horizontalSpeed).toEqual(horizontalSpeed);

    expect(processReflection(paddle, ball, sectorAmount, minAngle, maxAngle, -1)).toBeNull();
  });

  it('Корректно обрабатывает неверное значения аргумента speed', () => {
    const paddle = new Paddle(0, 0, 2, 1);
    const ball = new Ball(0, 0, 1, 1, 0);
    const sectorAmount = 1;
    const minAngle = 1;
    const maxAngle = 179;

    const ballX = ball.x;
    const ballY = ball.y;
    const verticalSpeed = ball.verticalSpeed;
    const horizontalSpeed = ball.horizontalSpeed;

    const result = processReflection(paddle, ball, sectorAmount, minAngle, maxAngle, null);

    expect(result).toBeNull();
    expect(ball.x).toEqual(ballX);
    expect(ball.y).toEqual(ballY);
    expect(ball.verticalSpeed).toEqual(verticalSpeed);
    expect(ball.horizontalSpeed).toEqual(horizontalSpeed);

    expect(processReflection(paddle, ball, sectorAmount, minAngle, maxAngle, -1)).toBeNull();
  });

  it('Ничего не изменяет при отсутствии столкновения ракетки и мяча', () => {
    const paddle = new Paddle(0, 0, 2, 1);
    const ball = new Ball(10, 10, 1, 1, 0);
    const sectorAmount = 1;
    const minAngle = 1;
    const maxAngle = 179;
    const speed = 1;

    const ballX = ball.x;
    const ballY = ball.y;
    const verticalSpeed = ball.verticalSpeed;
    const horizontalSpeed = ball.horizontalSpeed;

    const result = processReflection(paddle, ball, sectorAmount, minAngle, maxAngle, speed);

    expect(ball.x).toEqual(ballX);
    expect(ball.y).toEqual(ballY);
    expect(ball.verticalSpeed).toEqual(verticalSpeed);
    expect(ball.horizontalSpeed).toEqual(horizontalSpeed);
  });

  it('Отражает мяч влево при попадании в левый сектор ракетки', () => {
    const paddle = new Paddle(0, 100, 90, 10);
    const ball = new Ball(5, 99, 1, 5, 0); // левый край, летит вниз
    const sectorAmount = 3;
    const minAngle = 30;
    const maxAngle = 150;
    const speed = 10;

    processReflection(paddle, ball, sectorAmount, minAngle, maxAngle, speed);

    expect(ball.horizontalSpeed).toBeLessThan(0);
    expect(ball.verticalSpeed).toBeLessThan(0);
  });

  it('Отражает мяч вправо при попадании в правый сектор ракетки', () => {
    const paddle = new Paddle(0, 100, 90, 10);
    const ball = new Ball(85, 99, 1, 5, 0);
    const sectorAmount = 3;
    const minAngle = 30;
    const maxAngle = 150;
    const speed = 10;

    processReflection(paddle, ball, sectorAmount, minAngle, maxAngle, speed);

    expect(ball.horizontalSpeed).toBeGreaterThan(0);
    expect(ball.verticalSpeed).toBeLessThan(0);
  });

  it('Отражает мяч вертикально вверх при попадании в центральный сектор', () => {
    const paddle = new Paddle(0, 100, 90, 10);
    const ball = new Ball(45, 99, 1, 5, 0);
    const sectorAmount = 3;
    const minAngle = 30;
    const maxAngle = 150;
    const speed = 10;

    processReflection(paddle, ball, sectorAmount, minAngle, maxAngle, speed);

    expect(ball.horizontalSpeed).toBeCloseTo(0);
    expect(ball.verticalSpeed).toBeCloseTo(-speed);
  });
});
