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

void UpdateBandit(F32 delta, Bandit *bandit, World *world) {
  if (
      world->player.controls.shot && AABB_Slab(world->player.pos, world->player.shotPos, bandit->collision) && bandit->currentArea == world->player.currentArea)
  {
    printf("You shot the bandit %*.s\n", Sv(bandit->name));
    bandit->health--;
  }
  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;
      // TODO Shooting
      // TODO Running away
  }
}
