import * as THREE from "three";
import { ICharacterAI } from "../../interfaces/ICharacterAI";
import { Character } from "../Character";

export class RandomBehaviour implements ICharacterAI {
  public character: Character;
  private prevStep: number = 0;
  private prevAnimation: string = null;

  public randomNumber(min: number, max: number): number { 
    return Math.round(Math.random() * (max - min) + min);
  }

  public update(timeStep: number): void {
    this.prevStep += timeStep;
    if (Math.floor(this.prevStep) > 5) { // calls once every 5 seconds
      this.prevStep = 0;
      this.character.triggerAction("up", true);
      const animate = this.randomNumber(0, 6);
      if (this.prevAnimation) {
        this.character.triggerAction(this.prevAnimation, false);
      }
      this.character.setViewVector(
        new THREE.Vector3(
          Math.random() - 0.5,
          Math.random() - 0.5,
          Math.random() - 0.5
        )
      );
      switch(animate) {
        case 0:
          this.prevAnimation = 'idle';
          this.character.triggerAction("idle", true);
          break;
        case 1:
          this.prevAnimation = 'run';
          this.character.triggerAction("run", true);
          break;
        case 2: 
          this.prevAnimation = 'jump';
          this.character.triggerAction("jump", true);
          break;
      }
    }
    this.character.charState.update(timeStep);
  }
}
