import { Job } from "./job/base-job";

export class Worker {
  x = 0;
  y = 0;
  speed = 1;
  carrying = 0;
  job: Job | null = null;
  path: any[] = [];
  pathIndex = 0;

  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }

  update(dt: number) {
    if (this.job) {
      this.job.update(dt);
      if (this.job.done) this.job = null;
    }
  }

  setJob(job: Job) {
    this.job = job;
    job.start();
  }

  setPath(path: any[]) {
    this.path = path;
    this.pathIndex = 0;
  }

  followPath(dt: number): boolean {
    if (this.path.length === 0) return true;
    const target = this.path[this.pathIndex];
    const dx = target.x - this.x;
    const dy = target.y - this.y;
    const dist = Math.sqrt(dx * dx + dy * dy);
    if (dist < 0.05) {
      this.x = target.x;
      this.y = target.y;
      this.pathIndex++;
      if (this.pathIndex >= this.path.length) {
        this.path = [];
        return true;
      }
    } else {
      const nx = dx / dist;
      const ny = dy / dist;
      this.x += nx * this.speed * dt;
      this.y += ny * this.speed * dt;
    }
    return false;
  }

  get idle(): boolean {
    console.log(this.job == null)
    return this.job == null;
  }
}