#include <SFML/Graphics.hpp>

#include "Paddle.hpp"
#include "Ball.hpp"

int main()
{
    sf::RenderWindow window(sf::VideoMode({ 500, 500 }), "Pong");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    Paddle p1Paddle(sf::Vector2f(25, 75), sf::Vector2f(0, 0), sf::Color::White), 
        p2Paddle(sf::Vector2f(25, 75), sf::Vector2f(window.getSize().x-p1Paddle.getSize().x, 0), sf::Color::White);
    Ball gameBall(p1Paddle.getSize().x * .5, sf::Vector2f(window.getSize().x * .5, 
        window.getSize().y * .5),
        sf::Color::Red,{-.01f,0.f});



    while (window.isOpen())
    {
        while (const std::optional event = window.pollEvent())
        {
            if (event->is<sf::Event::Closed>())
                window.close();
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
        {
            p1Paddle.move({ 0.f, -0.05f }); 
            // up
        }
        
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
        {
            p1Paddle.move({ 0.f, 0.05f });
            // down
        }

        if (gameBall.getPosition().y + gameBall.getRadius() * 2 > window.getSize().y)
        {
            sf::Vector2f oldVel = gameBall.getVelocity();
            oldVel.y *= -1;
            gameBall.setVelocity(oldVel);
        }

        if (gameBall.getPosition().y < 0)
        {
            sf::Vector2f oldVel = gameBall.getVelocity();
            oldVel.y *= -1;
            gameBall.setVelocity(oldVel);
        }

        if (gameBall.getGlobalBounds().findIntersection(p1Paddle.getGlobalBounds()).has_value())
        {
            sf::Vector2f velocity = gameBall.getVelocity(); 
            velocity.x *= -1;
            gameBall.setVelocity(velocity);
        }

        gameBall.move(gameBall.getVelocity());


        window.clear();
        window.draw(p1Paddle);
        window.draw(p2Paddle);
        window.draw(gameBall);
        window.display();
    }
}