#include "raylib.h"
#include "raymath.h"
#include "stdio.h"

#include "keys.h"
#include "bullet.h"

Texture ninja_texture;
Texture ninja_attack_texture;

float dash_time = 0;

float main_attack_cooldown = 0;

void load_ninja()
{
    ninja_texture = LoadTexture("data/ninja.png");
    ninja_attack_texture = LoadTexture("data/ninja_attaque.png");
}

float ninja_radius()
{
    return fmax(ninja_texture.width, ninja_texture.height)/2;
}

int ninja_dash(Vector2 pos, Vector2 to, float sec, Vector2 *dash)
{
    static Vector2 target = {0};
    static int attack_type = 0;
    if (sec <= EPSILON)
        return attack_type;
    if (main_attack_cooldown <= EPSILON && key_pressed(KC_SHOOT)) {
        main_attack_cooldown = sec + 0.069;
        attack_type = KC_SHOOT;
        dash_time = sec;
        Vector2 diff = Vector2Subtract(to, pos);
        Vector2 scale = Vector2Normalize(diff);
        int dist = 200;
        scale.x *= dist;
        scale.y *= dist;
        target = Vector2Add(pos, scale);
    }
    else if (main_attack_cooldown <= EPSILON && key_pressed(KC_ULTI)) {
        sec *= 3;
        main_attack_cooldown = sec + 0.069;
        attack_type = KC_ULTI;
        dash_time = sec;
        Vector2 diff = Vector2Subtract(to, pos);
        Vector2 scale = Vector2Normalize(diff);
        int dist = 400;
        scale.x *= dist;
        scale.y *= dist;
        target = Vector2Add(pos, scale);
    }
    if (dash_time > EPSILON) {
        float dt = GetFrameTime();
        dash_time -= dt;
        dash->x = Lerp(pos.x, target.x, dt/sec) - pos.x;
        dash->y = Lerp(pos.y, target.y, dt/sec) - pos.y;
    } else {
        attack_type = 0;
    }
    if (main_attack_cooldown > EPSILON)
        main_attack_cooldown -= GetFrameTime();
    return attack_type;
}

float ninja_dash_time()
{
    return dash_time;
}

void hit_hitbox(Vector2 pos, float angle, float multiplicator)
{
    if (dash_time >= EPSILON) {
        Vector2 range = {ninja_texture.width, ninja_texture.height};
        range = Vector2Scale(range, multiplicator);
        Vector2 coll[] = {
            { range.x/2, 0},
            { range.x/2, -range.y},
            {-range.x/2, -range.y},
            {-range.x/2, 0},
            { range.x/2, 0},
            {-range.x/2, -range.y},
        };
        int coll_len = sizeof(coll) / sizeof(*coll);
        for (int i = 0; i < coll_len; i++) {
            coll[i] = Vector2Rotate(coll[i], angle * PI/180);
        }
        DrawTriangle((Vector2){ pos.x + coll[0].x, pos.y + coll[0].y },
                     (Vector2){ pos.x + coll[1].x, pos.y + coll[1].y },
                     (Vector2){ pos.x + coll[2].x, pos.y + coll[2].y }, VIOLET);
        DrawTriangle((Vector2){ pos.x + coll[3].x, pos.y + coll[3].y },
                     (Vector2){ pos.x + coll[4].x, pos.y + coll[4].y },
                     (Vector2){ pos.x + coll[5].x, pos.y + coll[5].y }, VIOLET);
    }
}

void draw_ninja(Vector2 pos, Camera2D *cam)
{
    Vector2 dash = {0};
    int is_attack = ninja_dash(GetScreenToWorld2D(pos, *cam), GetScreenToWorld2D(GetMousePosition(), *cam), 0.12, &dash);
    cam->target = Vector2Add(cam->target, dash);

    Texture n;
    if (dash_time >= EPSILON)
        n = ninja_attack_texture;
    else
        n = ninja_texture;

    Rectangle source = {0, 0, n.width, n.height};
    Rectangle dest = {pos.x, pos.y, n.width, n.height};
    Vector2 origin = {n.width/2, n.height/2};
    float angle = -Vector2LineAngle(pos, GetMousePosition());
    angle *= 180/PI;
    angle += 90;

    if (main_attack_cooldown <= EPSILON && key_pressed(KC_SPECIAL))
        bullet_spawn(BULLET_SHURIKEN, GetScreenToWorld2D(pos, *cam), angle - 90);

    DrawTexturePro(n, source, dest, origin, angle, WHITE);

    if (is_attack == KC_SHOOT)
        hit_hitbox(pos, angle, 1);
    else if (is_attack == KC_ULTI)
        hit_hitbox(pos, angle, 2);
}