#include "game/world.h"
#include "game/bandit.h"

V2f shootTowards(Bandit *bandit, V2f target, Random* r)
{
  V2f shooterV2 = bandit->collision.pos;
  F32 randX = Random_F32(r, -bandit->accuracyRange, bandit->accuracyRange);
  F32 randY = Random_F32(r, -bandit->accuracyRange, bandit->accuracyRange);
  return V2F(shooterV2.x + (target.x - shooterV2.x) * (1 + randX), shooterV2.x + (target.y - shooterV2.y) * (1 + randY));
}

void UpdateBandit(F32 delta, Bandit *bandit, World *world) {
  if (
      world->player.controls.shot && AABB_Slab(world->player.collision.pos, world->player.shotPos, bandit->collision) && bandit->currentArea == world->player.currentArea)
  {
    printf("You shot the bandit %*.s\n", Sv(bandit->name));
    bandit->health--;
  }
  if (AABB_Circle(bandit->agroRadius, AABB_Centre(bandit->collision), world->player.collision) && !(bandit->mode == BANDIT_SHOOTING || bandit->mode == BANDIT_SHOOTOUT))
  {
    printf("begin shootout");
    // shootout time o.o
    bandit->mode = BANDIT_SHOOTOUT;
  }
  switch (bandit->mode) {
    case BANDIT_WAITING:
      bandit->waitTime+=delta;
      if(bandit->waitTime > bandit->maxWaitTime) {
        bandit->mode = BANDIT_WALKING;
        do {
          U32 randomChoice = Random_U32(&world->random, 0, bandit->poiCount);
          bandit->targetNavNode = bandit->pointsOfInterest[randomChoice];
        } while(bandit->targetNavNode == bandit->currentNavNode);
        bandit->path = Nav_Path(world->navMesh, bandit->currentNavNode, bandit->targetNavNode);
        bandit->walkTimer = 0;
      }
    break;
    case BANDIT_WALKING:
      bandit->walkTimer+=delta;
      if(bandit->walkTimer >= NPC_SPEED){
        bandit->walkTimer = 0;
        if(bandit->path.nodeCount == bandit->pathIndex+1){
          bandit->mode = BANDIT_WAITING;
          bandit->maxWaitTime = Random_F32(&world->random, 20, 140);
          bandit->waitTime = 0;
          bandit->currentNavNode = bandit->targetNavNode;
          bandit->pathIndex = 0;
          return;
        }
        bandit->currentNavNode = bandit->path.indexes[bandit->pathIndex];
        bandit->pathIndex+=1;
      }
      NavNode cNav = world->navMesh->nodes[bandit->currentNavNode];
      NavNode tNav = world->navMesh->nodes[bandit->path.indexes[bandit->pathIndex]];
      bandit->collision.pos.x = cNav.pos.x * (1 - bandit->walkTimer/NPC_SPEED) + tNav.pos.x * bandit->walkTimer/NPC_SPEED;
      bandit->collision.pos.y = cNav.pos.y * (1 - bandit->walkTimer/NPC_SPEED) + tNav.pos.y * bandit->walkTimer/NPC_SPEED;
    break;
    case BANDIT_SHOOTOUT:
      bandit->shootoutTimer-=delta; 
      if(bandit->shootoutTimer < 0){
        bandit->mode=BANDIT_SHOOTING;
      }
    break;
    case BANDIT_SHOOTING:
      bandit->shootCooldownTimer -= delta;
      bandit->reloadTimer -= delta;
      if (bandit->shootCooldownTimer < 0 && bandit->reloadTimer < 0)
      {
        printf("shoot at player");
        bandit->bullets--;
        bandit->shootCooldownTimer = bandit->shootDelay;
        V2f banditShot = shootTowards(bandit, world->player.collision.pos, &world->random);
        if(AABB_Slab(bandit->collision.pos, banditShot, world->player.collision)){
          // gets shot lmao
          printf("hit");
          world->player.health--;
        }
        if(bandit->bullets == 0){
          printf("enemy reload");
          bandit->bullets = 6;
          bandit->reloadTimer = bandit->reloadTime;
        }
      }
    break;
      // TODO Running away
  }
}
