import { GameMap } from "../game-map";
import { CollectMaterialJob } from "../job/collect-material.job";
import { Worker } from "../worker";
import { Building } from "./building";
import { ProductionBuilding } from "./production-building";
import { ResouceBuilding } from "./resource-building";

export class StorageBuilding extends ResouceBuilding {
    
  constructor(map: GameMap, x: number, y: number, buildings: Building[]) {
    super(map, x, y, buildings);
    this.type = 'storage';
  }

  assignWorker(worker: Worker): void {
    this.assignedWorker = worker;
  }

  override update(dt: number): void {
    super.update(dt);
    if (!this.assignedWorker) { return; }
    if (!this.assignedWorker.idle) { return; }
    if (this.storageFull) { return; }
    
    const target = this.findNearestProdBuilding();
    if (!target) {
      console.log("Not Target found");
      return;
    }

    this.assignedWorker.setJob(new CollectMaterialJob(this.map, this.assignedWorker, target, this))
  }


  findNearestProdBuilding(): ProductionBuilding | null {
    const prod = this.buildings.filter(b => b instanceof ProductionBuilding);
    // FINDE WOOD;
    const b = prod.find(b => b.type == "lumberjack" && b.stored.wood && b.stored.wood > 0);
    if (!b) { return null; }

    return b;
  }
}