#include "shootingEnemy.hpp"
#include "bullet.hpp"
#include "enemyBullet.hpp"
#include "../world.hpp"
#include <cmath>
#include <app.hpp>
#include <app.hpp>

namespace game::entity {

ShootingEnemy::ShootingEnemy(Block *root, const vec2f &position, float health) noexcept: Enemy(root, position)
{
    this->body = this->world->CreateBody(this->position, b2_dynamicBody);

    b2PolygonShape shape;
    shape.SetAsBox(0.5, 0.5);

    this->health = health;

    b2FixtureDef fixDef;
    fixDef.filter.categoryBits = TYPE_ENEMY;
    fixDef.filter.maskBits = TYPE_HERO | TYPE_BULLET;
    fixDef.shape = &shape;
    fixDef.density = 1.0f;
    fixDef.userData = this;

    this->body->CreateFixture(&fixDef);

    this->reloadTime = this->cooldown = 2.5;

    this->velocity = b2Vec2(-4, 0);
    this->direction = false;

    this->changeDirectionCooldown = 2 + (rand()%100) / 100.0;


    switch (rand() % 2) {
    case 0:
        App::atlas->getIcon("shooter0.png", this->icon);
        break;
    case 1:
        App::atlas->getIcon("shooter1.png", this->icon);
        break;
    }

    this->icon.scale.x = 1 / 16.0;
    this->icon.scale.y = 1 / 16.0;

    this->points = 30;
}

ShootingEnemy::~ShootingEnemy()
{

}

void ShootingEnemy::OnCollide(Entity *entity)
{
    Bullet *bullet = dynamic_cast<Bullet*>(entity);

    if (bullet) {
        this->health -= bullet->damage;

        if (this->health <= 0) {
            this->Die();
        }
    }
}

void ShootingEnemy::Update() noexcept
{
    this->position.x = this->body->GetPosition().x;
    this->position.y = this->body->GetPosition().y;

    if (this->IsOnScreen()) {

        if (this->reloadTime > 0) {
            this->reloadTime -= App::GetInput()->deltaTime;

            if (this->reloadTime < 0) {
                this->reloadTime = 0;
            }
        } else {
            this->reloadTime = this->cooldown;

            vec2f playerVector = this->world->GetPlayerPosition() - this->position;

            float angle = atan2(playerVector.y, playerVector.x) * RTOD;

            new entity::EnemyBullet(this->world, this->position, vec2f{-4, 0}, 3, angle);
        }

        if (this->changeDirectionCooldown > 0) {
            this->changeDirectionCooldown -= App::GetInput()->deltaTime;
        } else {
            this->changeDirectionCooldown = 2 + (rand()%100) / 100.0;

            if (this->direction) {
                this->velocity = b2Vec2(-4, 0);
            } else if (this->position.y > 8.5) {
                this->velocity = b2Vec2(0, -2);
            } else {
                this->velocity = b2Vec2(0, 2);
            }

            this->direction = !this->direction;

        }

    } else if (this->position.x < 2) {
        this->world->RemoveManagedEntity(this);
    }

    this->body->SetLinearVelocity(this->velocity);

}

void ShootingEnemy::Render() noexcept
{
    this->icon.draw(this->position);
    //render::primitives::Circle(this->position, vec2f{0.5, 0.5}, color::green, 0, 16);
    //render::primitives::Circle(this->position - vec2f{0.4, 0.4}, vec2f{0.3, 0.1}, color::green, 45, 16);
    //render::primitives::Circle(this->position - vec2f{0.4, -0.4}, vec2f{0.3, 0.1}, color::green, -45, 16);
}

}

